Jump to content

Website search pagination trouble???


simmsy

Recommended Posts

Hi I was wondering if anyone could help with my php coding I recently started so any help would be appreciated ive got a simple website search and the search performs fine its just trouble with the pagination it constantly shows up that theres 3 pages even if there should only be 1 and when they click on next page that page doesn't exist so take them back to index. Heres the code please help if you can spot the problem please:

<?php

  //connect to our datebase

include("connect.php");

 

$limit = 10; //Number of results per page

$page=$_GET["page"]; //Get the page number to show

if($page == "") $page=1; //If no page number is set, the default page is 1

 

//Get the number of results

$searchresult=mysql_query("SELECT * FROM search WHERE keywords LIKE '%$searchstring%' ORDER BY title") or die(mysql_error());

$numberofresults=mysql_num_rows($searchresult);

 

//Get the number of pages

$numberofpages=ceil($numberofresults/$limit);

 

//get data

$button = $_GET['submit'];

$search = $_GET['search'];

 

if ($search==""){

    echo "<script language=\"JavaScript\">\n";

echo "alert('No search entered!');\n";

echo "window.location='search.php'";

echo "</script>";

}else{

        if (strlen($search)<=2){

                echo "<script language=\"JavaScript\">\n";

echo "alert('Search too short!');\n";

echo "window.location='search.php'";

echo "</script>";

        }else{

                echo "You searched for <b>'$search'</b><hr size='1'>";

             

 

             

                //explode our search term

                $search_exploded = explode(" ",$search);

             

                foreach($search_exploded as $search_each){

                        //construct query

                        $x++;

                        if ($x == 1){

                                $construct .= "keywords LIKE '%$search_each%'";

                        }else{

                                $construct .= "OR keywords LIKE '%$search_each%'";

                        }

                }

             

                $construct = ("SELECT * FROM search WHERE $construct ORDER BY title LIMIT " . ($page-1)*$limit . ",$limit");

                $run = mysql_query($construct);

             

                $foundnum = mysql_num_rows($run);

             

                if ($foundnum == 0){

                        echo "<script language=\"JavaScript\">\n";

echo "alert('No results found!');\n";

echo "window.location='index.php'";

echo "</script>";

die;

 

                }

                     

                        while ($runrows = mysql_fetch_assoc($run)){

                                //get data

                                $title = $runrows['title'];

                                $desc = $runrows['description'];

                                $url = $runrows['url'];

$picture = $runrows['picture'];

 

                              echo "<table width='600' align='center'>

              <tr>

                <td rowspan='2' align='left' valign='middle' width='110'><a href='$url'><img src='$picture' height='100' width='100' border='0'></a></td>

                <td align='center' valign='middle'><a href='$url'><b>$title</b></a></td>

              </tr>

            </table>";

}

}

?>

</div>

<div align="center">

<?

$nav="";

if($page > 1) {

$nav .= "<a href=\"searching.php?page=" . ($page-1) . "&search=" .urlencode($search) . "&submit=" .urlencode($button) . "\"><< Prev</A>";

}

for($i = 1 ; $i <= $numberofpages ; $i++) {

if($i == $page) {

$nav .= "<b>$i</b>";

}else{

$nav .= "<a href=\"searching.php?page=" . $i . "&search=" .urlencode($search) . "&submit=" .urlencode($button) . "\"> $i </A>";

}

}

if($page < $numberofpages) {

$nav .= "<a href=\"searching.php?page=" . ($page+1) . "&search=" .urlencode($search) . "&submit=" .urlencode($button) . "\">Next >></A>";

}

 

Echo "<br><br>" . $nav;

                        }

     

?>

</div>

Any help welcome Thanks!

Link to comment
https://forums.phpfreaks.com/topic/204634-website-search-pagination-trouble/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.