PHP & MySQL - Pagination display problems
For some reason my search script will only display the first set of results for the first page but when I click on the pagination link to the next page the results that are generated from the search script will not be displayed how can I correct this problem?
Here is my PHP & MySQL pagination code.
$x = '';
$construct = '';
if(isset($_POST['search'])) {
$search = $_POST['search'];
if(strlen($search) <= 2){
echo '';
} else {
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
mysqli_select_db($mysqli, "sitename");
$search_explode = explode(" ", $search);
foreach($search_explode as $search_each) {
$x++;
if($x == 1){
$construct .= " article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
} else {
$construct .= " OR article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
}
}
$construct = "SELECT users.*, users_articles.* FROM users_articles
INNER JOIN users ON users_articles.user_id = users.user_id
WHERE $construct";
$run = mysqli_query($mysqli, $construct);
$search_term = mysqli_num_rows($run);
}
}
// Number of records to show per page:
$display = 10;
// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined.
$pages = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_GET['p'])));
} else { // Need to determine.
// Count the number of records:
$records = $search_term;
// Calculate the number of pages...
if ($records > $display) { // More than 1 page.
$pages = ceil ($records/$display);
} else {
$pages = 1;
}
} // End of p IF.
// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_GET['s'])));
} else {
$start = 0;
}
// Make the links to other pages, if necessary.
if ($pages > 1) {
// Add some spacing and start a paragraph:
echo '<p>';
// Determine what page the script is on:
$current_page = ($start/$display) + 1;
//add this here... first will always be one
if ($current_page != 1) {
echo '<a href="search.php">First</a>';
}
// If it's not the first page, make a Previous button:
if ($current_page != 1) {
echo '<a href="search.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> ';
}
//create the links
for ($i = max(1, $current_page - 3); $i <= min($current_page + 3, $pages); $i ++) {
if ($i != $current_page) {
echo '<a href="search.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
} else {
echo '<span>' . $i . '</span> ';
开发者_开发技巧 }
}
// If it's not the last page, make a Next button:
if ($current_page != $pages) {
echo '<a href="search.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>';
}
//add this here... Last will always be one
if ($current_page != $pages) {
echo '<a href="search.php?s=' . ($display * ($pages - 1)) . '&p=' . $pages . '">Last</a>';
}
echo '</p>'; // Close the paragraph.
} // End of links section.
Here is the part of PHP & MySQL search code.
$x = '';
$construct = '';
if(isset($_POST['search'])) {
$search = $_POST['search'];
if(strlen($search) <= 2){
echo 'Your search term is too short!';
} else {
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
mysqli_select_db($mysqli, "sitename");
$search_explode = explode(" ", $search);
foreach($search_explode as $search_each) {
$x++;
if($x == 1){
$construct .= " article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
} else {
$construct .= " OR article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
}
}
$construct = "SELECT users.*, users_articles.* FROM users_articles
INNER JOIN users ON users_articles.user_id = users.user_id
WHERE $construct";
$construct .= " LIMIT $start, $display";
$run = mysqli_query($mysqli, $construct);
$foundnum = mysqli_num_rows($run);
if ($foundnum == 0) {
echo 'Search term is too short!</p>No results found.';
} else {
echo 'results';
}
}
}
That's a lot of code to look through, but could I hazard a guess that $_POST['search']
isn't set when you click on a pagination link, thus causing your entire second block of code not to be run?
Use Session . Below I show you an example of how to do it.
$number = $_POST['studentid'];
if (empty($number)) {
$number=$_SESSION['number'];
}
else{$number=$number ; )
//take a look
At the top of the page put - <? session_start(); ?>
Put the following just before the script for navigation (Next Page/Previous Page)
$_SESSION['number']=$number;
$_COOKIE['number'] = $number;
精彩评论