Christine Austin
Christine Austin

Reputation: 93

Previous/next Buttons?

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.

<?php 
$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>
  </select>
  <input type="submit" value="view" />
</form>

<div id="view">
 <center><img src="images/forum.png" width="589" height="97"></center>
</div>  
  <div id="white">
    <div id="blue">
      <div id="grey">
       <div id="container">
<?php
$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 />"; 
}

?>
</div>
</div>
</div>
</div>

Upvotes: 1

Views: 8542

Answers (2)

dqhendricks
dqhendricks

Reputation: 19251

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 ?>

Upvotes: 2

Exos
Exos

Reputation: 3988

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

-- Fist 10 results:
SELECT * FROM stories ORDER BY id DESC TOP 10 

-- 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";  

Upvotes: 0

Related Questions