php-general Digest 19 Dec 2011 21:13:05 -0000 Issue 7617

Topics (messages 316063 through 316068):

number_format
        316063 by: Floyd Resler
        316064 by: Bastien Koert
        316065 by: Ken Robinson
        316066 by: Robert Cummings
        316067 by: Floyd Resler

Re: Working on a Subsummary Report
        316068 by: Jim Lucas

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 ---
In the previous version of PHP we were using, I could pass a string to 
number_format and it would just change it to a 0 without complaint.  With 5.3.6 
I get an "expects double" error.  I don't suppose there's a way to make it work 
like it used to???  I'm dealing with really old code that wasn't very well 
structured.

Thanks!
Floyd


--- End Message ---
--- Begin Message ---
On Mon, Dec 19, 2011 at 9:19 AM, Floyd Resler <[email protected]> wrote:
> In the previous version of PHP we were using, I could pass a string to 
> number_format and it would just change it to a 0 without complaint.  With 
> 5.3.6 I get an "expects double" error.  I don't suppose there's a way to make 
> it work like it used to???  I'm dealing with really old code that wasn't very 
> well structured.
>
> Thanks!
> Floyd
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


Could you check it before formatting? Or if the data is coming from
the db, force it to 0 if null?

$var = (is_double($number_var)) ? $number_var : 0;



-- 

Bastien

Cat, the other other white meat

--- End Message ---
--- Begin Message ---
Quoting Floyd Resler <[email protected]>:

In the previous version of PHP we were using, I could pass a string to number_format and it would just change it to a 0 without complaint. With 5.3.6 I get an "expects double" error. I don't suppose there's a way to make it work like it used to??? I'm dealing with really old code that wasn't very well structured.

The way I would get around this would be to create a function like this:

function my_number_format($number, $decimals = 0, $dec_point = '.', $thousands_sep = ',') {
   $number += 0;  //convert value to a number.
   return number_format($number, $decimals, $dec_point, $thousands_sep);
}

The just do a global replace of number_format with my_number_format.

That should work, although I haven't tested it...

Ken


--- End Message ---
--- Begin Message ---
On 11-12-19 11:08 AM, Bastien Koert wrote:
On Mon, Dec 19, 2011 at 9:19 AM, Floyd Resler<[email protected]>  wrote:
In the previous version of PHP we were using, I could pass a string to number_format and 
it would just change it to a 0 without complaint.  With 5.3.6 I get an "expects 
double" error.  I don't suppose there's a way to make it work like it used to???  
I'm dealing with really old code that wasn't very well structured.

Thanks!
Floyd


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Could you check it before formatting? Or if the data is coming from
the db, force it to 0 if null?

$var = (is_double($number_var)) ? $number_var : 0;

Or possibly:

<?php

number_format( (float)$your_input, 2 );

?>

Cheers,
Rob.





--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.

--- End Message ---
--- Begin Message ---
On Dec 19, 2011, at 11:38 AM, Robert Cummings wrote:

> On 11-12-19 11:08 AM, Bastien Koert wrote:
>> On Mon, Dec 19, 2011 at 9:19 AM, Floyd Resler<[email protected]>  wrote:
>>> In the previous version of PHP we were using, I could pass a string to 
>>> number_format and it would just change it to a 0 without complaint.  With 
>>> 5.3.6 I get an "expects double" error.  I don't suppose there's a way to 
>>> make it work like it used to???  I'm dealing with really old code that 
>>> wasn't very well structured.
>>> 
>>> Thanks!
>>> Floyd
>>> 
>>> 
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>> 
>> 
>> 
>> Could you check it before formatting? Or if the data is coming from
>> the db, force it to 0 if null?
>> 
>> $var = (is_double($number_var)) ? $number_var : 0;
> 
> Or possibly:
> 
> <?php
> 
> number_format( (float)$your_input, 2 );
> 
> ?>
> 
> Cheers,
> Rob.
> 

That worked beautifully!

Thanks!
Floyd



--- End Message ---
--- Begin Message ---
On 12/17/2011 4:21 PM, DealTek wrote:
> 
> On Dec 16, 2011, at 12:56 PM, Jim Lucas wrote:
>>
>>
>> 1) What does your db schema look like?
>> 2) What SQL do you currently use?
>> 3) What criteria do you want to use to sort the data?
>> 4) Will the output be plaintext, html, etc?
>> 5) Is this going to be used to import into another app, or display & 
>> printing?
>>
> 
> Hi Jim - sorry I didn't see this earlier...
> 
> 
> 1 - schema - think of a basic 2 table system  parent table and line items 
> table... - but really all the important fields are in the child line items...
> 2 - mysql 5.xx
> 3 - sort 1st by date (all records happen 1st of every month) then product 
> (only 2 products needed for this)
> 4 - html for now
> 5 - for now just display and printing like:
> 
> 
> JAN 2011
> --- PRODUCT 1
> 
> data row 1
> data row 2
> 
> --- PRODUCT 2
> 
> data row 3
> data row 4
> 
> like that.......
> 
> 
> - thanks for checking this out....
> 
> 
> 
> 
> --
> Thanks,
> Dave - DealTek
> [email protected]
> [db-11]

Well, by the sounds of it, you are using a join to combine the two tables into
one result set and you have doubling up on the parent information.

I would do it like this.

$SQL = 'SELECT p_id, month_name, prod_id, l_id, descr';

$results = query($SQL);

while ( $r = fetch_assoc($results) ) {
  $data[$r['p_id']]['descr'] = $r['month_name'];
  $data[$r['p_id']]['x'][$r['prod_id']]['descr'] = $r['descr'];
  $data[$r['p_id']]['x'][$r['prod_id']]['x'][$r['l_id']] = $r;
}

print_r($data);

Now, use instead of looping through the result set multiple times, us the
$dataSet array to loop through it once.

foreach ( $data AS $months ) {
  echo "{$months['month']}\n";
  foreach ( $months['x'] AS $prod_id => $products ) {
    echo "--- {$products['descr']} #{$prod_id}\n\n";
    foreach ( $products['x'] AS $product ) {
      echo str_pad($product['l_id'], 16, ' ', STR_PAD_LEFT), '  ',
           str_pad($product['descr'], 32, ' ', STR_PAD_RIGHT), PHP_EOL;
    }
    echo PHP_EOL;
  }
  echo PHP_EOL;
}

YMMV - This is completely untested,  It should give you an idea about how to
prepare/store the data and then display it.

-- 
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
http://www.bendsource.com/

--- End Message ---

Reply via email to