It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming PHP and MySQL Forum

pagination and search

Moderator: Malcolm

pagination and search

Postby wood1e on Thu Jun 09, 2005 7:08 am

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...

[php]

<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>

[php]


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!!! :)


PHP:
--------------------------------------------------------------------------------

$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:


PHP:
--------------------------------------------------------------------------------

$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++;
}
wood1e
 
Posts: 11
Joined: Sun May 22, 2005 11:00 pm

Who is online

Users browsing this forum: No registered users and 0 guests