Note that there are some explanatory texts on larger screens.

plurals
  1. POMYSQL query with PHP pagination
    text
    copied!<p>I am trying to write a script that pulls a specific query from a MYSQL database and then paginates the results.</p> <p>I think I know what the solutions are its just a question of my lack of knowledge on how to implement / script them.</p> <p>Here is my script that is partially working :</p> <pre><code>&lt;?php $tstart = 0; $tend = 0; //$tpage = 0; // the start page //$tpages = 0; // number of pages $tpagelinks = ""; // stores the output pagination $ttotal = 0; / total number of results $trows = 5; // number of results per page $online = ""; // stores the results to display $dbhost = "localhost"; $dbuser = "****_models"; $dbpass = "****"; $dbcon = @mysql_connect($dbhost,$dbuser,$dbpass) or die('Database error: ' . mysql_error()); $db = mysql_select_db('****_cammodels', $dbcon) or die('Database error: ' . mysql_error()); $query = "SELECT * FROM cbmodels WHERE gender='f' AND status='Public' AND age&lt;='22'"; $result = mysql_query($query) or die('Query failed: ' . mysql_error() . "\nQuery: $query"); while($row = mysql_fetch_array($result)) { $online .= "#". $ttotal . " \n"; $online .= $row['status'] . "/" . $row['name'] . "/" . $row['gender'] . "/" . $row['age'] . "\n"; $online .= "&lt;br&gt;\n"; $ttotal ++; } $tstart = ($tpage * $trows) - $trows; $tend = $tstart + $trows; if ($tend &gt; $ttotal) { $tend = $ttotal; } $tpages = floor($ttotal / $trows); if ($ttotal % $trows != 0) { $tpages ++; } if ($tpage &gt; $tpages) { $tpage = 1; } if ($tpage &gt; 1) { $tpagelinks .= "&lt;a class='allonlinepages_url' href='new_from_db.php?page=" . ($tpage - 1) . "'&gt;PREVIOUS&lt;/a&gt;\n"; } for ($ti = 1; $ti &lt;= $tpages; $ti ++) { if ($tpage == $ti) { $tpagelinks .= "$ti \n"; } else { $tpagelinks .= "&lt;a class='allonlinepages_url' href='new_from_db.php?page=$ti' &gt;$ti&lt;/a&gt;\n"; } } if ($tpage &lt; $tpages) { $tpagelinks .= "&lt;a class='allonlinepages_url' href='new_from_db.php?page=" . ($tpage + 1) . "' &gt;NEXT&lt;/a&gt;\n"; } echo $online; echo $tpagelinks; mysql_close($dbcon); ?&gt; </code></pre> <p>What it is doing is showing all the results found in the database, the pagination count seems to be showing the correct number of pages but the links are wrong / do not show the next page of results.</p> <p>I think I need to query the database to get a row count for the pagination first and then calculate the number of pages, then do another query to obtain the results maybe store that in another array as results per page ? or something to that effect. I have tried many times and the script above is the closest I have gotten to get it almost working all my other attempts just break the script and either produce strange results or none at all.</p> <p>The script I made to pull the data from the actual feed to store in the database itself flagging online and offline works correctly so I have not included that since it is a separate script anyway.</p> <p>Thank you in advance for any help given Guys. </p> <p><em>EDIT</em> Removed all the none working code RE: Comments.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload