Greenguy's Board

Greenguy's Board (http://www.greenguysboard.com/board/index.php)
-   Programming & Scripting (http://www.greenguysboard.com/board/forumdisplay.php?f=15)
-   -   PHP function round(); (http://www.greenguysboard.com/board/showthread.php?t=19563)

Barron 2005-05-12 03:01 PM

PHP function round();
 
I've been dinking with this since yesterday, dinked with it in the past and still not solid solution.

Scenario: I have a list that grows and gets smaller on the fly. There may be 8 things in the list, or 108 things in the list.

For page design purposes I decided I want this list in a table with the number of cells in the table increasing and decreaseing with the size of the list to a maximum number of cells. As the list grows, the number of items in each cell will increase so that an equal number of items is in the first 6 cells, with overflow(if there is an overflow) of items going in the last cell.

I couldnt get the round(); function to work properly for me because I always want the number to round to the next highest whole number regardless of the size of the decimal. Example: 5.00000000001 would round up to 6.

Now I am forced to make this work because my work around that has been working for months, suddenly choked and the script dies.

Quote:

$count_categories_build=count($categories_build);
$how_many_category_columns=$count_categories_build/7;
$this_many_category_columns=explode(".", $how_many_category_columns);
if ($this_many_category_columns[1])
{
// True, there is a decimal
// round up.
$columns=$this_many_category_columns[0]+1;
}
else
{
$columns =$this_many_category_columns[0];
}
?>
This snippet of code has worked for a very long time, but over the weekend PHP suddenly see's $columns as an array and errors:

Quote:

$count_table=1;
foreach ($categories_build as $key=>$value)
{
$row=$count_table/$columns;
$ex_row=explode(".", $row);
$categories .=$value;
if (!$ex_row[1])
{
$categories.="
";
}
$count_table++;
}
I created a work around for the work around, but now if there is exactly the same number if entries in each of the columns, the last column is created with nothing in it.

Any suggestions on how to make round(); work for me?


-

cd34 2005-05-12 03:12 PM

$rounded_count = (int) ($count + 0.5);

unless you need bankers rounding. Most people don't.

Barron 2005-05-12 05:38 PM

Perfect! :)

-

Sams 2005-05-12 05:41 PM

Use ceil() function.
Quote:

float ceil ( float value)

Returns the next highest integer value by rounding up value if necessary. The return value of ceil() is still of type float as the value range of float is usually bigger than that of integer.
$columns=ceil($count_categories_build/7);

cd34 2005-05-12 05:49 PM

sorry, Sams is right, you wanted ceil, not round.

ceil(5.000001) = 6
floor(5.000001) = 5
round(5.000001) = 5
round(5.600001) = 6

Barron 2005-05-13 12:36 PM

Wow! Very different results using the two different ways of handling the round up.

In my first post I showed two snippets of code. The second snippet is what produces the table cells that will be sent to the browser. I didnt alter that part of the code. Here are the results of what happened:
Quote:

Quote:


$columns = (int) ($this_many_category_columns[0] + 0.5);

Using this, the number rounded down. But, the interesting part is that with 1-6 number of items I got the desired effect, 1 item in each of 6 columns. On the 7th item, I got 7 columns, but the 8th column was created with nothing in it. However, as I increased the number of items, every multiple of 7 gave me an extra column. By the time I got to 49 items in the list I recieved 13 columns. not good.
Quote:

Quote:


$columns=ceil($count_categories_build/7);

Using ceil(); the number did round up nd I "did not" get an extra column for every multiple of 7. (I eliminated 5 lines of code, not counting the bracket lines)However, for each mulitiple of 7 I did get a blank 8th column. If the number of items was not a multiple of seven, I didnt not get the desired effect. I recieved 6 columns, with overflow going to the 7th column. I am suppose to get 7 columns with the over flow in the eighth column.

I changed the 7 to a 6 and got different results. So experimented with different numbers. Every odd number produced the same results as any other odd number, and every even number produced the same results as any other even number.
Anyway, I thought it was interesting the way PHP handled the two different lines of code. All I need to do know is eliminate the column with nothing in it and that will be easy.

Sams and cd34, thanks for your help! |thumb


-

cd34 2005-05-13 01:13 PM

generally I use modulo or % to do this.

basically, set your $loop = 0;

if ($loop % 7) { print "
" }

$loop modulo 7 = 0 when there is no remainder, i.e., the column is divisible by 7 (or if the loop is 0) You need to do some special case handling for 0, and some end of loop processing if there aren't a multiple of 7 columns in your result set.

The reason for your extra column is probably that you started loop at 1, and are dividing by 7, which gives you a first blank column -- then things should realign themselves unless there is something else I'm not seeing. From what you posted, you should get 6 columns in the first row, 7 in each successive row.

Another way is to use an indexed array and use two for loops...

Code:

print "";
for ($loop=0;loop<$numcols;loop+=7) {
  print "";
  for ($loop2=$loop;$loop2<$loop+7;$loop2++) {
    print "";
  }
  print "";
}
print "
asdfa
";

Many different ways to handle tabular data.... and they all boil down to a simple control-break structure.

Barron 2005-05-13 03:48 PM

Your right, I may end up changing the routine to a nested loop of some kind to catch the end of the list.

Your example brings to mind something I've been wanting to ask someone for a long time.

I read a post on php.net about 2 years ago I think, where some guy benchmarked, do, for, foreach and while. The slowest was for when it came to speed and do being the fastest. If I remember correctly do is 60% faster than for. Since then, I stopped using for statements unless dealing with very small arrays. I try to trim off any excess load that I can.

Have you noticed any speed difference between any of these?


-


All times are GMT -4. The time now is 12:49 AM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© Greenguy Marketing Inc