Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Search Results on Several Pages

Featured Replies

Hi all,

 

I have a search results page on my site and was wondering how can I divide search results to 10 per page as some keywords could bring up many links ('bulletin' for example).

 

Here's my site

 

Thanks,

 

Niall

Hi all,

 

I have a search results page on my site and was wondering how can I divide search results to 10 per page as some keywords could bring up many links ('bulletin' for example).

 

Here's my site

 

Thanks,

 

Niall

 

Research Pagination it involves using a language like php to split up search results into page links. A good PHP class to do just that is here https://github.com/stefangabos/Zebra_Pagination also i'm sure wordpress has a plugin for this as well if you are using wordpress

Edited by webdesigner93

Depending on the amount of data I normally use a count first followed by an offset call for the 10 per page.

$sql_count = "SELECT COUNT(*) FROM `table`";
$stmt = mysqli_prepare($con, $sql_count);
if(mysqli_stmt_execute($stmt)) {
    $result = mysqli_stmt_get_result($stmt);
    while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
        foreach($row as $r){
            $total_rows = $r;
        }
    }
}
mysqli_stmt_free_result($stmt);
mysqli_stmt_close($stmt);

$rowsperpage = 10; // How many results per page.
$totalpages = ceil($total_rows / $rowsperpage);
if(isset($_GET['page']) && is_numeric($_GET['page'])) { $page = (int)$_GET['page']; $curpage = $page; } else { $page = 1; $curpage = 1; };
$offset = ($page - 1) * $rowsperpage;
$result_start = ($curpage-1)*$rowsperpage+1;
$result_end = $result_start+$rowsperpage-1;
if($result_end >= $total_rows) { $result_end = $resCount; };
if($page > $totalpages && $totalpages>=1) {
    // I normally forward back to last page e.g. they try page 14 of 13 they forward to 13.
};

// now you can get just the 10 required for the page
$sql_data = "SELECT `col1`, `col2`, `col3` FROM `table` LIMIT ?, ?";
// these would be $offset, $rowsperpage

Then you just need the pagination ...

<?php if($totalpages >= 2) { // Only show pagination if there are more than 1 page.
$pageurl = "http://www.yourdomain.com/pagename/"; // Used to keep clean pages ?>
<div>Page <b><?php echo $page; ?></b> of <b><?php echo $totalpages; ?></b><br>
Showing <?php echo $result_start; ?> - <?php echo $result_end; ?> of <?php echo $total_rows; ?> Results</div>
<div>
<?php
$range = 2; // How many page links you want in a range around the current page.
for($x = ($page - $range); $x < (($page + $range) + 1); $x++) {
    // if it's a valid page number...
    if(($x > 0) && ($x <= $totalpages)) {
        // if we're on current page...
        if ($x == $page) {
	    // 'highlight' it but don't make a link
	    echo "<span>" . $x . "</span>";
	} else {
	    echo "<a href=\"" . $pageurl;
	    if($x>= 2) { // Only add a page link if greater than 1, /pagename/?page=1 is the same results as /pagename/ 
	        echo "?page=".$x;
	    }
	    echo "\">".$x."</a>";
	}
    } 
}
?>
</div>
<?php } ?>

Edited by BrowserBugs

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.