TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Tips & Tricks (http://www.talkphp.com/tips-tricks/)
-   -   Finish Table Columns With Number Divisibility Function (http://www.talkphp.com/tips-tricks/2805-finish-table-columns-number-divisibility-function.html)

Nor 05-14-2008 04:44 AM

Finish Table Columns With Number Divisibility Function
 
Well I'm working on a script where you must complete the table to look right or I might be missing a TD which I can't have, so I had this problem where I only had 9 results in the db and I needed to know how many more I needed to add(columns) to complete a four column table.

So:

1 2 3 4
5 6 7 8
9 | | |

the |'s didn't display since I didn't know how to get the extra td's and came up with this function.

PHP Code:

function number_divisibility($num1,$num2)
{
    
$save 0;
    if(
$num1 $num2 == 0) return 0;
    if(
$num1 $num2 != 0)
    {
        while(
True)
        {
            
$save++;
            if((
$num1+$save) % $num2 == 0) return $save;
        }
    }


PHP Code:

number_divisibility(9,4);//outputs 3 :D 

Pretty handy function

Nor 05-14-2008 05:04 AM

Hmm shorter version:

PHP Code:

function number_divisibility($num1,$num2)
{
    if(
$num1 $num2 == 0) return 0;
    return 
$num2 - ($num1 $num2);



Village Idiot 05-14-2008 03:02 PM

I dont get what you are trying to do. Are you trying to fill in the number of remaining TDs left from whatever you are doing? If so, do this

PHP Code:

//The number of TDs per row
$max 4;

//the number of results returned
$results 9;

//The offset we will be using
$offset 0;

//Add until the number of rows we are displaying is divisible by your max number of spaces per row.
while(($results+$offset)%$max != 0)
{
echo 
"<td></td>";
$offset++;



Nor 05-14-2008 03:35 PM

I'm not trying to do nothing atm as I did it.

And yes I was doing that but in a different/shorter way

I had a table with 4 columns and I had 9 results that had to fit in the table, SO I made this function to tell me how many columns are needed to make a complete 4 by # without leaving off a couple columns to make my table not be incomplete.

And my function is shorter then using that code multiple times(repeated code) since I need this function 3 times in a class I'm creating for different table column sizes:

PHP Code:

function number_divisibility($num1,$num2)
{
    if(
$num1 $num2 == 0) return 0;
    return 
$num2 - ($num1 $num2);


So you might get the point of this function and I'm not asking for help, but letting other people get help from many people might need to use this at some point in PHP.

Salathe 05-14-2008 03:51 PM

You could also calculate the remainder another way which negates the need to check for the case where the cells fit nicely into the grid. It's just another way of doing the same thing.

PHP Code:

function remaining_cells($cells$cols)
{
    return (
$cols ceil($cells $cols)) - $cells;
}
// Example use: $cells = 17; $cols = 4; ... remainder = 3;
str_repeat('<td>&nbsp;</td>'remaining_cells($cells$cols); 

Good work on refactoring your original 13 lines function down to 5. :-)


All times are GMT. The time now is 10:05 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0