bdg bdg - 7 months ago 22
PHP Question

pagination only showing first 10 results instead of x amount of pages with 10 results on each

so I have a piece of

php
code that is supposed to loop through all the results and make a new page for every 10 results, however it only shows the first 10 results and no options of next page or page numbers.

is there a reason for this or have I missed something in the code I have written?

Thanks in advance.

My code is below;

$search_course = "
SELECT title, summary, id
FROM course
WHERE title LIKE '%".$_POST['searchBar']."%'";
$result = $mysqli->query($search_course) or die($mysqli->error);
$search_result = $result->fetch_assoc();
$row = mysqli_fetch_row($result);
//total rows for search
$rows = $row[0];
//number of results per page
$rows_per_page = 10;
//shows last page
$last_page = ceil($rows/$rows_per_page);
if($last_page < 1){
$last_page = 1;
}
$page_number = 1;
if(isset($_GET['pn'])){
$page_number = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
//makes sure page number is between limits of $page_number
if($page_number < 1){
$page_number = 1;
} else if($page_number > $last_page){
$page_number = $last_page;
}
// sets the value of items to view
$limit = 'LIMIT ' .($page_number -1) * $rows_per_page .',' .$rows_per_page;
//queery again only grabbing the set number of rows depending on page number
$search_course = "
SELECT title, summary, id
FROM course
WHERE title LIKE '%".$_POST['searchBar']."%'
ORDER BY title DESC $limit";
$result = $mysqli->query($search_course) or die($mysqli->error);
$search_result = $result->fetch_assoc();
//displays to the user the total number of results and the page numbers
$total_number_of_results = "Search Results (<b>$rows</b>)";
$page_user_is_on = "Page <b>$page_number</b> of <b>$last_page</b>";
//set up pagination
$pagination_controls = '';
if($last_page != 1){
if($page_number > 1){
$previous = $page_number - 1;
$pagination_controls .='<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">previous</a> &nbsp; &nbsp; ';
for($i = $page_number - 4; $i < $page_number; $i++)
{
if($i > 0){
$pagination_controls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
}
}
}
$pagination_controls.=''.$page_number.' &nbsm; ';
//clickable links to the left
for($i = $page_number+1; $i <= $last_page; $i++)
{
$pagination_controls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
if($i >= $page_number+4){
break;
}
}
if($page_number != $last){
$next = $page_number + 1;
$pagination_controls.='&nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a>';
}
}
$list = '';
while($row = mysqli_fetch_array($result)){
$title = $row['title'];
$id = $row['id'];
$list.='<p><a href="Selectedcourse.php">'.$title.' </p>';
}
mysqli_close($mysqli);


and the html element;

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel='stylesheet' href='courses.css'>
</head>
<body>
<div class="header">
<h1>Search Results for - <?= $_POST['searchBar'] ?></h1>
</div>
<div>
<h3> <?php echo $page_user_is_on ?> </h3>
<p><?php echo $list; ?></p>
<p><?php echo $search_result['summary']; ?> </p>
</div>
<div id="pagination_controls"><?php echo $pagination_controls; ?></div>

</body>
</html>

Answer

This is a good effort to roll your own pagination. It was mostly working. The problem was in your first query here:

$search_result = $result->fetch_assoc();
//This fetches only the first row of results
$row = mysqli_fetch_row($result);
//So this is not the total rows for search
$rows = $row[0];

Here's an updated script using prepared statements, which you really need to use. Unless you love being hacked.

//turn errors on to develop, back off when you go live
error_reporting(E_ALL);
ini_set('display_errors', 1);

//here are my main changes
$search_param = "%" . $_POST['searchBar'] . "%";
$stmt = $mysqli->prepare("SELECT title, summary, id FROM course WHERE title LIKE ?");   
$stmt->bind_param("s", $search_param); //learn this
$stmt->execute();
$result = $stmt->get_result();

//This gets the number of rows in a query result
$rows = $result->num_rows;

//number of results per page
$rows_per_page = 10;

//shows last page
$last_page = ceil($rows/$rows_per_page);
if($last_page < 1){
    $last_page = 1;
}

if(isset($_GET['pn'])){
    $page_number = preg_replace('#[^0-9]#', '', $_GET['pn']);
} else {
    $page_number = 1;
}

//makes sure page number is between limits of $page_number
if($page_number < 1){
    $page_number = 1;
} else if($page_number > $last_page){
    $page_number = $last_page;
}

// sets the value of items to view
$limit = 'LIMIT ' .($page_number -1) * $rows_per_page .',' .$rows_per_page;

//displays to the user the total number of results and the page numbers
$total_number_of_results = "Search Results (<b>$rows</b>)";
$page_user_is_on = "Page <b>$page_number</b> of <b>$last_page</b>";

//query again only grabbing the set number of rows depending on page number
$stmt = $mysqli->prepare("SELECT title, summary, id FROM course WHERE title LIKE ? ".$limit);   
$stmt->bind_param("s", $search_param);
$stmt->execute();
$result = $stmt->get_result();


$list = '';
while($row = $result->fetch_assoc()){
    $title = $row['title'];
    $id = $row['id'];
    //I'm assuming you want each link to be different here...
    $list.='<p><a href="Selectedcourse.php?id='.$id.'">' . $title . '</a></p>';
}
mysqli_close($mysqli);

//set up pagination
$pagination_controls = '';
if($last_page != 1){
    if($page_number > 1){
        $previous = $page_number - 1;
        $pagination_controls .='<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">previous</a> &nbsp; &nbsp; ';
        for($i = $page_number - 4; $i < $page_number; $i++)
        {
            if($i > 0){
                $pagination_controls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
            }
        }
    }
    $pagination_controls.=''.$page_number.' &nbsp; ';

    //clickable links to the left
    for($i = $page_number+1; $i <= $last_page; $i++)
        {
            $pagination_controls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
            if($i >= $page_number+4){
                break;
            }
        }
        if($page_number != $last_page){
            $next = $page_number + 1;
            $pagination_controls.='&nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a>';
        }
}

And finally the HTML section, with only one change:

<div class="header">
    <h1>Search Results for - <?= $_POST['searchBar'] ?></h1>
</div>
<div>
<h3> <?php echo $page_user_is_on ?> </h3>
<p><?php echo $list; ?></p>
<p><?php /* echo $search_result['summary']; //Where was this coming from? */?> </p>
</div>
<div id="pagination_controls"><?php echo $pagination_controls; ?></div>