Jump to content

Error in pagination


jam123

Recommended Posts

Dear all experts,

 

I am really stucked with below coding. Below is the Code with paging.when executed the fist page result is shown correctly with all page numbers. But 2nd,3rd,4th etc. page results are not shown when clicked. Please help.. :'(

 

<?php
@session_start();
if(!isset($_SESSION['SESSION'])) {
    require ("sess.php");
}
if (isset($_GET['page'])) { $page  = $_GET['page']; }
else { $page=1;};
echo 'page'.$page.'<br><br>';
?>
<?php
$raw_rst = "";

$page_limit = 2;
$start_page = ($page-1)*$page_limit;

$record_id = $_POST['record_id']; ;
$file_number =  $_POST['file_number'];
$name_land =  $_POST['name_land'];
$location = $_POST['location'] ;


@$connect = mysql_connect($_SESSION['server'],$_SESSION['un'],$_SESSION['pass'])
    or die ('Error : Unable to connect to the MYSQL Server');;
@mysql_select_db($_SESSION['db']) or die ('Error : Unable to connect to the Data Base');

$query = "select Record_id,file_number,Land Name from TABLE
  	where file_number = '$file_number' ||
	Record_id = '$record_id'     ||
	land_name = '$name_land'     ||
	location = '$location'  
	ORDER BY Record_id ASC LIMIT $start_page,$page_limit";

$query1 = @mysql_query($query) or die (mysql_error());
?>
<table border="1">
    <thead>
        <tr>
            <th>Record ID</th>
            <th>file_number</th>
            <th>Land Name</th>

        </tr>
    </thead>
    <tbody>
        <?php
        while ($rw = mysql_fetch_array($query1)) {
            $k = $rw['Record_id'];
            echo '<tr>';
            echo '<td>'.$rw['Record_id'].'</td>';
            echo '<td>'.$rw['file_number'].'</td>';
            echo '<td>'.$rw['land_name'].'</td>';
            echo' <td>';
            echo'<form action="dlt_grid.php" method = "GET">';
            echo'<input type="submit" value="Delete">';
            echo'<input type="hidden" name="hf" value="'.$k.'">';
            echo'</form> ';
            echo'</td>';
            echo '</tr>';
        };
        

$page_query = "select COUNT(Record_id)
FROM land_general
WHERE Record_id = '$record_id'
|| file_number = '$file_number'
|| land_name = '$name_land'
|| location = '$location' ";

        $page_qry = @mysql_query($page_query) or die (mysql_error());
        $page_row = mysql_fetch_array($page_qry) or die (mysql_error());
        $total_records = $page_row[0];
        echo 'tot rec'.$total_records.'<br><br>';
        $total_pages = ceil($total_records / $page_limit);
        for($i=1;$i<=$total_pages;$i++) {
            echo"<a href='searching.php?page=".$i."'>".$i."</a>".' '.' ';
        };
        ?>
    </tbody>
</table>

Link to comment
Share on other sites

I'm too lazy right now to check every line of code right now. But, I do see you are using POST & GET values in that page. I suspect that what is happening is that you have a form that is used to first access the page. So, the first load of the page has those POST values that are used in the query. But, when you click one of the links to access another page the POST values do not exist. Therefore your query is not returning any results.

 

You will need to save the POST values in session or cookie values on the first page load so you can use them on subsequent loads for the other pages.

Link to comment
Share on other sites

Thank you  mjdamato,

 

What you have mentioned is purely correct.

I reset all the request to GET methods (I believe this is where I went wrong using GET/POST methods both together in the page) & saved it in a session (which I had already done) & sent all thru GET Urls.

 

Thanks a lot.

Link to comment
Share on other sites

Here is a complete rewrite (not tested) that puts all the coding in a more logical order and solves the problem

<?php
@session_start();
if(!isset($_SESSION['SESSION'])) {
    require ("sess.php");
}
    
//Set page parameters
$records_per_page = 2;
    
//Set the search criteria
if (isset($_POST['record_id']))
{
    $record_id   = $_POST['record_id']; ;
    $file_number = $_POST['file_number'];
    $name_land   = $_POST['name_land'];
    $location    = $_POST['location'] ;
    $_SESSION['record_id']   = $record_id;
    $_SESSION['file_number'] = $file_number;
    $_SESSION['name_land']   = $name_land;
    $_SESSION['location']    = $location;
}
else
{
    $record_id   = $_SESSION['record_id']; ;
    $file_number = $_SESSION['file_number'];
    $name_land   = $_SESSION['name_land'];
    $location    = $_SESSION['location'] ;
}
//Create common WHERE clause
$WHERE = "file_number = '$file_number' ||
          Record_id = '$record_id'     ||
          land_name = '$name_land'     ||
          location = '$location'";
    
//Connect to database
@$connect = mysql_connect($_SESSION['server'],$_SESSION['un'],$_SESSION['pass'])
    or die ('Error : Unable to connect to the MYSQL Server');;
@mysql_select_db($_SESSION['db']) or die ('Error : Unable to connect to the Data Base');
    
//Get total number of records and determine total pages
$query = "SELECT COUNT(Record_id) as total
          FROM {TABLE}
          WHERE {$WHERE}";
$result = mysql_query($query);
$total_records = mysql_result($result, 0, 'total');
$total_pages = ceil($total_records / $records_per_page);
    
//Determine curernt page and start limit
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
if($page<1 || $page>$total_pages) { $page = 1; }
$start_limit = ($page-1) * $records_per_page;
    
//Get the records for the current page
$query = "SELECT Record_id,file_number,Land Name from TABLE
          WHERE {$WHERE}
          ORDER BY Record_id ASC
          LIMIT {$start_limit}, {$records_per_page}";
$result = mysql_query($query);
    
//Process the results
$tableBody = '';
while ($row = mysql_fetch_array($query1))
{
    $tableBody .= "<tr>\n";
    $tableBody .= "<td>{$row['Record_id']}</td>\n";
    $tableBody .= "<td>{$row['file_number']}</td>\n";
    $tableBody .= "<td>{$row['land_name']}</td>\n";
    $tableBody .= "<td>\n";
    $tableBody .= "<form action=\"dlt_grid.php\" method = \"GET\">\n";
    $tableBody .= "<input type=\"submit\" value=\"Delete\">\n";
    $tableBody .= "<input type=\"hidden\" name=\"hf\" value=\"{$row['Record_id']}\">\n";
    $tableBody .= "</form>\n";
    $tableBody .= "</td>\n";
    $tableBody .= "</tr>\n";
};
    
//Create pagination menu
$page_links_ary = array();
for($p=1; $p<=$total_pages; $p++)
{
    $page_links_ary[] = ($page==$p) ? "<b>{$i}</b>" : "<a href=\"searching.php?page={$i}\">{$i}</a>";
}
$page_links = implode('', $page_links_ary);
        
?>
<html>
<body>
Page: <?php echo $page; ?>
<br /><br />
<table border="1">
    <thead>
        <tr>
            <th>Record ID</th>
            <th>file_number</th>
            <th>Land Name</th>

        </tr>
    </thead>
    <tbody>
    <php echo $tableBody; ?>
    </tbody>
</table>
<br /><br />
Total Records: <?php echo $total_records; ?>
<br /><br />
<?php echo $page_links; ?>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.