0

I'm trying to use a "previous" and "next" button to return one record at a time ordered by ID. I'm having some issues with the code though.

<?php 
include ('header.php');
include ('connection.php');
//pagination starts here
if(isset($_GET['page'])){
$page = preg_replace('#[^0-9]#','',$_GET['page']);
}else{
$page = 1;
}
$perPage = 1;
$lastPage = cell($count / $perPage);

if($page < 1){
$page = 1;
}else if($page > $lastPage){
$page = $lastPage;
}

$limit = 'LIMIT ' .($page -1)*$perPage .',$perPage';

$query = mysql_query('SELECT LastName FROM residents ORDER BY ID DESC $limit');

if($lastPage != 1){

if($page != $lastPage){
    $next = $page + 1;
    $pagination.='<a href="AutoIncrement.php?page='.$next.">Next</a>';
}
//error occurs here
if($page != 1){
    $prev = $page - 1;
    $pagination.='<a href="AutoIncrement.php?page='.$prev.">Previous</a>';
}
}
?>

It all seems to be ok except I keep getting an error for the last IF statement. Any thoughts?

IGrowBeards
  • 75
  • 13
  • Your query is right! What realy is your question?! – Elias Soares Sep 26 '14 at 16:01
  • Sound like you want someone to write some code for you. Have you tried anything? Can you share it with us? – Jay Blanchard Sep 26 '14 at 16:02
  • 1
    This can be an application of *Paging*: [MySql Data - Best way to implement paging?](http://stackoverflow.com/questions/3799193/mysql-data-best-way-to-implement-paging) – Alex K. Sep 26 '14 at 16:03
  • So, what's the question? Are you asking for a button code? – Funk Forty Niner Sep 26 '14 at 16:08
  • Actually it may just be a paging issue now that you mention it. My original query works fine. It returns one entry in alphabetical order. The issue I'm having is being able to return the next line of data in alphabetical order. For example, the query returns "John Allen". Once the user uses the button then it returns the next line of data, "Davon Bruno" and so on in ascending order. – IGrowBeards Sep 26 '14 at 16:43

3 Answers3

2

I've finally figured out what the problem was. Here is what I came up with:

<?php
//browsing feature of database
include ('connection.php');  
include ('header.php');
$count_query = mysql_query("SELECT NULL FROM residents");
$count = mysql_num_rows($count_query);

//pagination starts here
if(isset($_GET['page'])){
//$page = preg_replace("##[^0-9]","",$_GET['page']);
$page = $_GET['page'];
//echo "Page is ".$page;
}else{
$page = 1;
//echo "Page is one";
}
$perPage = 1;
$lastPage = ceil($count/$perPage);
if($page <1){
$page = 1;
}else if($page > $lastPage){
$page = $lastPage;
}

$limit = "LIMIT " .($page-1) * $perPage .", $perPage";

$query = mysql_query("SELECT * FROM residents ORDER BY ID ASC $limit");
if($lastPage != 1){


if($page != $lastPage){
    $next = $page + 1;
    $pagination.='<nav><ul class="pager"><li><a href=Browse.php?page='.$next.'>Next</a>          </li></ul><nav>';
}
 if($page != 1){
    $prev = $page - 1;
    $pagination.='<nav><ul class="pager"><li><a href=Browse.php?    page='.$prev.'>Previous</a></li></ul></nav>';
} 
}


while($row = mysql_fetch_array($query)){
$output.='<p><img src="upload/' . $row['Picture'].'" width="70" height="70"/></p>';
$output.= $row['FirstName']. '<br />';
$output.= $row['LastName']. '<br />';
$output.= $row['Address']. '<br />';
$output.= $row['Birthday']. '<br />';
$output.= $row['FormerResidence']. '<br />';
$output.= $row['Career']. '<br />';
$output.= $row['Education']. '<br />';
$output.= $row['SpecialInterests']. '<br />';


}
?>






<?php
    /*while($person = mysql_fetch_array($result)) {
        echo "<h3>" . $person['FirstName'] . "</h3>";
        echo "<p>" . $person['LastName'] . "</p>"; 
        echo "<p>" . $person['Address'] . "</p>";
        echo "<p>" . $person['Birthday'] . "</p>"; 
        echo "<p>" . $person['FormerResidence'] . "</p>";
        echo "<p>" . $person['Career'] . "</p>";
        echo "<p>" . $person['Education'] . "</p>";
        echo "<p>" . $person['SpecialInterests'] . "</p>";
        echo '<p><img src="upload/' . $person['Picture'].'" width="70" height="70"/>      </p>';
    }*/
?>
<h1>Browse Residents</h1>
<!-- Comment start
        <a href="nextandprevious.php?page='.$previous.">Previous</a>
        <a href="nextandprevious.php?page='.$next.">Next</a>
             Comment end -->

        <?php echo $output;?>
        <?php echo $pagination;?>
Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107
IGrowBeards
  • 75
  • 13
0

Limit and offset can be used to get specific record, but it will be way too much overhead. Instead fetch entire table data in a variable pass it on to javascript and iterate within javascript.

Edit 1: i deliberately haven't provided any code samples so you can give a try at it yourself.

Abhi
  • 724
  • 1
  • 6
  • 15
0

You can then after use query like, SELECT * FROM table ORDER BY LastName ASC Limit 1,1. (For next) and then after changing limit to 2,1 and so on.

bear
  • 11,364
  • 26
  • 77
  • 129
Bhupender Keswani
  • 1,218
  • 1
  • 9
  • 16