strago Posted April 23, 2013 Share Posted April 23, 2013 I think it's the code the generates the 1, 2, 3.... links that's messed up. If I have say 50 results and have it set up to show up to 100 results per page, the 1 still shows up at the bottom of the page where it links to the results. If I have it 50 per page and have 200 results it'll show 1 2 3 4 and at 4 it hasn't gone through them all, you can replace the number up to 10 before you get to the end, generating 462 results, when there are actually just 200 results. The results are showing up two or three times!! Part probably messed up... <?php $query = "SELECT COUNT(*) FROM database"; $result = mysql_query($query); $row = mysql_fetch_row($result); $total_records = $row[0]; $total_pages = ceil($total_records / 50); for ($i=1; $i<=$total_pages; $i++) { echo "<a href='sort.php?order=$order&page=".$i."'>".$i."</a> "; }; ?> The complete script... <?php /* set the allowed order by columns */ $default_sort = 'Title'; $allowed_order = array ('Title', 'Description','URL'); /* if order is not set, or it is not in the allowed * list, then set it to a default value. Otherwise, * set it to what was passed in. */ if (!isset ($_GET['order']) || !in_array ($_GET['order'], $allowed_order)) { $order = $default_sort; } else { $order = $_GET['order']; } /* connect to db */ mysql_connect ('localhost','root', 'password'); mysql_select_db ('database'); /* construct and run our query */ if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = ($page-1) * 20; $query = "SELECT * FROM database ORDER BY $order ASC LIMIT $start_from, 50"; $result = mysql_query ($query); /* make sure data was retrieved */ $numrows = mysql_num_rows($result); if ($numrows == 0) { echo "No data to display!"; exit; } /* now grab the first row and start the table */ $row = mysql_fetch_assoc ($result); echo "<TABLE border=1>\n"; echo "<TR>\n"; foreach ($row as $heading=>$column) { /* check if the heading is in our allowed_order * array. If it is, hyperlink it so that we can * order by this column */ echo "<TD><b>"; if (in_array ($heading, $allowed_order)) { echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>"; } else { echo $heading; } echo "</b></TD>\n"; } echo "</TR>\n"; /* reset the $result set back to the first row and * display the data */ mysql_data_seek ($result, 0); while ($row = mysql_fetch_assoc ($result)) { echo "<TR>\n"; foreach ($row as $column) { echo "<TD>$column</TD>\n"; } echo "</TR>\n"; } echo "</TABLE>\n"; ?> <?php $query = "SELECT COUNT(*) FROM database"; $result = mysql_query($query); $row = mysql_fetch_row($result); $total_records = $row[0]; $total_pages = ceil($total_records / 50); for ($i=1; $i<=$total_pages; $i++) { echo "<a href='sort.php?order=$order&page=".$i."'>".$i."</a> "; }; ?> I think the part generating the pages doesn't know about the rest of the script, and probably needs to be merged in with the main script code, and geting the results to only show up once per result. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 23, 2013 Share Posted April 23, 2013 the number of rows you display per page should be a variable, so that it will be the same value in the three places you are using it. your code is using 20 in one place, and 50 in the other two places. 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.