php-general Digest 19 Jul 2011 14:04:37 -0000 Issue 7406
Topics (messages 314103 through 314115):
Re: How to sum monetary variables
314103 by: Jasper Mulder
314104 by: Tim Streater
314105 by: Adam Richardson
314106 by: admin.buskirkgraphics.com
314107 by: Richard Quadling
314108 by: Adam Richardson
314109 by: Paul M Foster
default option
314110 by: Chris Stinemetz
314111 by: Stuart Dallas
314112 by: Chris Stinemetz
? simple solution for error resulting from upgrade to php5
314113 by: Dr Michael Daly
314114 by: Tamara Temple
314115 by: Dr Michael Daly
Administrivia:
To subscribe to the digest, e-mail:
[email protected]
To unsubscribe from the digest, e-mail:
[email protected]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
> Date: Mon, 18 Jul 2011 19:00:52 -0300
> From: [email protected]
> To: [email protected]
> Subject: [PHP] How to sum monetary variables
>
> I'm building a table (which is a report that has to be printed) with a
> bunch of items (up to 300 in some cases) that have unitary price
> (stored in a numeric(9,2) field), how many there are, and the total
> price for each item. At the end of the table there is a total of all
> the items.
>
> The app is running on PHP and PostgreSQL is the backend.
>
> The question is, how do I get the total of everything?
>
> Running it on PHP gives one value, doing a sum() on the backend gives
> another, and I'm starting to notice that even using python as a
> calculator gives me errors (big ones). Right now I'm doing the maths
> by hand to find out who has the biggest error, or if any is 100%
> accurate.
>
> Any ideas?
According to the postgreSQL docs, there might occur an error as the sum()
output is coerced to have a 9 digit precision (so at most 9999999,99 as a
value), and as this is different from the PHP float interpretation, they might
yield different results in case of overflow. However as python supports
arbitrary integer arithmetic, overflows should not occur.
At the moment, still overflow errors seem the most likely explanation. Does
your table consist of very large values (occasionally perhaps)?
Could you give us an example?
Best regards,
Jasper
--- End Message ---
--- Begin Message ---
On 18 Jul 2011 at 23:00, Martín Marqués <[email protected]> wrote:
> I'm building a table (which is a report that has to be printed) with a
> bunch of items (up to 300 in some cases) that have unitary price
> (stored in a numeric(9,2) field), how many there are, and the total
> price for each item. At the end of the table there is a total of all
> the items.
>
> The app is running on PHP and PostgreSQL is the backend.
>
> The question is, how do I get the total of everything?
>
> Running it on PHP gives one value, doing a sum() on the backend gives
> another, and I'm starting to notice that even using python as a
> calculator gives me errors (big ones). Right now I'm doing the maths
> by hand to find out who has the biggest error, or if any is 100%
> accurate.
Much safer to price everything internally in pence or cents or whatever, and
convert to £xxx.xx for external display. Then just use integer arithmetic for
the calculations.
--
Cheers -- Tim
--- End Message ---
--- Begin Message ---
2011/7/18 Martín Marqués <[email protected]>
> I'm building a table (which is a report that has to be printed) with a
> bunch of items (up to 300 in some cases) that have unitary price
> (stored in a numeric(9,2) field), how many there are, and the total
> price for each item. At the end of the table there is a total of all
> the items.
>
> The app is running on PHP and PostgreSQL is the backend.
>
> The question is, how do I get the total of everything?
>
> Running it on PHP gives one value, doing a sum() on the backend gives
> another, and I'm starting to notice that even using python as a
> calculator gives me errors (big ones). Right now I'm doing the maths
> by hand to find out who has the biggest error, or if any is 100%
> accurate.
>
> Any ideas?
>
Hi,
I've not had issues with PostgreSQL when using the numeric data type. That
said, when you need more precision than PHP's standard handling of floating
points (http://php.net/manual/en/language.types.float.php), you can use
PHP's BC Math functions to enforce arbitrary precision:
http://www.php.net/manual/en/ref.bc.php
<http://www.php.net/manual/en/ref.bc.php>
Adam
--
Nephtali: A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com
--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: Martín Marqués [mailto:[email protected]]
> Sent: Monday, July 18, 2011 6:01 PM
> To: PHP General
> Subject: [PHP] How to sum monetary variables
>
> I'm building a table (which is a report that has to be printed) with a
> bunch of items (up to 300 in some cases) that have unitary price
> (stored in a numeric(9,2) field), how many there are, and the total
> price for each item. At the end of the table there is a total of all
> the items.
>
> The app is running on PHP and PostgreSQL is the backend.
>
> The question is, how do I get the total of everything?
>
> Running it on PHP gives one value, doing a sum() on the backend gives
> another, and I'm starting to notice that even using python as a
> calculator gives me errors (big ones). Right now I'm doing the maths
> by hand to find out who has the biggest error, or if any is 100%
> accurate.
>
> Any ideas?
>
> --
> Martín Marqués
> select 'martin.marques' || '@' || 'gmail.com'
> DBA, Programador, Administrador
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
Can you show us some examples of what you have tried please.
Richard L. Buskirk
--- End Message ---
--- Begin Message ---
2011/7/18 Martín Marqués <[email protected]>:
> I'm building a table (which is a report that has to be printed) with a
> bunch of items (up to 300 in some cases) that have unitary price
> (stored in a numeric(9,2) field), how many there are, and the total
> price for each item. At the end of the table there is a total of all
> the items.
>
> The app is running on PHP and PostgreSQL is the backend.
>
> The question is, how do I get the total of everything?
>
> Running it on PHP gives one value, doing a sum() on the backend gives
> another, and I'm starting to notice that even using python as a
> calculator gives me errors (big ones). Right now I'm doing the maths
> by hand to find out who has the biggest error, or if any is 100%
> accurate.
>
> Any ideas?
For financial values, I use the money type.
I use MS SQL, but PostgreSQL has
http://www.postgresql.org/docs/9.0/interactive/datatype-money.html
--
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea
--- End Message ---
--- Begin Message ---
2011/7/18 Richard Quadling <[email protected]>
> 2011/7/18 Martín Marqués <[email protected]>:
> >
> > Any ideas?
>
> For financial values, I use the money type.
>
> I use MS SQL, but PostgreSQL has
> http://www.postgresql.org/docs/9.0/interactive/datatype-money.html
>
The version of PostgreSQL plays a role, too, as at one point the money type
was deprecated (and I still tend to use numeric, even though work has been
done to improve the money type):
http://archives.postgresql.org/pgsql-general/2008-05/msg00979.php
http://www.postgresql.org/docs/8.2/static/datatype-money.html
<http://archives.postgresql.org/pgsql-general/2008-05/msg00979.php>Adam
--
Nephtali: A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com
--- End Message ---
--- Begin Message ---
On Mon, Jul 18, 2011 at 11:22:00PM +0100, Tim Streater wrote:
> On 18 Jul 2011 at 23:00, Martín Marqués <[email protected]> wrote:
>
> > I'm building a table (which is a report that has to be printed) with a
> > bunch of items (up to 300 in some cases) that have unitary price
> > (stored in a numeric(9,2) field), how many there are, and the total
> > price for each item. At the end of the table there is a total of all
> > the items.
> >
> > The app is running on PHP and PostgreSQL is the backend.
> >
> > The question is, how do I get the total of everything?
> >
> > Running it on PHP gives one value, doing a sum() on the backend gives
> > another, and I'm starting to notice that even using python as a
> > calculator gives me errors (big ones). Right now I'm doing the maths
> > by hand to find out who has the biggest error, or if any is 100%
> > accurate.
>
> Much safer to price everything internally in pence or cents or whatever, and
> convert to £xxx.xx for external display. Then just use integer arithmetic for
> the calculations.
Let me echo this. If you need precise arithmetic in an environment where
you're doing mostly adds and subtracts, store data as integers and do
your math that way. Only convert to decimal for display. If you're doing
multiplies and divides, do them with the multi-precision PHP functions.
Paul
--
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com
--- End Message ---
--- Begin Message ---
Hello,
I am building some select menu's dynamically from a mysql database and
am courous how to give the menu a default option "Choose".
Below is what I have so far for one of my menu's.
Thank you in advace.
<select name="market" id="market"
onchange="javascript:get(this.parentNode);">
<?php
foreach($market_prefix as $key => $value)
{
$selected = '';
if($value == $market)
{
$selected = 'selected';
}
echo("<option value=$value $selected
>$value : $market_name[$key]");
}
?>
</select>
--- End Message ---
--- Begin Message ---
On Tue, Jul 19, 2011 at 4:28 AM, Chris Stinemetz
<[email protected]>wrote:
> Hello,
>
> I am building some select menu's dynamically from a mysql database and
> am courous how to give the menu a default option "Choose".
>
> Below is what I have so far for one of my menu's.
>
> Thank you in advace.
>
> <select name="market" id="market"
> onchange="javascript:get(this.parentNode);">
> <?php
> foreach($market_prefix as $key => $value)
> {
> $selected = '';
> if($value == $market)
> {
> $selected = 'selected';
> }
> echo("<option value=$value $selected
> >$value : $market_name[$key]");
> }
> ?>
> </select>
Just add it as the first option. If none of the options have the selected
attribute, the first option will be the selected one.
I'd also recommend you escape the variables you're outputting, on the off
chance they contain HTML-like code.
<select name="market" id="market" onchange="if (this.value != '') {
javascript:get(this.parentNode); }">
<option value="">Choose...</option>
<?php
foreach($market_prefix as $key => $value)
{
$selected = '';
if ($value == $market)
{
$selected = 'selected';
}
echo '<option value="', htmlspecialchars($value), '" ', $selected, '>',
htmlspecialchars($value.' : '.$market_name[$key]), '</option>';
}
?>
</select>
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--- End Message ---
--- Begin Message ---
Thank you Stuart. That did the trick.
On Mon, Jul 18, 2011 at 10:38 PM, Stuart Dallas <[email protected]> wrote:
> On Tue, Jul 19, 2011 at 4:28 AM, Chris Stinemetz <[email protected]>
> wrote:
>>
>> Hello,
>>
>> I am building some select menu's dynamically from a mysql database and
>> am courous how to give the menu a default option "Choose".
>>
>> Below is what I have so far for one of my menu's.
>>
>> Thank you in advace.
>>
>> <select name="market" id="market"
>> onchange="javascript:get(this.parentNode);">
>> <?php
>> foreach($market_prefix as $key => $value)
>> {
>> $selected = '';
>> if($value == $market)
>> {
>> $selected = 'selected';
>> }
>> echo("<option value=$value $selected
>> >$value : $market_name[$key]");
>> }
>> ?>
>> </select>
>
> Just add it as the first option. If none of the options have the selected
> attribute, the first option will be the selected one.
> I'd also recommend you escape the variables you're outputting, on the off
> chance they contain HTML-like code.
> <select name="market" id="market" onchange="if (this.value != '') {
> javascript:get(this.parentNode); }">
> <option value="">Choose...</option>
> <?php
> foreach($market_prefix as $key => $value)
> {
> $selected = '';
> if ($value == $market)
> {
> $selected = 'selected';
> }
> echo '<option value="', htmlspecialchars($value), '" ', $selected, '>',
> htmlspecialchars($value.' : '.$market_name[$key]), '</option>';
> }
> ?>
> </select>
> -Stuart
>
> --
> Stuart Dallas
> 3ft9 Ltd
> http://3ft9.com/
--- End Message ---
--- Begin Message ---
Hi
is there a simple solution here, other than reverting to php4?
An upgrade from php5 to php5 has resulted in an error msg in this line:
if( strlen($db_res ) > 0 ) {
I understand this is bec. php5 is object orientated. It says an
Object of class DB_result could not be converted to a string
This is the full function:
function pbcs_db_query( $q , $die_on_error=true, $ext_db_handle="" ) {
global $db_handle;
global $db_res;
global $query_logging,$PHP_SELF;
global $short_query_cache,$short_query_caching;
if( $ext_db_handle == "" )
$query_db_handle = $db_handle;
else
$query_db_handle = $ext_db_handle;
if( $short_query_caching && strlen( $q ) < 80 ) {
$db_res = $short_query_cache[str_replace(" ","",$q)];
if( strlen($db_res ) > 0 ) {
//do_log("object.log","READ: ".$db_res);
// $db_res->resetResultSet( 0 );
mysql_data_seek( $db_res->result , 0 );
//do_log("query.log",$PHP_SELF." - FROM-CACHE - ".$q);
return $db_res;
}
}
Thanks
Michael
--- End Message ---
--- Begin Message ---
On Jul 19, 2011, at 3:05 AM, Dr Michael Daly wrote:
Hi
is there a simple solution here, other than reverting to php4?
An upgrade from php5 to php5 has resulted in an error msg in this
line:
if( strlen($db_res ) > 0 ) {
I understand this is bec. php5 is object orientated. It says an
Object of class DB_result could not be converted to a string
This is the full function:
function pbcs_db_query( $q , $die_on_error=true, $ext_db_handle="" ) {
global $db_handle;
global $db_res;
global $query_logging,$PHP_SELF;
global $short_query_cache,$short_query_caching;
if( $ext_db_handle == "" )
$query_db_handle = $db_handle;
else
$query_db_handle = $ext_db_handle;
if( $short_query_caching && strlen( $q ) < 80 ) {
$db_res = $short_query_cache[str_replace(" ","",$q)];
if( strlen($db_res ) > 0 ) {
//do_log("object.log","READ: ".$db_res);
// $db_res->resetResultSet( 0 );
mysql_data_seek( $db_res->result , 0 );
//do_log("query.log",$PHP_SELF." - FROM-CACHE - ".$q);
return $db_res;
}
}
What does short_query_cache[] contain?
--- End Message ---
--- Begin Message ---
Hi
short_query_caching is referenced only once more in the script:
if( $query_logging )
do_log("query.log",$PHP_SELF." - ".$q);
if (preg_match('/^\s*delete/i', $q)) {
$p = $query_db_handle->prepare($q);
if (DB::isError($p) && $die_on_error ) { die ("PREPARE:\n".$q .
"\n<br>\n" . $p->getMessage() . "\n<br>\n" . $p->getCode()); }
$db_res = $query_db_handle->execute($p);
if (DB::isError($db_res) && $die_on_error ) { die
("EXECUTE:\n".$q . "\n<br>\n" . $db_res->getMessage() .
"\n<br>\n" . $db_res->getCode()); }
} else {
$db_res = $query_db_handle->query($q);
if (DB::isError($db_res) && $die_on_error ) { die ($q .
"\n<br>\n" . $db_res->getMessage() . "\n<br>\n" .
$db_res->getCode()); }
}
// Let's cache all single row results and serve those cached values, if
// the query string matches exaclty.
if( $short_query_caching && is_object($db_res) && $db_res->numRows() ==
1
&& strlen($q) <= 80) {
//do_log("query.log","$PHP_SELF - CACHED $q");
$short_query_cache[str_replace(" ","",$q)] = $db_res ;
// print "<br>";
}
return $db_res;
}
Michael
On Jul 19, 2011, at 3:05 AM, Dr Michael Daly wrote:
> Hi
> is there a simple solution here, other than reverting to php4?
> An upgrade from php5 to php5 has resulted in an error msg in this
> line:
>
> if( strlen($db_res ) > 0 ) {
>
> I understand this is bec. php5 is object orientated. It says an
> Object of class DB_result could not be converted to a string
>
>
> This is the full function:
>
>
> function pbcs_db_query( $q , $die_on_error=true, $ext_db_handle="" ) {
> global $db_handle;
> global $db_res;
> global $query_logging,$PHP_SELF;
> global $short_query_cache,$short_query_caching;
>
> if( $ext_db_handle == "" )
> $query_db_handle = $db_handle;
> else
> $query_db_handle = $ext_db_handle;
>
> if( $short_query_caching && strlen( $q ) < 80 ) {
> $db_res = $short_query_cache[str_replace(" ","",$q)];
> if( strlen($db_res ) > 0 ) {
> //do_log("object.log","READ: ".$db_res);
> // $db_res->resetResultSet( 0 );
> mysql_data_seek( $db_res->result , 0 );
> //do_log("query.log",$PHP_SELF." - FROM-CACHE - ".$q);
>
> return $db_res;
> }
> }
What does short_query_cache[] contain?
Dr Michael Daly MB, BS
GradDip(Integrative Medicine), GradCert(Evidence Based Practice),
M Bus(Information Innovation), GradDip(Document Management)
03 9521 0352
0413 879 029
--- End Message ---