Need help with Search Script

I need help with Search Script I have the code but am at a loss on one area. This isnt my php it was on the internet unfortunatly the ppl who made it no longer exists. The problem I have is in this area

[php]// Build SQL Query
$query = “select * from the_table where 1st_field like “%$trimmed%”
order by 1st_field”; // EDIT HERE and specify your table and field names for the SQL query[/php]

I dont know what to put. The table I am searching is “search” and the field name is “title”
here is the whole code just in case anyone else would like it. Thanks for anyhelp

[php]<?php

// Get the search variable from URL

$var = @$_GET[‘q’] ;
$trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10;

// check for an empty string and display a message.
if ($trimmed == “”)
{
echo “

Please enter a search…

”;
exit;
}

// check for a search parameter
if (!isset($var))
{
echo “

We dont seem to have a search parameter!

”;
exit;
}

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect(“localhost”,“username”,“password”); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db(“database”) or die(“Unable to select database”); //select which database we’re using

// Build SQL Query
$query = “select * from the_table where 1st_field like “%$trimmed%”
order by 1st_field”; // EDIT HERE and specify your table and field names for the SQL query

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

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
{
echo “

Results

”;
echo “

Sorry, your search: “” . $trimmed . “” returned zero results

”;

// google
echo “

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

";
}

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die(“Couldn’t execute query”);

// display what the person searched for
echo “

You searched for: “” . $var . “”

”;

// begin to show results set
echo “Results”;
$count = 1 + $s ;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row[“1st_field”];

echo “$count.) $title” ;
$count++ ;
}

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

//break before paging
echo “
”;

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href="$PHP_SELF?s=$prevs&q=$var"><<
Prev 10&nbsp ";
}

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

// not last page so give NEXT link
$news=$s+$limit;

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

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo “

Showing results $b to $a of $numrows

”;

?>[/php]

[php]
$query = “SELECT * FROM search WHERE title LIKE ‘%$trimmed%’ ORDER BY title ASC”;
[/php]
:wink:

if you want the searches to be backwords then change order by title asc to desc (they mean ascending and descending

Thanks it sorta worked…lol it gives me a result but not what I want here is what it looks like.

You searched for: “swamp people”
Results1.) 2.)

Showing results 1 to 2 of 2

I need it to show the links to the pages the posts are on I have tried using all the different field names but same result. my sql Search has 4 fields ID, title,body,postid if this helps

well from what you are saying you only have four fields in the database:
ID, title,body,postid
so you never stored the links to the pages in the database so there is no possible query to write in which to get links from the database. If you are talking about, because you see the

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

in the script and you are wondering why are you not getting links to the posts, well that link is only for when there is more results than the limit which you set to 10, it will show them on a different page. but it will only show the result no links to anything because no links were in the database

actually I do have the links to the posts in the database the Table I used search doesnt have them but another one does so I will try changing tables and use that one to see if it works.

in the script above it shows to display the results but I want it to show the results to the pages containing the word they searched for. any help would be appreciated if you need to look at the search let me know and I can post the webpage I have it on.

what about adding a new field to the table search call it link, then start storing the links here, then you could change this part
[php]// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row[“1st_field”];

echo “$count.) $title” ;
$count++ ;
}[/php]

to:

[php]

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row[“1st_field”];
$link = $row[‘link’];

echo “$count.) $title” ;
$count++ ;
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service