Jump to content

Issue with Pagination


dapcigar

Recommended Posts

I want to add the result of a query to another table but every time i click on it, it only saves the first record, please help me check out my code

 

thanks

 

 <?php
               // hide errors
 error_reporting(0);
               
               /* Include the Pear::Pager file */
 include('mysql_connect.php');   //include of db config file
include ('paginate.php'); //include of paginat page
 
$per_page = 5;         // number of results to show per page
$result = mysql_query("SELECT * FROM users");
$total_results = mysql_num_rows($result);
$row=mysql_fetch_assoc($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have
 //while ($row=mysql_fetch_assoc($result)){

//-------------if page is setcheck------------------//
if (isset($_GET['page'])) {
    $show_page = $_GET['page'];             //it will telles the current page
    if ($show_page > 0 && $show_page <= $total_pages) {
        $start = ($show_page - 1) * $per_page;
        $end = $start + $per_page;
    } else {
        // error - show first set of results
        $start = 0;              
        $end = $per_page;
    }
} else {
    // if page isn't set, show first set of results
    $start = 0;
    $end = $per_page;
}
// display pagination
$page = intval($_GET['page']);

$tpages=$total_pages;
if ($page <= 0)
    $page = 1;
?>

 <?php
// hide errors
 error_reporting(0);
                    $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
                    echo '<div class="pagination"><ul>';
                    if ($total_pages > 1) {
                        echo paginate($reload, $show_page, $total_pages);
                    }
                    echo "</ul></div>";
                    // display data in table
                    echo "<table class='table table-bordered'>";
                    echo "<thead><tr><th>First Name</th> <th>Last Name</th> <th>Department</th> <th>Status</th></tr></thead>";
                    // loop through results of database query, displaying them in the table
                    for ($i = $start; $i < $end; $i++) {
                        // make sure that PHP doesn't try to show results that don't exist
                        if ($i == $total_results) {
                            break;
                        }
                   
                        // echo out the contents of each row into a table
                        echo "<tr> ";
                        echo '<td>' . mysql_result($result, $i, 'firstname') . '</td>';
                        echo '<td>' . mysql_result($result, $i, 'lastname') . '</td>';
                         echo '<td>' . mysql_result($result, $i, 'department') . '</td>';
                        
                         echo ' <td><a   href="add_team.php?id='.$row['id'].'"> Add Team Member </a></td>';
                      }
                        echo "</tr>";
                    
            //        }       
                    // close table>
                echo "</table>";
            // pagination
            
        
            ?>

Link to comment
Share on other sites

The code has many problems. For example

 

$result = mysql_query("SELECT * FROM users");
$total_results = mysql_num_rows($result);

$row=mysql_fetch_assoc($result);

 

You don't query all the records to get the count. That's a waste of resources. Just query the count directly SELECT COUNT(*) FROM table

 

It was too difficult trying to work with the code you have, so here is a rewrite that should get you started

 

<?php
 
//Show errors
error_reporting(-1);
 
##Config settings
$records_per_page = 5; // number of results to show per page
               
/* Include the Pear::Pager file */
include('mysql_connect.php');   //include of db config file
include ('paginate.php'); //include of paginat page
 
//Get count of total record count
$query = "SELECT COUNT(*) FROM users";
$result = mysql_query($query);
$total_records = mysql_result($result, 0);
//Calculate total pages
$total_pages = ceil($total_records / $records_per_page);
 
//Determine current page
$current_page = isset($_GET['page']) ? intval($_GET['page']) : 1;
if($current_page<1 || $current_page>$total_pages)
{
    //Set page to 1 if page # is invalid
    $current_page = 1;
}
 
//Get records for current page
$startIndex = ($current_page-1) * $records_per_page;
$query = "SELECT id, firstname, lastname, department
          FROM users
          ORDER BY lastname ASC, firstname ASC
          LIMIT {$startIndex}, {$records_per_page}";
$result = mysql_query($query);
 
//Create output for the result set
$current_pageOutput = '';
while($row = mysql_fetch_assoc($result))
{
    $current_pageOutput .= "<tr>\n";
    $current_pageOutput .= "  <td>{$row['firstname']}</td>\n";
    $current_pageOutput .= "  <td>{$row['lastname']}</td>\n";
    $current_pageOutput .= "  <td>{$row['department']}</td>\n";
    $current_pageOutput .= "  <td><a href='add_team.php?id={$row['id']}'>Add Team Member</a></td>\n";
    $current_pageOutput .= "</tr>\n";
}
 
//Create pagination links
//This can be modified to only show certain 'spans' of pages if there are too many
$pagination = '';
for($page=1; $page<=$total_pages; $page++)
{
    if($page != $current_page)
    {
        $pagination .= "<li><a href='?page={$page}'>{$page}</a></li>";
    }
    else
    {
        $pagination .= "<li><b>{$page}</b></li>";
    }
}
 
?>
<html>
<head></head>
<body>
<div class="pagination">
  <ul>
    <?php echo $pagination; ?>
  </ul>
</div>
<table class='table table-bordered'>
  <thead><tr><th>First Name</th> <th>Last Name</th> <th>Department</th> <th>Status</th></tr></thead>
  <?php echo $current_pageOutput; ?>
</table>
 
</body>
</html>
Link to comment
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.