TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-14-2008, 04:44 AM   #1 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default 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
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 05-14-2008, 05:04 AM   #2 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Hmm shorter version:

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

__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!

Last edited by Nor : 05-14-2008 at 03:28 PM.
Nor is offline  
Reply With Quote
Old 05-14-2008, 03:02 PM   #3 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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++;

__________________

Village Idiot is offline  
Reply With Quote
Old 05-14-2008, 03:35 PM   #4 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

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.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 05-14-2008, 03:51 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:25 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design