Domanda

It's been a month and am really messed up trying to integrate a php pagination code to my search script. Referred to most of the tutorials Googling, but in vain. Any help would be much appreciated. Here I go...

<?php
 ob_start();
 session_start();
 $_GET['term'] = trim($_GET['term']);
 $output = preg_replace('!\s+!', ' ', $_GET['term']);
 if(empty($_GET['term'])|| preg_match("/^[@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\?\ _ ]+$/i", $_GET['term']) || $output== ' ' || $_GET['term']== "%24_GET%5B%27term%27%5D")
 {
 echo "<BR>";
 echo "<BR>";
 echo("Please enter a Valid Search term");
 }
 else
 {
    mysql_connect("localhost", "root", "root");
    mysql_select_db("search");
    $_GET['term'] = explode(' ', $_GET['term']);
    foreach($_GET['term'] AS $_GET['term'])
     {
     $_GET['term'] = trim($_GET['term']);
 $sql = mysql_query("SELECT DISTINCT * FROM searchengine WHERE pagecontent LIKE '%" . str_replace(' ', "%' AND pagecontent LIKE '%", $_GET['term'])."%' LIMIT 0,10");
   while($ser = mysql_fetch_array($sql)) {
       echo "<BR>";
        echo "<b><u><a href='$ser[pageurl]'>$ser[title]</a></u></b>";
        echo "<BR>";
        echo("<span class='style_block'>{$ser['pagecontent']}</span>");
        echo "<BR>";
        echo ("<a href='$ser[pageurl]'>$ser[pageurl]</a>");
        echo "<BR>";
        echo "<BR>";
       } 
    }
$count=mysql_num_rows($sql);
if($count==0)
{
 echo "<BR>";
 echo "<BR>";
echo "Sorry, No News material was found... Please refine your search criteria and try again.";
}
    }
?>
È stato utile?

Soluzione

Apart from the problems Luc M has mentioned in his comment (which you should certainly resolve before moving forward), you are almost there.

You need to consider a couple of points, really: How many records to display per page, and what page you are on. These will dictate the records you need to retrieve and display. So, how do you go about this?

The first point is covered in your code already through use of the LIMIT clause in your SQL query. The second point is a tiny bit more complex to start with. You need a way of identifying the page you are on. This is probably easiest to identify through a GET variable, for example http://site.com/search.php?page=2. Now, for implementing this, you want something along these lines:

$recordsPerPage = 10; // although you may want to have this as a GET or POST variable as well, so the user can decide
if(isset($_GET['page']) // this ensures a default value
{
    $currentPage = $_GET['page'];
}
else
{
    $currentPage = 1;
}

Then, for your SQL query, you want to build something like this:

$query = "SELECT * FROM table_name LIMIT " . $recordsPerPage . " OFFSET " . ($currentPage - 1)*$recordsPerpage . ";";

The OFFSET clause of SQL along with LIMIT basically says "Select this many records, starting from result number x". You offset on $currentPage - 1 because the first page doesn't want an offset, and the second page only wants an offset of however many records were shown on the first page, so on and so forth.

To create navigation for the paginated data, you want to find out how many records are in your result set, which can be done through the count($array) function of PHP. Then, to find the number of pages, simply use something like:

$numPages = ceil(count($array)/$recordsPerPage);

Where $array is your dataset from the SQL query. The ceil() function rounds the result up to the next integer.

Once you have this result, you simply need to output links to each page, which can be done simply with a for loop:

for($i = 0; i < $numPages; i++)
{
    echo '<a href="/search.php?page="' . $i+1 . '>' . $i+1 . '</a>';
}

To create first, previous, next and last page links, you need to do something like:

$firstPage = 1;
$previousPage = $currentPage - 1; // you may want to check here or elsewhere to make sure you have no page zero
$nextPage = $currentPage + 1; // may also want to make sure you don't go past the last page
$lastPage = $numPages;

These values can then be put into your generated links.

Again, I will refer you to Luc M's comment... These need to be fixed, take a look at mysqli functions instead of the now-deprecated mysql_*() functions you're currently using, make sure you clean any user-inputted data before using it, and consider looking at the MVC design pattern.

Hopefully, this will help you out.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top