Jump to content

Search with pagination help, please?


simmsy

Recommended Posts

Hi ive almost got my website up and running im just having trouble with the pagination on the search engine. It shows the first 4 (or how ever many I have set) results then when I click next page it just displays the error of the search engine bar and not the rest of the search, please help, im desperate??

Heres the code:

<?php

include("connect.php");

 

//max displayed per page

$per_page = 4;

 

//get start variable

$start = $_GET['start'];

 

//count records

$record_count = mysql_num_rows(mysql_query("SELECT * FROM search"));

 

//count max pages

$max_pages = $record_count / $per_page; //may come out as decimal

 

if (!$start)

  $start = 0;

 

//get data

$button = $_GET['submit'];

$search = $_GET['search'];

 

if (!$button){

    echo "<span class='class2 style39'>You didn't submit a keyword.</span>";

}else{

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

                echo "Search term too short.";

        }else{

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

             

                //connect to our datebase

include("connect.php");

             

                //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 LIMIT $start, $per_page";

                $run = mysql_query($construct);

             

                $foundnum = mysql_num_rows($run);

             

                if ($foundnum == 0){

                        echo "No results found.";

                }else{

                        echo "$foundnum results found!";

                     

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

                                //get data

                                $title = $runrows['title'];

                                $desc = $runrows['description'];

                                $url = $runrows['url'];

$picture = $runrows['picture'];

 

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

              <tr>

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

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

              </tr>

              <tr>

                <td align='center' valign='middle'>$desc</td>

              </tr>

              <tr>

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

              </tr>

            </table><p></p>";

                        }

//setup prev and next variables

$prev = $start - $per_page;

$next = $start + $per_page;

 

//show prev button

if (!($start<=0))

      echo "<a href='pagination.php?start=$prev'>Prev</a> ";

 

//show page numbers

 

//set variable for first page

$i=1;

 

for ($x=0;$x<$record_count;$x=$x+$per_page)

{

if ($start!=$x)

    echo " <href='pagination.php?start=$x'>$i</a>  ";

else

    echo " <a href='pagination.php?start=$x'><b>$i</b>  ";

$i++;

}

 

//show next button

if (!($start>=$record_count-$per_page))

      echo " <a href='pagination.php?start=$next'>Next</a>";

                        }

        }

}

?>

Thanks

Link to comment
https://forums.phpfreaks.com/topic/195739-search-with-pagination-help-please/
Share on other sites

Please use the php/code tags next time.

 

I briefly glanced over, and this stuck out:

for ($x=0;$x<$record_count;$x=$x+$per_page)
{
if ($start!=$x)
  echo " <href='pagination.php?start=$x'>$i</a>  ";
else
   echo " <a href='pagination.php?start=$x'><b>$i</b>  ";
$i++;
}

The first line was missing an "a"

 echo " <a href='pagination.php?start=$x'>$i</a>  ";

and the second was missing a closing tag

 echo " <a href='pagination.php?start=$x'><b>$i</b> </a> ";

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.