Log in

View Full Version : Create A Simple Search Page



tomyknoker
03-29-2007, 02:20 AM
I am trying to create a simple search page which can search from a table in my database called 'tblmember', I want to have a filed where I can type antyhing and it will search the entire table but not sure how to achive this... Any ideas?

Titan85
03-29-2007, 09:59 PM
What you can do is create a script that searches for whatever field you want (ex: first name). The script will then search all the first names for a matching name. Here is a search script I made to search jobs by title or user. It will need to be changed around a bit to fit your needs, but here is what I have (sorry about the sloppy coding, made it a while back):
<?php

// If form is not active
if(!$_POST['search']) {
?>
<form method="post" action="">
<b>Search Jobs:</b>
<br />
<input type="text" name="search_field" class="input" />
<br />
Search By: <select name="searches" class="input">
<option value="title" selected="selected">Title</option>
<option value="username">Username</option>
</select>
<input type="submit" name="search" value="Search Jobs" />
</form>
<?
}

// If form is active
if($_POST['search']) {
$data = $_POST['search_field'];

// Check fields
if(!$search_field) {
echo '
<meta http-equiv="refresh" content="2;URL=index.php?act=search" />';

die('
<div class="message_urgent">
Please fill in something to search for
</div>
');
}

########## Get What To Search By ##########
// If search set to title
if($_POST['searches'] == "title") {
// Query to get results
$sql = "SELECT * FROM `jobs` WHERE title = '$data' ORDER BY id DESC";
$result = mysql_query($sql) or die ('Error Searching Jobs! <br />' .mysql_error());
$results = mysql_num_rows($result);
}
// If search set to username
if($_POST['searches'] == "username") {
$sql = "SELECT * FROM `jobs` WHERE username = '$data' ORDER BY id DESC";
$result = mysql_query($sql) or die ('Error Searching Jobs! <br />' .mysql_error());
$results = mysql_num_rows($result);
}
########## End Search Type Script ##########



// If there are no results
if($results < 1) {
echo '<span style="color: #FF0000">There were no jobs in the database with the title or username <b><i>'.$data.'</i></b></span>.
<br />
<a href="'.$_SERVER['REQUEST_URI'].'">Search Again</a>';
}

// If results are found
if($results >= 1) {
echo '
<div class="message">
Showing search results for <b>'.$_POST['search_field'].'</b> searched in <i>'.$_POST['searches'].'</i>
</div>
<br />
';

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

echo '<a href="index.php?act=manage&action=view&id='.$r['id'].'"><b>'.$r['title'].'</b></a> - '.$r['date'].' - <i>'.$r['price'].'</i>
<br />
'.$r['description'].'
<br /><br />';
}
}
}

?>Hope that helps, if you need something explained, just let me know.

mburt
03-29-2007, 10:02 PM
echo '<a href="index.php?act=manage&action=view&id='.$r['id'].'"><b>'.$r['title'].'</b></a> - '.$r['date'].' - <i>'.$r['price'].'</i>
That won't do anything unless the index.php is hooked up properly to the according variables.

Titan85
03-29-2007, 11:28 PM
That won't do anything unless the index.php is hooked up properly to the according variables.Yes, I didn't see a reason to post the view page or calling scripts because it wouldn't really help him out ;)

thetestingsite
03-30-2007, 04:10 PM
Also, for a more productive search, why not try this in your sql query:



$sql = "SELECT * FROM `jobs` WHERE `username` LIKE '&#37;$data%' ORDER BY id DESC";


That way, if the user types in ad in the text field, it will return any values with that in it (such as "adam", "mad","glad", etc).

Hope this helps.

gilanibusiness
03-30-2007, 06:14 PM
Dear Sir I want to learn PHP from the basic. I am a new comer in this forum. Please tell me the best website which teach me PHP step by step lessons.

Thanks

Allah Hafiz

Mubashar Gilani

boxxertrumps
03-30-2007, 07:55 PM
http://www.w3schools.com/php/default.asp

Have fun.

thetestingsite
03-30-2007, 07:58 PM
or http://www.php.net/tut.php

ski
12-03-2007, 12:38 AM
What should the page index.php contain? Can you maybe provide an example if possible? I added:

<?php
$data = mysql_connect('localhost', '...', '...') or die (mysql_error ());
mysql_select_db ('...', $data) or die (mysql_error ());

?>

before. Do I need to add anything else?

thetestingsite
12-03-2007, 01:52 AM
That all depends on what it is that you are trying to do exactly. If you are making a search, you also need to have a query to the "search" table in there.

Hope this helps.