Jump to content

Paging is not working.


bryanptcs

Recommended Posts

I have followed the php tutorial on how to make pages from your results pulled from a mysql db and I am not getting it to work.  The limit is working, but the links that it is supposed to make to go to next and previous pages do not ever get created what is wrong?

[code]<form class="form" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

    <p><label for="txtName">Name:</label><br />
    <input type="text" title="Enter your name" name="txtName" /></p>

<p><label for="rate">How would you rate us:</label><br />
  <input type="radio" name="rate" value="1">
1(poor)
<input type="radio" name="rate" value="2">
2 <input type="radio" name="rate" value="3">
3 <input type="radio" name="rate" value="4">
4 <input type="radio" name="rate" value="5">
5(excellent)<br>
</p>

    <p><label for="txtMessage">Your message:</label><br />
    <textarea title="Enter your message" name="txtMessage"></textarea></p>
   
    <p><label title="Send your message">
    <input type="submit" value="Send" />
   
    </label></p><br />
<hr>
   
</form><?php

/**
* Create the table in your MySQL database:
*
* CREATE TABLE guests (
*  id int(10) NOT NULL auto_increment,
*  name varchar(50) NOT NULL,
*  message varchar(255) NOT NULL,
*  date timestamp(14) NOT NULL,
*  PRIMARY KEY (id)
* )
*
* Change the database login settings to your own
*
* The script is now ready to run
*/

// Change these to your own database settings
$host = "localhost";
$user = "";
$pass =" "
$db = "";

mysql_connect($host, $user, $pass) OR die ("Could not connect to the server.");
mysql_select_db($db) OR die("Could not connect to the database.");
   
// If current page number, use it
// if not, set one!

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 5;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
 
$name = stripslashes($_POST['txtName']);
$message = stripslashes($_POST['txtMessage']);
$rate = stripslashes($_POST['rate']);

if (!isset($_POST['txtName'])) {
    $query = "SELECT id, name, rate, message, DATE_FORMAT(date, '%D %M, %Y at %H:%i') as newdate FROM guests ORDER BY id DESC LIMIT $from, $max_results";
    $result = mysql_query($query);
   
    while ($row = mysql_fetch_object($result)) {

?>

<p class="infotext"><?php echo $row->message; ?><br />
Rating: <?php echo $row->rate; ?> </strong>
<br />
<span class="form2">Posted by <?php echo $row->name; ?> on <?php echo $row->newdate; ?></span></p>

<?php
       
    }


   
?>





<?php

}

else {

    // Adds the new entry to the database
    $query = "INSERT INTO guests SET message='$message', name='$name', date=NOW(), rate='$rate'";
    $result = mysql_query($query);

    // Takes us back to the entries
    $ref = $_SERVER['HTTP_REFERER'];
    header ("Location: $ref");
}

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM guests"),0);

// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}
// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}

echo "</center>";

?> [/code]
Link to comment
https://forums.phpfreaks.com/topic/27273-paging-is-not-working/
Share on other sites

[quote author=printf link=topic=115000.msg468095#msg468095 date=1163542325]
this...

[code]$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM guests"),0);[/code]


should be...

[code]$total_pages = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM guests"),0);[/code]



[/quote]

I did that, now it puts the pages at the bottom.  The problem is that there is only 2 pages worth of content, but it puts 8 pages on the list.  Only 2 pages actually have stuff, and the other 6 are just blank pages.  Why is it doing this?
Link to comment
https://forums.phpfreaks.com/topic/27273-paging-is-not-working/#findComment-124999
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.