• 0

[PHP] Search feature


Question

timsweb

Hey,

Im coding a search page for my site, here is the code so far:

<?php	  

mysql_connect("localhost","","") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

$search = $_GET['q'];

echo("
<form action=\"search.php\" method=\"get\">
<table>
<tr>
<td><input type=\"text\" name=\"q\" value=\"$search\" /></td></tr>
<tr>
<td><input type=\"submit\" value=\"Search!\" /></td></tr>
</table>
</form>
");

$search = trim($search);

if($search == "") {
echo("<font class=\"style5\"><b>Tip: </b>Use <b>%</b> to display all results.</font>
");
}

if($search=="") {
echo("");
}
else {


$result = mysql_query("SELECT * FROM news WHERE activated='1' AND title LIKE \"%$search%\"  
  ");

$numrows = mysql_num_rows($result);
if($numrows == 0) {
echo("
<font class=\"style5\">Your search for <b>".$search."</b> returned no results.</font>
");
}
else{
echo("
<p><font class=\"style5\">Your search for <b>".$search."</b> returned <b> ".$numrows."</b> result(s).</font></p>
");
}

while($row = mysql_fetch_array($result)) {
$page = $row['id'];
$page = "news.php?id=".$page."";
$pagetitle = $row['title'];

echo("
<font class=\"style5\"><a href=\"$page\">$pagetitle</a></font><br />
");
}
}

ok as you see so far it is very simple, it just searchs a table called news and returns the results which when clicked will go to news.php?id=$id, which is fine but i want it to be able to search through my tutorials table and my links table, and so when it shows the results it will change the link to links.php?id=$id or tutorials.php?id=$id depending on what it is,

Also i want to give the user options to choose to search between in either tutorials, links and news or all of them,

Any help is appricated,

Thanks, Tim

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0
Sir Topham Hatt

if an answer is found then i'd be interested :)

Link to post
Share on other sites
  • 0
jimbo11883

Run separate queries for each table.

News Results:

==========

1

2

3

Tutorials Results:

===========

1

2

3

etc...

Link to post
Share on other sites
  • 0
JoeC

Just run a switch/case statement assuming you have a column in the table which is either "links" OR "news" OR "tutorials". Example:

while ($r = mysql_fetch_assoc($result))
	{
	$page = $row['id'];
	$pageon = $row['page'] . ".php?id="; // This is either links, news or tutorials, in the DB
	$pagetitle = $row['title'];
.....

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.