¿Botones anterior/siguiente?

I wanna input previous and next buttons for a forum that i have because after the forum gets to long, it surpasses the background image and doesn't look good! Does anyone know either a simple way to do it or something i can look at for previous and next buttons? I want it so each page is limited to only 5 posts per page or something.

Heres the code if it helps at all. I'm sorry if this is a stupid question.

$dbc=mysql_connect('host','user','password','database') or die(mysql_error());
mysql_select_db('database',$dbc) or die(mysql_error());

View Posts <br>
<form method="get" action="view_forum.php">
  <label>Select Weather to Filter </label><br />
  <select name="weather">
    <option value="all">all</option>
    <option value="cloudy">Cloudy</option>
    <option value="sunny">Sunny</option>
    <option value="windy">Windy</option>
    <option value="snowy">Snowy</option>
    <option value="mixy">Wintery Mix</option>
    <option value="rainy">Rainy</option>
  <input type="submit" value="view" />

<div id="view">
 <center><img src="images/forum.png" width="589" height="97"></center>
  <div id="white">
    <div id="blue">
      <div id="grey">
       <div id="container">
$weather = mysql_real_escape_string( $_GET["weather"] ); // keep your input clean

if ( $weather == "all" ) {
  $sql = "SELECT * FROM stories ORDER BY id DESC";
} else {
  $sql = "SELECT * FROM stories WHERE weather = '$weather' ORDER BY id DESC";

$result = mysql_query( $sql ) or die( mysql_error() );

while ( $row = mysql_fetch_assoc( $result ) ) {

    echo "<div class=\"names\"> {$row['name']}<br /></div>";
    echo "<div class=\"weathers\">{$row['weather']}<br /></div>";
   echo "<div class=\"stories\">{$row['story']}<br /></div>";

        echo "<img src=\"images/line.png\" width='800' height='3'>";

         echo "<br />"; 


preguntado el 03 de mayo de 12 a las 17:05

2 Respuestas

It's easy. You keep a page variable in the request. As shown below

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

And in you SQL statement, you would put something like this, which uses the page variable to adjust the query limits:

$query = 'SELECT * FROM someTable WHERE 1 LIMIT ' . (($page - 1) * $recordsPerPage) . ' ' . $recordsPerPage;

Something like that anyways. Now for your Previous and Next links you put something like this, so that you can increment/decrement the page variable:

<? if ($page > 1) : ?>
   <a href="self.php?page=<?= $page - 1 ?>">Prev</a>
<? endif ?>
<? if ($page != $maxPages) : ?>
   <a href="self.php?page=<?= $page + 1 ?>">Next</a>
<? endif ?>

Respondido el 25 de Septiembre de 18 a las 16:09

I'm having trouble where i should place this all in the code. Sorry...this php stuff gives me trouble! - cristina austin

@ChristineAustin This is not meant to be a cut and paste site. You should try to understand what is going on in the code first, and then implement it. If you don't know where to put the above items, that leads me to believe that you don't understand what they do, which can be very dangerous for a web engineer and their server. Go through each, and if you don't understand a piece of what they are doing, then research until you do. - dqhendricks

You are right, I wont argue against it. Since I am new to this I'll try to break it down. Thanks for your help. - cristina austin

You need to paginate, in MySQL you can use TOP:

-- Fist 10 results:

-- 10 results from the number 11 (0 is the first)
SELECT * FROM stories ORDER BY id DESC TOP 10,10

-- 10 results from the number 50
SELECT * FROM stories ORDER BY id DESC TOP 50,10

For know the total result you can use SQL_CALC_FOUND_ROWS:

SELECT SQL_CALC_FOUND_ROWS as total, stories.* FROM stories ORDER BY id DESC TOP 10 

From PHP, you can calculate the pagination:

$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;

if ($page < 1) $page = 1;

$sql = "SELECT SQL_CALC_FOUND_ROWS as total, stories.* FROM stories ORDER BY id DESC TOP " . ($page-1) .  ",10";  

contestado el 03 de mayo de 12 a las 18:05

@JeffPigarelli MySQL doesn't actually understand "TOP". Instead you have to use "LIMIT". - dqhendricks

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.