RSS Forums RSS
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 7544 | Replies: 4
Reply
Join Date: Jun 2004
Posts: 11
Reputation: charter is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
charter charter is offline Offline
Newbie Poster

searching multiple fields

  #1  
Jun 19th, 2004
I have a form in my search.php page and want to know how to search on more than one field in my Mysql Database to give me the result.

This is my form for searching one field:

<form action="results.php" method="post">
Search:&nbsp;<select name="searchtype">
<option value="date">Date</option>
<option value="wk no.">Wk #</option>
<option value="year">Year</option>
<option value="artist">Artist</option>
<option value="title">Title</option>

</select>&nbsp;&nbsp;<input name="searchterm" type="text">&nbsp;
<INPUT type=image src="search.gif" value="Search"></div>
</form>

The thing is I wish to search on both the wk no. field and theyear field to give the result I want.

The think is that the Year is on multiple records (about 50 records) the Wk No. is the same on only 10 records in the database then the Wk No. changes with the Year staying the same.

This means that if I can search on both the Year and Wk No. it would give me the 10 records that match both criteria.

If I search on just the Year it gives me all 50 records for the year regardless of Wk No.
Or if I search on Wk No. it gives me all the records with the same WK No. in all the different years.

So if anyone can please amend my form code to do this it would be very much appreciated.
Last edited by charter : Jun 19th, 2004 at 4:44 pm. Reason: email notification
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2004
Location: In a house
Posts: 94
Reputation: Ragnarok is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Ragnarok Ragnarok is offline Offline
Junior Poster in Training

Re: searching multiple fields

  #2  
Jun 20th, 2004
The SQL would look something like this:

"SELECT * FROM table WHERE wkno LIKE '%".$_POST['searchterm']."%' OR year LIKE '%".$_POST['searchterm']."%'"
Reply With Quote  
Join Date: Jun 2004
Posts: 11
Reputation: charter is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
charter charter is offline Offline
Newbie Poster

Re: searching multiple fields

  #3  
Jun 20th, 2004
Originally Posted by Ragnarok
The SQL would look something like this:

"SELECT * FROM table WHERE wkno LIKE '%".$_POST['searchterm']."%' OR year LIKE '%".$_POST['searchterm']."%'"

Sorry but this isn't what I am looking for.

I want a page with 2 search boxes (the code I gave was for 1 box).

The data base includes 2 fields 1 for wk no and 1 for year.

I want to search in 1 box for the wk no and to search in the other box for the year.

Then for the result to match the wk no. and the year.

This means having 2 search box forms on one page but one button to search for the criteria
Reply With Quote  
Join Date: Jun 2004
Posts: 11
Reputation: charter is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
charter charter is offline Offline
Newbie Poster

Re: searching multiple fields

  #4  
Jun 20th, 2004
Thanks Ragnarok but i'm a newbie to PHP/MySQL

Here's my code for the results page:

This is the full code I have on the results.php page:

<?php
// create short variable names

if(ini_get('register_globals') != "1")
{
$post = (floatval(substr(phpversion(), 0, 3)) >= 4.1) ? $_POST : $HTTP_POST_VARS;

foreach($post as $key => $value)
{
$$key = $value;
}
}
$searchtype = addslashes(trim($searchtype));
$searchterm = addslashes(trim($searchterm));

if(!$searchtype || !$searchterm)
{
exit('You have not entered search details. Please go back and try again.');
}
$link_id = @mysql_connect("localhost", "root", "password");

if($link_id === false)
{
exit("Error, Could not connect to the system database. Sorry...");
}
mysql_select_db('charts50', $link_id);
$result = mysql_query("SELECT * FROM `uk` WHERE `{$searchtype}` LIKE '%{$searchterm}%'");
?>
</head>

<body background="0000ff">
<div align="center">

<table class="table" width="760" height="266" border="0" cellpadding="5" cellspacing="0">

<tr>
<td height="29" class="years-ago">50 YEARS AGO</td>
</tr>
<tr align="left">
<td height="29" colspan="2" class="years-ago">
<table width="100%" border="0" cellspacing="0" cellpadding="1">
<tr class="headings">
<tr>
<th class="headings" width="28">TW</th>
<th class="headings" width="53">LW</th>
<th class="headings" width="28">Wks</th>
<th class="headings" width="340">Title</th>
<th class="headings" width="307">Artist</th>

</tr>
<?php $i = 1; while($row = mysql_fetch_array($result)){

$item = array();
$item['tw'] = stripslashes($row['tw']);
$item['lw'] = stripslashes($row['lw']);

// now we can override that

$item['lw'] = ($item['lw']=='new')
? "<img src=\"images/new2.gif\" alt=\"new!\" />"
: $item['lw'];


$item['wks'] = stripslashes($row['wks']);
$item['title'] = strtoupper(stripslashes($row['title']));
$item['artist'] = stripslashes($row['artist']);
?>


<tr>
<td width="28" class="tw"><?php echo $item['tw']; ?></td>
<td width="53" class="lw"><?php echo $item['lw']; ?></td>
<td width="28" class="wks"><?php echo $item['wks']; ?></td>
<td width="340" class="title"><?php echo $item['title']; ?></td>
<td width="307" class="artist"><?php echo $item['artist']; ?></td>
</tr>
<?php $i++; } ?>
</table> </td>
</tr>
</table>

What I want is a AND situation. I wish to enter data for 2 fields into 2 search boxes on the page (field 1 is wk no and field 2 is year) I want the result to find the record (rows) which match both wk no AND year

I also need to know how to set up the search terms on the forms.
Many Thanks
Reply With Quote  
Join Date: Mar 2004
Location: In a house
Posts: 94
Reputation: Ragnarok is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Ragnarok Ragnarok is offline Offline
Junior Poster in Training

Re: searching multiple fields

  #5  
Jun 20th, 2004
use exactly the same query put use an AND insted of OR
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 9:36 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC