Jump to content

php pagination - hightlight current page


matthew9090

Recommended Posts

Well, you need a variable for the current page. Assuming the value is stored in the variable $current_page you would do something like below. Note I change $i to $page - always give your variables descriptive names.

$page =1; 
for ($x=0;$x<$foundnum;$x=$x+$e)
{
    $style = ($page = $current_page) ? 'font-weight:bold;' : '';
    echo " <a href='search.php?search=$search&s=$x' style='{$style}'>$page</a> ";
    $page++;

 

Also, what is the purpose of $x and $e in your code? I would just use a for() loop where the $page variable is incremented as needed.

here is the full code:

 

<?php

//get data
$button = $_GET['submit'];
$search = $_GET['search'];


$s = $_GET['s'];
if (!$s)
$s = 0;


$e = 10;


$next = $s + $e;
$prev = $s - $e;




if (strlen($search)<=2)
  echo "Must be greater then 3 chars";
else
{
  echo "<br /><table><tr><td><a href = index.php></a></td><td><form action='search.php' method='GET'><a href ='index.php'><img src='logo.png' align='top' height='28' width='60'></img></a><input type='text' value='' size='50' name='search' value='$search'> <input type='submit' name='submit' value='Search'></form></td></tr></table>";
  
  //connect to database
  mysql_connect("localhost","root","");
  mysql_select_db("test");
   
   //explode out 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%'";
   
   }
   
  //echo outconstruct
  $constructx = "SELECT * FROM search WHERE $construct";
  
  $construct = "SELECT * FROM search WHERE $construct LIMIT $s,$e";
  $run = mysql_query($constructx);
  
  $foundnum = mysql_num_rows($run);
   
  $run_two = mysql_query("$construct");
  
  if ($foundnum==0)
   echo "No results found for <b>$search</b>";
  else
  {
   echo "<table bgcolor='#0000FF' width='100%' height='1px'><br /></table><table bgcolor='#f0f7f9' width='100%' height='10px'><tr><td><div align='right'><b>$foundnum</b> result(s) found for <b>$search.</b></div></td></tr></table><p>";
   
   while ($runrows = mysql_fetch_assoc($run_two))
   {
    //get data
   $title = $runrows['title'];
   $desc = $runrows['description'];
   $url = $runrows['url'];
   
   echo "<table width='300px'>
   <h4><a href='http://$url'><b>$title</b></a><br />
   $desc<br>
   <font color='00CC00'>$url</font></table></h4>
   ";
   }
?>

<table width='100%'>
<tr>
<td>
<div align="center">

<?php
if (!$s<=0)
echo "<a href='search.php?search=$search&s=$prev'>Prev</a>";

$i =1; 
for ($x=0;$x<$foundnum;$x=$x+$e)
{

echo " <a href='search.php?search=$search&s=$x'>$i</a> ";


$i++;


}

if ($s<$foundnum-$e)
  echo "<a href='search.php?search=$search&s=$next'>Next</a>";

  
}
}  

?>
</div>
</td>
</tr>
</table>

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.