djfox Posted May 17, 2008 Share Posted May 17, 2008 For whatever reason, it seems as though my code can`t count. I`ve set the offset to be at 50, and it is displaying every single item from the table rather than just the 50. I`ve copied and pasted the code form another file which uses the same system and works successfully, but it still seems to have trouble counting on this file I`m trying to get this to work for. <a href="mlpcollection_main.php?g=<? echo "$g"; ?>&sort=newest">Newest</a> | <a href="mlpcollection_main.php?g=<? echo "$g"; ?>&sort=alpha">Alphabetical</a> <p> <?php if (isset($_GET['sort']) && $_GET['sort'] == "newest") { if(!$offset) $offset=0; $recent = 0; $res = mysql_query("SELECT id, image, date, date2, name, descr, type, gen, cond, owner FROM ponycol WHERE owner='$g' ORDER BY id DESC")or die( mysql_error() ); while( $row = mysql_fetch_row($res) ){ if( $recent >= $offset && $recent < ($offset + 50 )){ echo "<a href='mlpcollection.php?id=$row[0]'>$row[4]</a><br>"; } } $recent = $recent + 1; } ?> <p> <table border=0 width=100%> <tr> <td><?php if ($offset >= 50) { ?><b><a href="mlpcollection_main.php?g=<? echo $g ?>&sort=<? echo "$sort"; ?>&offset=<? echo $offset - 50 ?>"><img src="<? echo $butprev ?>" border=0></a></b> <?php } ?> <td><div align=right> <b><a href="mlpcollection_main.php?g=<? echo $g ?>&sort=<? echo "$sort"; ?>&offset=<? echo $offset + 50 ?>"><img src="<? echo $butnext ?>" border=0></a></b> </div> </table> The Next/Previous counts show up properly, they add/subtract the correct number from the correct field. Break-down: Instead of showing just 50 items, it shows every single item in the table. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 17, 2008 Share Posted May 17, 2008 Two things. 1) A better way to limit your results to 50 is to use "LIMIT 50" in your query! Then ther is no need to synthetically limit the results in the PHP code. 2) If you break down the relevent code it shoud be obvious why it is not working as you intend <?php //$offset is not already set so it will be set to 0 if(!$offset) $offset=0; //$recent is set to 0 $recent = 0; $res = mysql_query("SELECT id, image, date, date2, name, descr, type, gen, cond, owner FROM ponycol WHERE owner='$g' ORDER BY id DESC")or die( mysql_error() ); while( $row = mysql_fetch_row($res) ){ //$recent and $offeset never get modified (in the loop) so this will always be true if( $recent >= $offset && $recent < ($offset + 50 )){ echo "<a href='mlpcollection.php?id=$row[0]'>$row[4]</a><br>"; } } //This is outside the loop $recent = $recent + 1; Quote Link to comment Share on other sites More sharing options...
djfox Posted May 17, 2008 Author Share Posted May 17, 2008 To see the page this is being done to: http://secrettrance.net/mlpcollection_main.php?g=97&sort=newest 2) If you break down the relevent code it shoud be obvious why it is not working as you intend My first post: I`ve copied and pasted the code form another file which uses the same system and works successfully This copied and pasted code from another file works on the other file. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 17, 2008 Share Posted May 17, 2008 It`s still showing every single item in the table. To see the page this is being done to: http://secrettrance.net/mlpcollection_main.php?g=97&sort=newest I didn't supply "corrected" code. I added comments to show why it is not working as you intended. Here is a better way to do this: <?php if (isset($_GET['sort']) && $_GET['sort'] == "newest") { $query = "SELECT id, image, date, date2, name, descr, type, gen, cond, owner FROM ponycol WHERE owner='$g' LIMIT 50 ORDER BY id DESC"; $res = mysql_query($query)or die( mysql_error() ); while( $row = mysql_fetch_row($res) ){ echo "<a href='mlpcollection.php?id=$row[0]'>$row[4]</a><br>"; } } ?> Quote Link to comment Share on other sites More sharing options...
djfox Posted May 17, 2008 Author Share Posted May 17, 2008 If you LIMIT 50, you can`t "browse" through the list then, can you? In your code, I see no way for a viewer to "browse" through the list. Btw, your code gave an error message. Quote Link to comment Share on other sites More sharing options...
corbin Posted May 17, 2008 Share Posted May 17, 2008 Well, the full syntax for limit is: LIMIT start, rows In other words, LIMIT 20, 5, would show 20 rows (if they exist) starting from row 5. Knowing that, you could make the 20 dynamic. For example, you could have, ?p=1 on your URL to indicate page 1. Example: $per_page = 25; $page = (isset($_GET['page']) && ctype_digit($_GET['page']) && $_GET['page'] > 0) ? $_GET['page'] : 1; //you would want page 1 to start from 0, page 2 to start from $per_page and so on.... So, you would do: $l = ($page-1)*$per_oage; $query = mysql_query("SELECT something FROM sometable LIMIT {$l}, $per_page ORDER BY some_other_column_or_something"); //if rows exist, show them and what not //if not, say no rows exist //echo links for the previous/next page //(do a check to make sure there is a previous/next page of course) Quote Link to comment Share on other sites More sharing options...
djfox Posted May 17, 2008 Author Share Posted May 17, 2008 Never mind, I think I got it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.