pagination and search

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wood1e
Forum Newbie
Posts: 7
Joined: Tue Oct 05, 2004 11:39 am

pagination and search

Post by wood1e »

Hi I have used the tutorial on full text search which was brilliant...I now want to add in pagination to it....Bu ti can;t seem to work it from the pagination tutorial...


This is the code for my full text search...

Code: Select all

<html> 
<head><title>Search</title></head> 
<body> 

<?php 

// Database Connection 
include 'db.php'; 


// Create the search function: 

function searchForm() 
{ 
// Re-usable form 

// variable setup for the form. 
$searchwords = (isset($_GET['words']) ? htmlspecialchars(stripslashes($_REQUEST['words']))
: ''); 
$normal = (($_GET['mode'] == 'normal') ? ' selected="selected"' : '' ); 
$boolean = (($_GET['mode'] == 'boolean') ? ' selected="selected"' : '' ); 

echo '<form method="get" action="'.$_SERVER['PHP_SELF'].'">'; 
echo '<input type="hidden" name="cmd" value="search" />'; 
echo 'Search for: <input type="text" name="words" value="'.$searchwords.'" /> '; 
echo 'Mode: '; 
echo '<select name="mode">'; 
echo '<option value="normal"'.$normal.'>Normal</option>'; 
echo '<option value="boolean"'.$boolean.'>Boolean</option>'; 
echo '</select> '; 
echo '<input type="submit" value="Search" />'; 
echo '</form>'; 
} 




// Create the navigation switch 
$cmd = (isset($_GET['cmd']) ? $_GET['cmd'] : ''); 

switch($cmd) 
{ 
default: 
echo '<h1>Search Database!</h1>'; 
searchForm(); 

break; 


case "search": 
searchForm(); 
echo '<h3>Search Results:</h3><br />'; 

$searchstring = mysql_escape_string($_GET['words']); 
switch($_GET['mode']) 
{ 
case "normal": 
$sql = "SELECT id, author, title, caption, dts, 
MATCH(author, title, caption, full_body) 
AGAINST ('$searchstring') AS score FROM user 
WHERE MATCH(author,title, caption, full_body) 
AGAINST ('$searchstring') ORDER BY score DESC"; 
break; 

case "boolean": 
$sql = "SELECT id, author, title, caption, dts, 
MATCH(author, title, caption, full_body) 
AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM user 
WHERE MATCH(author, title, caption, full_body) 
AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC"; 
break; 
} 

// echo $sql; 

$result = mysql_query($sql) or die (mysql_error()); 

while($row = mysql_fetch_object($result)) 
{ 
echo '<strong>Title: '.stripslashes(htmlspecialchars($row->title)).'</strong><br />'; 
echo '<strong>Author: '.stripslashes(htmlspecialchars($row->author)).'</strong><br /> 
'; 
echo 'Score:'. number_format($row->score, 1).' Date: '.date('d/m/y', $row->dts).'<br />'; 
echo '<p>'.stripslashes(htmlspecialchars($row->caption)).'</p>'; 
echo '<hr size="1" />'; 
} 
break; 
} 




?> 

</body> 
</html>

And below is some code a I was given to add in to give pagination....but I can't seemt o get it to work...please anyone out there that can help...I am getting desparate...and my eyes are now hirting!!! :)

Code: Select all

--------------------------------------------------------------------------------
 
$sql = "select SQL_CALC_FOUND_ROWS * from news where approved='1' order by story_date desc limit $offset,$results_per_page"; 

   $result = mysql_query($sql); 

   $sql = "select FOUND_ROWS()"; 
   $count_result = mysql_query($sql); 
   $count =    mysql_fetch_array($count_result);

--------------------------------------------------------------------------------

This will give you two things, a subset of results starting at record # offset and returning $results_per_page results. It will also give you the total number of rows that would have been returned had you not limited the result set.

You can then use the result set in your pagination, and use the number of results that woudl have been returned to display your page numbers at the bottom of the page.

Here's how I would display page numbers:

Code: Select all

$pages = 0; 
    
   while($count > 0){ 
         echo "<a href='index.php?module=news&action=news&offset=".($pages*$results_per_page)."'>" . ($pages + 1) . "</a>&nbsp;&nbsp;"; 
         $count = $count - $rpp; 
         $pages++; 
   }

feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

hi....if you like, i can post my pagination code here and just play around the code to get what will fit your page...
method_man
Forum Contributor
Posts: 257
Joined: Sat Mar 19, 2005 1:38 am

Post by method_man »

when u use the php tags u have to have an ending one

thats what it looks like
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

pagination code has been talked about many many many many times in the forums. Things to search for: pagination. ;)
Post Reply