Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically Generating Page Numbers
    primarykey
    data
    text
    <p>I currently have an index.php page which serves as an index for a "My Bookmarks" page. The page returns the results of a MySQL Database Query, into a table, which is automatically generated. The code is only supposed to allow five records per page, before generating an additional page (thus inserting page numbers/links at the bottom) - For some reason, my code just stopped working/producing the page numbers.</p> <p>What is wrong with my code? I can't seem to find the problem and I don't know what to search on the forum; any guidance is much appreciated.</p> <p>Here is my index.php code:</p> <pre><code> &lt;?php session_start(); //check session first if (!isset($_SESSION['email'])){ echo "You are not logged in!"; exit(); }else{ //include the header include ("../includes/header.php"); require_once ('../../mysql_connect.php'); echo ("&lt;center&gt;"); echo ("&lt;div class='bookmarkMenu'&gt;&lt;h1 style='text-decoration:underline;'&gt;Q &amp; A Database&lt;/h2&gt;&lt;p&gt;"); echo ("&lt;p&gt;&lt;a class='bookmarkAdd' href=add.php&gt;Add Record&lt;/a&gt; "); echo ("&lt;a class='bookmarkSearch' href=searchform.php&gt;Search Records&lt;/a&gt;&lt;/p&gt;&lt;hr /&gt;&lt;/div&gt;&lt;br /&gt;"); //Set the number of records to display per page $display = 5; //Check if the number of required pages has been determined if(isset($_GET['p'])&amp;&amp;is_numeric($_GET['p'])){//Already been determined $pages = $_GET['p']; }else{//Need to determine //Count the number of records; $query = "SELECT COUNT(ID) FROM bookmark"; $result = @mysql_query($query); $row = @mysql_fetch_array($result, MYSQL_NUM); $records = $row[0]; //get the number of records //Calculate the number of pages ... if($records &gt; $display){//More than 1 page is needed $pages = ceil($records/$display); }else{ $pages = 1; } }// End of p IF. //Determine where in the database to start returning results ... if(isset($_GET['s'])&amp;&amp;is_numeric($_GET['s'])){ $start = $_GET['s']; }else{ $start = 0; } //Make the paginated query; $query = "SELECT * FROM answers LIMIT $start, $display"; $result = @mysql_query ($query); //Table header: echo "&lt;table class='bookmarksTable' cellpadding=5 cellspacing=5 border=1&gt;&lt;tr&gt; &lt;th&gt;ID&lt;/th&gt;&lt;th&gt;Question&lt;/th&gt;&lt;th&gt;Answer&lt;/th&gt;&lt;th&gt;Comment&lt;/th&gt;&lt;th&gt;*&lt;/th&gt;&lt;th&gt;*&lt;/th&gt;&lt;/tr&gt;"; //Fetch and print all the records... while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "&lt;tr&gt;&lt;td class='bookmarkInfo'&gt;".$row['ID']."&lt;/td&gt;"; echo "&lt;td class='bookmarkInfo'&gt;".$row['Question']."&lt;/td&gt;"; echo "&lt;td class='bookmarkInfo'&gt;".$row['Answer']."&lt;/td&gt;"; echo "&lt;td class='bookmarkInfo'&gt;".$row['Comment']."&lt;/td&gt;"; echo "&lt;td class='bookmarkInfo'&gt;&lt;a href=deleteconfirm.php?id=".$row['ID']."&gt;Delete&lt;/a&gt;&lt;/td&gt;"; echo "&lt;td class='bookmarkInfo'&gt;&lt;a href=updateform.php?id=".$row['ID']."&gt;Update&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;"; } // End of While statement echo "&lt;/table&gt;"; mysql_free_result ($result); // Free up the resources. mysql_close(); // Close the database connection. //Make the links to other pages if necessary. if($pages&gt;1){ echo '&lt;br/&gt;&lt;table&gt;&lt;tr&gt;'; //Determine what page the script is on: $current_page = ($start/$display) + 1; //If it is not the first page, make a Previous button: if($current_page != 1){ echo '&lt;td&gt;&lt;a href="index.php?s='. ($start - $display) . '&amp;p=' . $pages. '"&gt; Previous &lt;/a&gt;&lt;/td&gt;'; } //Make all the numbered pages: for($i = 1; $i &lt;= $pages; $i++){ if($i != $current_page){ // if not the current pages, generates links to that page echo '&lt;td&gt;&lt;a href="index.php?s='. (($display*($i-1))). '&amp;p=' . $pages .'"&gt; ' . $i . ' &lt;/a&gt;&lt;/td&gt;'; }else{ // if current page, print the page number echo '&lt;td&gt;'. $i. '&lt;/td&gt;'; } } //End of FOR loop //If it is not the last page, make a Next button: if($current_page != $pages){ echo '&lt;td&gt;&lt;a href="index.php?s=' .($start + $display). '&amp;p='. $pages. '"&gt; Next &lt;/a&gt;&lt;/td&gt;'; } echo '&lt;/tr&gt;&lt;/table&gt;'; //Close the table. }//End of pages links //include the footer include ("../includes/footer.php"); } ?&gt; </code></pre> <p>Much thanks, -Rockmandew</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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