Jump to content

Recommended Posts

Hi all,

 

For some reason I cant wrap my head around a concept, its pretty simple if I explain it English but my tired brain can't follow the path to coding it.

 

Basically what I want to do is this

 

count rows in database (ive got this bit :P)

divide rows by 20

display 1st 20 results in <div id=a>

divide the remaing results by 20 and create a <div id=b> for each

 

<div id=a> displays the 1st 20

<div id=b> displays 21-40

<div id=b> displays 41-60

 

etc

 

its basically for a jquery pagination script where each set of results is held in a div

 

Hope someone can point me in the right direction!

 

many thanks

Link to comment
https://forums.phpfreaks.com/topic/242397-help-with-achieving-method/
Share on other sites

Either I am misunderstanding what you want or the problem is super simple. You don't need to count the rows, just get all the results and process them in-line. BUt, I would highly suggest using a numeric id for the divs. It will make life so much simper. Otherwise you have to decide what happens if you were to have more than 26 divs. Do you use 'aa', 'ab', ... or what.

 

$rows_per_div = 20;

$query = "SELECT * FROM table_name";
$result = myslq_query($query);

$html = '';
$rowNum = 0; 
while($row = mysql_fetch_assoc($result))
{
    $rowNum++;
    //Open new div if first record in set
    if($rowNum%$rows_per_div == 1)
    {
        $html .= "<div id=\"$rowNum\">\n";
    }
    
    //Display current record
    $html .= "{$row['name']}<br>\n";

    //Close div if max records reached
    if($rowNum%$rows_per_div == 0)
    {
        $html .= "</div>\n";
    }
}
//Close last div if still open
if($rowNum%$rows_per_div != 0)
{
    $html .= "</div>\n";
}

 

If you really, really need to use an alphabetically named id's for the divs, replace this

        $html .= "<div id=\"$rowNum\">\n";

with this

        $html .= "<div id=\"" . chr($rowNum+96) . "\">\n";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.