PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hello, I have a problem with my code. The php code function is to allow users to search into the database and display database information in paginated form. My searching is working as well as the pagination. My only problem here is that the pages from search results displays up to 2 pages only even if supposedly it should display 3 or more pages. Here is my code:
<?php
$per_page = 15;
if (!empty($_GET['start'])) {
$start = $_GET['start'];
} else {
$start = 0;
}
if (!empty($_GET['search'])) {
$search = $_GET['search'];
} elseif (!empty($_POST['search'])) {
$search = $_POST['search'];
} else {
$search = "";
}
$sql = "SELECT * FROM `records` WHERE `last_name` LIKE '%$search%' OR `first_name` LIKE '%$search%' OR `territory` LIKE '%$search%' OR `job_title` LIKE '%$search%' OR `title` LIKE '%$search%' OR `employer` LIKE '%$search%' ORDER BY `territory` ASC LIMIT $start, $per_page";
$record_count = mysql_num_rows(mysql_query($sql));
$max_pages = ciel($record_count / $per_page);
$get = mysql_query($sql);
if ($get) {
while ($row = mysql_fetch_assoc($get)) {
$id = trim($row['id']);
$territory = trim($row['territory']);
$employer = trim($row['employer']);
$last_name = trim($row['last_name']);
$first_name = trim($row['first_name']);
echo "<tr>";
echo "<td>".$territory."</td>";
echo "<td>".$employer."</td>";
echo "<td>".$last_name.", ".$first_name."</td>";
echo "<td><a href='edit_client.php?id=".$id."'>edit</a> | <a href='delete_client.php?id=".$id."'>delete</a></td>";
echo "</tr>";
}
} else {
echo "<em>No results found.</em>";
}
$prev = $start - $per_page;
$next = $start + $per_page;
if ($start > 0) {
echo "<a href='view_client.php?search=$search&start=$prev'>Prev</a> ";
}
for ($x=0;$x<$max_pages;$x++) {
$y = $x * $per_page;
$z = $x + 1;
if ($start !== $y) {
echo " <a href='view_client.php?search=$search&start=$x'>$z</a> ";
} else {
echo " <b>$z</b> ";
}
}
if ($record_count >= ($next)) {
echo " <a href='view_client.php?search=$search&start=$next'>Next</a>";
}
?>
Can anyone identify what I am doing wrong? I am still newbie and still pushing myself to learn every bit of php. The code I have has been a trial and error and has been with the support of forum sites like phpdn.
Thank you in advanced. I hope to get your comments soon.
Ok, the problem is that you are trying to get the result count from the query you ran, but that query is always restricted to 15 at the most. So, you need to remove the LIMIT statement from the query, and handle the start/limit manually with PHP. Just try removing the LIMIT and see what happens first.