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?
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?
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):Hope that helps, if you need something explained, just let me know.PHP Code:<?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 />';
}
}
}
?>
Thanks DD, you saved me countless times
That won't do anything unless the index.php is hooked up properly to the according variables.Code:echo '<a href="index.php?act=manage&action=view&id='.$r['id'].'"><b>'.$r['title'].'</b></a> - '.$r['date'].' - <i>'.$r['price'].'</i>
- Mike
Also, for a more productive search, why not try this in your sql query:
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).Code:$sql = "SELECT * FROM `jobs` WHERE `username` LIKE '%$data%' ORDER BY id DESC";
Hope this helps.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design
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
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design
What should the page index.php contain? Can you maybe provide an example if possible? I added:
before. Do I need to add anything else?PHP Code:<?php
$data = mysql_connect('localhost', '...', '...') or die (mysql_error ());
mysql_select_db ('...', $data) or die (mysql_error ());
?>
Last edited by ski; 12-03-2007 at 12:46 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.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design
Bookmarks