Forum Moderators: coopster

Message Too Old, No Replies

Searching Variable Fields

PHP and MySQL

         

Maynard

11:21 am on Feb 26, 2004 (gmt 0)

10+ Year Member



Hi,

I want to design a search script in which users can choose which fields of a table they want to search. For example, one user may search for keyword "search term 1" in fields a, c, and f. Another user may search for keyword "search term 2" in fields f, l, m and v.

I have no idea how to do this in PHP but suspect it would involve the following: a) work out which fields were selected and store them in an array b) for each field stored in the array, do a SQL search.

I am a complete PHP newbie so any help and advice would be most welcome.

Thanks,
Maynard

henry0

12:47 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



here is a simple model
that can be reworked for register global OFF

<<<
// FORM

Chose a Beginning letter to Search by City and by Alpha</h4>
<p>
<form action ="resto_list_city_alpha.php" method="post">
<font color="red">Enter any Single Letter : </font><input type="text"name="letter" size="1" value="<?php echo $letter;?>">
<p>

//END OF FORM

<?php
// FILE NAME= resto_list_alpha.php
include "common_db.inc";

$percent="%";
$alpha=$letter.$percent;
$link_id = db_connect('your DB');
$result = mysql_query("SELECT restaurant, address, city, phone FROM stories_resto WHERE restaurant LIKE '$alpha' order by city ASC", $link_id) ;
echo "<td>";
echo "<table border=3, width=100%, align=center, valign=top>\n";

echo "<tr><td>";
printf ("<tr>");

Printf("<td width=150>"."<i><font color=80000><b>Restaurant</i></b></font>"."</td>");
Printf("<td width=250>"."<i><font color=80000><b>Address</i></b></font>"."</td>");
Printf("<td width=150>"."<i><font color=80000><b>City</i></b></font>"."</td>");
Printf("<td width=200>"."<i><font color=80000><b>Telephone</i></b></font>"."</td>"."</tr>");
while($query_data = mysql_fetch_array ($result) )
{

Printf("<td width=150> ".$query_data [ "restaurant" ]."</td>\n");
printf( "<td width=250> ".$query_data ["address"]."</td>\n" );
Printf("<td width=150> ".$query_data [ "city" ]."</td>\n");
printf( "<td width=200> ".$query_data ["phone"]."</td>\n"."</tr>\n" );

}

>>>

Maynard

3:52 pm on Feb 26, 2004 (gmt 0)

10+ Year Member



Hi Henry,

From your SELECT query I can't see where your multiple fields are:

SELECT restaurant, address, city, phone FROM stories_resto WHERE restaurant LIKE '$alpha' order by city ASC

I need my users to choose which fields they will search from and have my PHP script determine what those fields are and put them in a SELECT query, eg "SELECT * FROM table WHERE fieldC LIKE '%$keyword%' OR fieldG LIKE '%$keyword%' OR fieldJ LIKE '%$keyword%'";

Thanks, any other advice much appreciated.

Maynard.