[SOLVED] Tricky pagination question...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

[SOLVED] Tricky pagination question...

Post by Steveo31 »

Hey guys, long time no talk :)

I'm making a word list of about 121,000+ words for a game a few friends and I made. Now, I'd like this list to be available to them 24/7, so what better place than a website? Anyway...

I need to pull about 225 words per page. I had a method that worked for a bit, but in the end was a catastrophe. I need to have 75 words in 3 columns on each page. I'm out of ideas.

I looked at the pagination snip here and there's errors with it. I can't seem to find a script that works for something like this. Can you guys send some links or pointers on this? I think I need to use the modulus operator, but I can't get it to work.

Thanks.
Last edited by Steveo31 on Fri Sep 03, 2004 5:59 pm, edited 1 time in total.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Ok, got a bit further...

Code: Select all

if(empty($_GET['page'])){
                $page = 1;
            }else{
                $page = $_GET['page'];
            }
            $limit = 225;
            
            $countQuery = mysql_query("SELECT COUNT(*) AS totalNum FROM {$_GET['letter']}");
            $count = mysql_fetch_assoc($countQuery);
            $total = $count['totalNum'];
            $totalPages = $total/$limit;
            
            $from = ($page-1)*$limit;
            $to = ($page*$limit)-1;
            
            $query = mysql_query("SELECT word, id FROM {$_GET['letter']} LIMIT $from, $to");
            echo "<table width='100%'>";
            echo "<td width='33%'>";
            while($row = mysql_fetch_assoc($query)){
                echo $row['id'].":  "."<a class='largeLink' href='http://dictionary.reference.com/search?q={$row['word']}'>".$row['word']."</a><br />";
                if($row['id'] == ($limit)*(1/3)){
                    echo "</td><td width='33%'>";
                }
                if($row['id'] == ($limit)*(2/3)){
                    echo "</td><td width='33%'>";
                }
            }
            echo "</table>";
That works for records 1-224. After that, it doesn't cause $row['id'] is no longer 225 or under. Hmm....
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

For each page, grab the 225 into an array. And then split them up into three arrays with something like:

Code: Select all

// untested code
foreach ($dbarrayof225 as $key => $val)
{
   if ($key >= 0 && $key <= 74) {
        $firstarray[] = $val;
    } elseif ($key >= 75 && $key <= 150) {
        $secondarray[] = $val;
    } elseif ($key >= 151 && $key <= 255) {
        $thirdarray[] = $val;
    } else {
        $extraarray[] = $val;
    }
}
Then you have the three arrays for the three columns.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Ooooh... good idea. I'd love to try it, but I have to get ready for work. :(

Thanks sami.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

No problemo. :)
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

I looked at it and played with it, and I now have 2 problems. One, the query selects more than it should:

Code: Select all

$limit = 225;
$from = ($page-1)*$limit;
$to = ($page*$limit)-1;
echo "FROM:  ".$from;
echo " TO:  ".$to."<br />";

$query = mysql_query("SELECT word, id FROM {$_GET['letter']} LIMIT $from, $to");

while($row = mysql_fetch_assoc($query)){
      echo $row['id']."<br />";
}
On page one, I get what I should...or close to. It echoes "FROM: 0 TO: 224" and outputs 1-224. Fine.

On page 2, I get "FROM: 225 TO: 449" which is what it should be getting, but it outputs 226-674. It goes up from there.

I can't see the error, or a solution, maybe you can Sami or someone else can.

Please and thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

limit syntax: LIMIT start, length
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Ooooooh.... thanks feyd. There's one problem down. :D

Now, I need to be able to split the results into 3 columns... It seems fairly easy in theory, but ... I can't get it. Another google search came up with this:

Code: Select all

for ($ctr = 0; $SQL_Array = mysql_fetch_array($SQL_Result); $ctr++) {

    if (($ctr % 2) == 0) echo('<tr>');

    echo('<td>');

    echo($SQL_Array['keywords'].'<br>');

    echo('<a href="'.$SQL_Array['url'].'"><img src="'.$SQL_Array['thumb_url'].'" border="0"></a>');

    echo('</td>');

    if (($ctr % 2) == 1) echo('</tr>');

}
But it doesn't work with the $SQL_Array parameter there. I can't seem to figure out the way to combine the while($row=...) within the for loop. *sigh*

I think it's close. All I need to do is have the words cut off at a certain point. It's math, but what is it?! ARRRHGGHH!! heh... With 225 results per page, and 75 per column, it should be easy to cut up. I'm having one heck of a time.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

for starters, you're only making 2 columns.

viewtopic.php?t=25105
viewtopic.php?t=23442
viewtopic.php?t=23418
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Yeah, I know that script is. I tried jimmying it but couldn't get it.

According to your first link there (all helpful btw.. :D), this is what I altered it to look like:

Code: Select all

$limit = 200;
            
            //$countQuery = mysql_query("SELECT COUNT(*) AS totalNum FROM {$_GET['letter']}");
            //$count = mysql_fetch_assoc($countQuery);
            //$total = $count['totalNum'];
            //$totalPages = $total/$limit;
            
            $from = ($page-1)*$limit;
            //$to = ($page*$limit)-1;
            $output = "<table width='100%'>\n<tr>\n";
            $query = mysql_query("SELECT word, id FROM {$_GET['letter']} LIMIT $from, $limit");
            $howmany = mysql_num_rows($query);
            $rowmax = 3;
            for($x = 0; $row = mysql_fetch_array($query);$x++){
                if($x % $rowmax == 0){
                    $output .= "<tr>\n";
                    $output .= "<td width='33%'>".$row['id'].":  ".$row['word']."<br /></td>\n</tr>\n";
                    if($x % $rowmax == -1){
                        $output .= "</tr>\n";
                    }
                }
            }
           if($left = (($howmany + $rowmax - 1) % $rowmax))
           $output .= '<td colspan="' . $left . '">&nbsp' . ";</td>\n</tr>\n\n";

           $output .= "</table>\n\n";
            
           echo $output;
But it's still only one column. Excuse my ignorance... I just can't get this thing!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try this:

Code: Select all

$limit = 200;
            //$countQuery = mysql_query("SELECT COUNT(*) AS totalNum FROM {$_GET['letter']}");
            //$count = mysql_fetch_assoc($countQuery);
            //$total = $count['totalNum'];
            //$totalPages = $total/$limit;
            
            $from = ($page-1)*$limit;
            //$to = ($page*$limit)-1;
            $output = "<table width='100%'>\n";
            $query = mysql_query("SELECT word, id FROM {$_GET['letter']} LIMIT $from, $limit");
            $howmany = mysql_num_rows($query);
            $rowmax = 3;
            for($x = 0; $row = mysql_fetch_array($query);$x++){
                if($x % $rowmax == 0){
                    $output .= "<tr>\n";
                    $output .= "<td>".$row['id'].":  ".$row['word']."<br /></td>\n</tr>\n";
                    if($x % $rowmax == $rowmax - 1){
                        $output .= "</tr>\n";
                    }
                }
            }
           if($left = (($howmany + $rowmax - 1) % $rowmax))
           $output .= '<td' . ($left > 1 ? ' colspan="' . $left . '"' : '') . '>&nbsp' . ";</td>\n</tr>\n\n";

           $output .= "</table>\n\n";
            
           echo $output;
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Hmm, didn't work for me...did it work for you?

*30 minutes later*

Victory! Thanks for the push feyd. Here's the code that worked for me:

Code: Select all

$limit = 225;
            //$countQuery = mysql_query("SELECT COUNT(*) AS totalNum FROM {$_GET['letter']}");
            //$count = mysql_fetch_assoc($countQuery);
            //$total = $count['totalNum'];
            //$totalPages = $total/$limit;
           
            $from = ($page-1)*$limit;
            //$to = ($page*$limit)-1;
            echo "<table width='100%'>\n<tr valign='top'>\n<td width='33%'>\n";
            $query = mysql_query("SELECT word, id FROM {$_GET['letter']} LIMIT $from, $limit");
            $howmany = mysql_num_rows($query);
            $rowmax = 3;
            for($x = 0; $row = mysql_fetch_array($query);$x++){
                echo $row['id'].":  ".$row['word']."    "."<br />\n";
                if($x % 75 == 74){
                    echo "</td>\n<td width='33%'>\n";
                }
            }
            echo "</tr></table>";
Thanks again for stickin to it :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

actually, now that I looked at it more closely, this should work...:

Code: Select all

$limit = 200;
            //$countQuery = mysql_query("SELECT COUNT(*) AS totalNum FROM {$_GET['letter']}");
            //$count = mysql_fetch_assoc($countQuery);
            //$total = $count['totalNum'];
            //$totalPages = $total/$limit;
            
            $from = ($page-1)*$limit;
            //$to = ($page*$limit)-1;
            $output = "<table width='100%'>\n";
            $query = mysql_query("SELECT word, id FROM {$_GET['letter']} LIMIT $from, $limit");
            $howmany = mysql_num_rows($query);
            $rowmax = 3;
            for($x = 0; $row = mysql_fetch_array($query);$x++){
                if($x % $rowmax == 0){
                    $output .= "<tr>\n";
                }
                $output .= "<td>".$row['id'].":  ".$row['word']."<br /></td>\n";
                if($x % $rowmax == $rowmax - 1){
                    $output .= "</tr>\n";
                }
            }
           if($left = (($howmany + $rowmax - 1) % $rowmax))
           $output .= '<td' . ($left > 1 ? ' colspan="' . $left . '"' : '') . '>&nbsp' . ";</td>\n</tr>\n\n";

           $output .= "</table>\n\n";
            
           echo $output;
Post Reply