mysql - PHP Search Result Split Into Pages -



mysql - PHP Search Result Split Into Pages -

i creating search engine website in want search result split pages navigation. here php code

<?php if (isset($_get['search'])){ $search = $_get['search']; $query = "select * search keywords '%$search%' or title '%$search%' order keywords limit 0, 30 "; // connect mysql_connect("localhost","root","") or die("could not connect"); mysql_select_db("search") or die("could not find db"); $query = mysql_query($query); $numrrows = mysql_num_rows($query); if ($numrrows > 0) { echo "$numrrows result found searching <b>$search </br> "; while ($row = mysql_fetch_assoc($query)) { $id = $row['id']; $title = $row['title']; $description = $row['description']; $keywords = $row['keywords']; $link = $row['link']; echo "<h2><a href='$link'>$title</a></h2> $description</b></br>"; } } } ?>

i utilize mysqls offset this.

step 1: store page number in query string e.g. www.example.com?page=1. doesn't matter how (whether it's form or link www.example.com?page=1), have number in query string.

// assign globals local variables if (empty($_get['page'])) { $page = 1; } else { $page = $_get['page']; } // pagination $items = 3; // number of items per page $offset = ($page * $items) - $items;

the code above takes page number , figures out how many should offset by.

step 2: write sql query

$query = "select * search keywords '%$search%' or title '%$search%' order keywords limit $items offset $offset";

step 3: pagination buttons: getting next/previous page can simple as

<a href="www.example.com?page=<?php echo $page + 1; ?>">next page</a> <?php if ($page != 1) { ?> <a href="www.example.com?page=<?php echo $page - 1; ?>">previous page</a> <?php } ?>

php mysql

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -