0

I have been messing around, and been trying to make this script work (for later implenting it in my own projects). It works fine, beside that I when I click on a new page, the results doesnt' change..

here is the script:

<?php 
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 5; 
$sql = "SELECT * FROM employee ORDER BY emp_id ASC LIMIT $start_from, 20"; 
$rs_result = mysql_query ($sql); 
?> 
<table>
<tr><td>Name</td><td>Phone</td><td>Salary</td></tr>
<?php 
while ($row = mysql_fetch_assoc($rs_result)) { 
?> 
            <tr>
            <td><?php echo $row['emp_id']; ?></td>
            <td><?php echo $row['emp_name']; ?></td>
            <td><?php echo $row['emp_salary']; ?></td>
            </tr>
<?php 
}; 
?> 
</table>
<?php 
$sql = "SELECT COUNT(emp_name) FROM employee"; 
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
$total_pages = ceil($total_records / 5); 

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='pagination?page=".$i."'>".$i."</a> "; 
}; 
?>
Abidas
  • 1
  • 2
  • in query you passed 20 recode per page and $start_from = ($page-1) * 5; this statement used 5 recode per page replace this with $start_from = ($page-1) * 20; and try – Ankur Bhadania Oct 11 '14 at 12:55
  • or in query change per page recode with 5 – Ankur Bhadania Oct 11 '14 at 12:57
  • I changed it to: $start_from = ($page-1) * 5; $sql = "SELECT * FROM employee ORDER BY emp_id ASC LIMIT $start_from, 5"; But still doesn't seem to work – Abidas Oct 11 '14 at 12:58
  • i use notepad++ it has several options and plugins to align code pls try some.. – Ronser Oct 11 '14 at 13:02
  • hi remove the semicolon after if condition if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; } – Ankur Bhadania Oct 11 '14 at 13:06
  • "SELECT * FROM employee ORDER BY emp_id ASC LIMIT `".$start_from."`, 20"; try editing this and reply also, tell where it throws the error – Ronser Oct 11 '14 at 13:07
  • ankurbhadania sadly it didn't change anything. @Ronser it doesn't throw any errors, but still doesn't work. – Abidas Oct 11 '14 at 13:12
  • Not an answer, but `mysql_*` functions are deprecated and unsafe. See http://stackoverflow.com/q/12859942/3794472 . You should consider switching to `mysqli` or `PDO`. – Jeremiah Winsley Oct 17 '14 at 13:36

1 Answers1

1

Actually there seems nothing fundamentally wrong. Did you try a hard reload in your browser ?

Use this in the unshown head part of your page to get rid of some caching issues:

<html>
        <head>
            <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
            <META HTTP-EQUIV="Expires" CONTENT="-1">
            <meta http-equiv="cache-control" content="no-cache">
        </head>
...
</html>

I changed some things anyway:

  • $perpage contains the number of entries per page nice in one place
  • never use unsanitized values in sql so I (int)ed the $_GET

--- need this line for the code to show ---

<?php 
$perpage = 5;

if (isset($_GET["page"])) { $page  = abs ((int)$_GET["page"]); } else { $page=1; }; 
$start_from = ($page-1) * $perpage; 
$sql = "SELECT * FROM employee ORDER BY emp_id ASC LIMIT $start_from, $perpage"; 
$rs_result = mysql_query ($sql); 
?> 
<table>
<tr><td>Name</td><td>Phone</td><td>Salary</td></tr>
<? php 
while ($row = mysql_fetch_assoc($rs_result)) { 
?> 
            <tr>
            <td><?php echo $row['emp_id']; ?></td>
            <td><?php echo $row['emp_name']; ?></td>
            <td><?php echo $row['emp_salary']; ?></td>
            </tr>
<?php 
}; 
?> 
</table>
<?php 
$sql = "SELECT COUNT(emp_name) FROM employee"; 
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
$total_pages = ceil($total_records / 5); 

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='pagination?page=".$i."'>".$i."</a> "; 
}; 
?> 
snitch182
  • 723
  • 11
  • 21
  • You could further add verification that the page number is within the expected range. Negative values and values higher than `$total_pages` would not be handled cleanly. – Jeremiah Winsley Oct 17 '14 at 13:44
  • That ist true. But it would (only?) show an empty table. But only if you are editing params by hand. It would not cause errors though. – snitch182 Oct 17 '14 at 14:31
  • 1
    A negative offset will throw an SQL error. Since OP's code doesn't include error handling for the query, this will throw a PHP error as well. – Jeremiah Winsley Oct 17 '14 at 14:37
  • OK,Jeremiah. I changed that. – snitch182 Oct 21 '14 at 08:54