Jump to content

Search results pagination


squiblo

Recommended Posts

Hi, I have two fully working scripts, the first being a search script and the second being a pagination script, and my hair is all falling out trying to get pagination for my search results by trying to merge the two scripts together, please could someone help im losing the will to live  :'(

 

this is the search script...

<?php

$search = $_GET['results'];
if(strlen($search)<3)
{
   echo "Your search must be at least 3 characters long.";
   $div_height = 'height:500px;';
   $img_height = 'height="500"';
}
else
{
   echo ""; 


//connect to our database
mysql_connect("***","***","***");
mysql_select_db("***");


//explode our search term
$search_exploded = explode(" ",$search);

foreach($search_exploded as $search_each)
{
   //construct query
   $x++;
   if ($x==1)
      $construct .= "company LIKE '%$search_each%'";
   else
      $construct .= " OR company LIKE '%$search_each%'";         
}
//echo out construct

$construct = "SELECT * FROM members WHERE $construct";
$run = mysql_query($construct);

$foundnum = mysql_num_rows($run);

if ($foundnum==0)
   echo "You searched for <b>$search</b>. No results found.";
else
{
   echo "You searched for <b>$search</b><br>$foundnum result(s) found!<p><hr size='1' width='387'color='#E6E6E6' align='left'>";

   while ($runrows = mysql_fetch_assoc($run))
   {
      //get data
      $city = ucwords($runrows['city']);
      $pageurl = $runrows['pageurl'];
      $company = ucwords($runrows['company']);
      $imagelocation = $runrows['imagelocation'];
      if ($imagelocation == "") 
      {
         $imagelocation = "./profileimages/noprofilepic.jpg";
      } 
      echo "
      <img src ='$imagelocation' width='100' height='105' border='0' align='left' style='padding-right:10px'><br>
      <b>$company</b><br>
      $city<br>
      <a href='$page' style='text-decoration:none;'><font color='#FFFF00'><u>View page</u></font></a><br><br><br><br><br>
      <hr size='1' width='387' align='left' color='#E6E6E6'>
      ";
   }
}   

}


?>

 

and this is the pagination script i have...

<?php

mysql_connect('***','***','***') or die("Couldn't connect");
mysql_select_db('***') or die("Couldn't find db");

//max per page
$per_page = 5;

//start variable
$start = $_GET['start'];

//$count records
$record_count = mysql_num_rows(mysql_query("SELECT * FROM members"));

//count max pages
$max_pages = $record_count / $per_page; //may be a decimal

if (!$start)
$start = 0;

//display data
$get = mysql_query("SELECT * FROM members LIMIT $start,$per_page");
while ($row = mysql_fetch_assoc($get))
{
//get data
$company = $row['company'];
$id = $row['id'];

echo $company." (".$id.")<br />";

}

//set prev and next variables
$prev = $start - $per_page;
$next = $start + $per_page;

//show prev
if (!($start<=0))
echo " <a href='resultstest.php?start=$prev'>Previous</a> ";

//show numbers

//set variable for first page

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


//show next
if (!($start>=$record_count-$per_page))
echo " <a href='resultstest.php?start=$next'>Next</a> ";



?>

Link to comment
https://forums.phpfreaks.com/topic/172545-search-results-pagination/
Share on other sites

Hi, I have two fully working scripts, the first being a search script and the second being a pagination script, and my hair is all falling out trying to get pagination for my search results by trying to merge the two scripts together, please could someone help im losing the will to live

 

If you are already losing the will to live over this small problem you might aswell quit programming because you'll encounter problems later that will be a lot harder then this one.

 

if(strlen($search)<3)

 

Can also be written as:

 

if (!isset($search[2]))

 

--

 

echo "";

 

Is useless as it does nothing, remove it.

 

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

 

If you want the words inside a string use:

 

$search_exploded = str_word_count($search, 1);

 

$where = array();
foreach ($search_exploded as $search_each) {
    $where[] = "company LIKE '%$search_each%'";
}

$construct = 'SELECT * FROM members WHERE ' . implode(' OR ', $where);
print $construct;

 

And can you please explain which problem you are having?

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.