• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Get only 10 results at a time(php/mysql)

Weblife

New Member
im gonna start a website with a whole bunch of jokes on it. and what i want is that people could search the jokes database and it just displays 10 jokes a time.. as in links to the jokes, not that actual jokes, and then at the bottom there will be numbers so you could continue browsing through the jokes u searched for, but only 10 at a time

how do i do this? i aint got a clue

thanks in advance.
 
This code should work for you. I found it on my harddrive, and I'm not sure where I got it from. :p

Code:
<?  
  $db_user   = "root";  
  $db_pass    = "pass";  
  $db_name    = "phpMember";  
  $db_host    = "localhost";  

  mysql_pconnect($db_host, $db_user, $db_pass) or die("Unable to connect to SQL server");  
  mysql_select_db($db_name) or die("Unable to select database");  

  $script="search.php";  
  $offset+=0;  
  $item_perpage= 10;
  $prev= $offset-$item_perpage;  
  $next= $offset+$item_perpage;  

  $query="SELECT name,id FROM jokes WHERE name LIKE '%$keyword%' LIMIT $offset, $item_perpage";  
  $querytotal="SELECT count(*) FROM jokes WHERE name LIKE '%$keyword%'";  
  $total=mysql_fetch_row(mysql_query($querytotal));  

   $pages = (int) ($total[0] / $item_perpage);  
   if ($total[0] % $item_perpage) {$pages++;}  
   for ($a=1;$a<=$pages;$a++) {  
	$pagebold = ($offset/$item_perpage)+1;
	if($pagebold == $a) {
     $map[$a] =" <b>$a</b> ";
	} else {
     $map[$a] =" <a href=\"$script?offset=$offset2\">$a</a> ";  
	}
     $offset2+=$item_perpage;  
   }  
   $pagemap=implode("", $map);  

  if ($prev < 0) {$prev="";}  
  else {$prev="<a href=\"$script?offset=$prev&keyword=$keyword\">&lt;&lt;</a>";}  
  if ($total[0] > $next) {$next="<a href=\"$script?offset=$next&keyword=$keyword\">&gt;&gt;</a>";}  
  else {$next="";}  

$from = $offset+1;
$to = $offset+$item_perpage;
$of = $total[0];

echo "Showing <b>$from</b> to <b>$to</b> of <b>$of</b>"; 
        $data=mysql_query($query);  
        while ($result=mysql_fetch_array($data)) {  
	    $id = $result["id"];
	    $name = $result["name"];
            echo "<a href=\"jokes.php?id=$id\">$name</a><br>"; 
        }  
	echo "<p align=\"center\">$prev$pagemap$next</p>;
?>

You'll have to edit some stuff inside to match your database and site configurations, though.
 
Back
Top