Welcome Guest [Log In] [Register]
Add Reply
Better search engine with Paginate
Topic Started: Jan 23 2009, 10:44 AM (119 Views)
php-coder
Member Avatar

Hello,

I put this search engine together after giving Jakkis ago and not liking it, this search engine has paginate built in so you can either show 10, 20, 30, results per page entirely upto you, you only need to make some small edits these being your Database information. This also links to users profiles, i will be modifying it over time to search emails also, and then link some user info below the search results similair to how google displays its results.


Code:
 

<form name="form" action="search.php" method="get">
<input type="text" name="q" />
<input type="submit" name="Submit" value="Search" />
</form>


<?php

$var = @$_GET['q'] ;
$s = $_GET['s'];

$trimmed = trim($var);

$limit=10;

if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}

if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}

mysql_connect("HOSTNAME","USERNAME","PASSWORD"); // Make sure to enter your Hostname, Username & Password

mysql_select_db("login") or die("Unable to select database");

$query = "select * from TABLE where username like \"%$trimmed%\" // Make sure to add your table names here that you want to search
order by username";

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

if ($numrows == 0)
{
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";

echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}

if (empty($s)) {
$s=0;
}

$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");

echo "<p>You searched for: "" . $var . ""</p>";

echo "Results :<br><br>";
$count = 1 + $s ;

while ($row= mysql_fetch_array($result)) {

$usersname = $row['username'];
echo "<td><a href='userinfo.php?user=$usersname'>$usersname</a></div></td>";
echo "</tr>";
echo "</table>";


$count++ ;
}

$currPage = (($s/$limit) + 1);

echo "<br /><br><br>";

if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev</a>&nbsp ";
}

$pages=intval($numrows/$limit);

if ($numrows%$limit) {
$pages++;
}

if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

$news=$s+$limit;

echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next >></a>";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";

?>



Enjoy, Feel free to modify this, Any good modifications please post them up.

~Pc
Offline Profile Quote Post Goto Top
 
Admin
Administrator
very nice :)
Offline Profile Quote Post Goto Top
 
php-coder
Member Avatar

Its proving to be a pain in the butt to get it to show the users avatar aswell as there name and a link to there profile if anyone wants to give this a shot and keep us updated on your prgoress.

~Pc
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Other · Next Topic »
Add Reply