PDA

View Full Version : php search code


red_rose
03-11-2007, 08:15 AM
hello there

i need some help on creating a search page. i have a dropdown menu which asks the user to select a search category and once user selects one, the value will populate a second dropdown next to it so that user can select a subcategory to search. then below the dropdowns there is an input text where user can type in their search item according to the categories they selected above.

then php coding will take all the values and search in the particular tables in the db. my problem is : nothing is displayed when i click the search button. i don't know what i did wrong. i need some help from those who know how to solve this.

search.php :


<html>
<head>
<title>:: Search ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>

<table width="100%">
<tr><td colspan="2"><?php include_once("title.inc"); ?></td></tr>
<tr><td width="20%" valign="top"><?php include_once("links.inc"); ?></td>
<td valign="top">

<form name="form1" method="post" action="search.php">
<p>Search :
<select name="category" onChange="document.forms[0].submit()">
<option value="">[Select a category]</option>
<option value="Accommodation" <?php if(!(strcmp("Accommodation", $category))){echo "selected";}?>>Accommodation</option>
<option value="Student" <?php if(!(strcmp("Student", $category))){echo "selected";}?>>Student</option>
<option value="Landlord" <?php if(!(strcmp("Landlord", $category))){echo "selected";}?>>Landlord</option>
<option value="Institution" <?php if(!(strcmp("Institution", $category))){echo "selected";}?>>Institution</option>
</select>

<select name="subcategory">
<option value="">[Select a subcategory]</option>
<?php do { ?>
<option value="<?php echo @$row['subctgyID']; ?>"><?php echo @$row['subCategory']; ?></option>
<?php }while ($row = @mysql_fetch_array(@$result)); ?>
</select>
</p>
<p>Search term :
<input name="searchterm" type="text" id="searchterm" value="<?php echo $searchterm; ?>">

</p>
<p><input name="search" type="submit" id="search" value="Search">


</form>
</td>
</tr>
<tr><td colspan="2"><?php include_once("footer.inc"); ?></td></tr>
</table>
</body>
</html>




below is the php coding to execute the search but i don't know where to put this code. i want the table to be displayed under the search button. i tried in many ways but didn't work.


if(isset($_POST['search'])) {
if($category == "Accommodation") {
if(@$subcategory == "By Location") {
$result = mysql_query("select * from accommodation where City like '%".$searchterm."%'");
$row = mysql_fetch_array($result);

if($row) {
do {
echo "<table>";
echo "<tr>";
echo '<td>Type</td>';
echo '<td>Street</td>';
echo '<td>City</td>';
echo '<td>State</td>';
echo '<td>Price (RM)</td>';
echo '<td>Facilities</td>';
echo '<td>Status</td>';
echo '<td>Landlord</td>';
echo "</tr>";
echo "<tr>";
echo '<td>'.$row['Type'].'</td>';
echo '<td>'.$row['Street'].'</td>';
echo '<td>'.$row['City'].'</td>';
echo '<td>'.$row['State'].'</td>';
echo '<td>'.$row['Price'].'</td>';
echo '<td>'.$row['Facilities'].'</td>';
echo '<td>'.$row['Status'].'</td>';
echo '<td>'.$row['LdlordUsername'].'</td>';
echo "</tr>";
echo "</table>";
} while($row = mysql_fetch_array($result));
} else echo "No results found"; } }
}


personally i believe that this is a poor type of coding because i'm using many if else statements and for each category i will have so many if else statements and i will have to repeat the table echo-ing each time there is a query to display data. the code above is showing just one example of the search category, altogether i have 4 categories and each categories have its own subcategories.

i'm a student, still learning php, so that's why i'm asking for some help and guidance to improve my coding style so that i don't have to repeat the coding many times.

any help is appreciated. thank you so much.

davidj
03-12-2007, 11:29 AM
first of all rose your developing a habit where you are missing obvious mistakes for some one of your level...

} else echo "No results found"; } } // << INVALID ELSE

your nested IF's are not needed


if(isset($_POST['search'])) {
if($category == "Accommodation") {
if(@$subcategory == "By Location") {
this is valid and is better (one IF)


if (isset($_POST['search']) && $category == "Accommodation" && $subcategory == "By Location"){


also your way of producing dynamic HTML would be a nightmare to administer

instead of echoing every tag like your doing here...

if($row) {
do {
echo "<table>";
echo "<tr>";
echo '<td>Type</td>';
echo '<td>Street</td>';
echo '<td>City</td>';
echo '<td>State</td>';
echo '<td>Price (RM)</td>';
echo '<td>Facilities</td>';
echo '<td>Status</td>';
echo '<td>Landlord</td>';
echo "</tr>";
echo "<tr>";
echo '<td>'.$row['Type'].'</td>';
echo '<td>'.$row['Street'].'</td>';
echo '<td>'.$row['City'].'</td>';
echo '<td>'.$row['State'].'</td>';
echo '<td>'.$row['Price'].'</td>';
echo '<td>'.$row['Facilities'].'</td>';
echo '<td>'.$row['Status'].'</td>';
echo '<td>'.$row['LdlordUsername'].'</td>';
echo "</tr>";
echo "</table>";
} while($row = mysql_fetch_array($result));
just do this..


<?php if($row) {
do { ?>
<table width="45%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<?php } while($row = mysql_fetch_array($result)); ?>

notice how i terminate the php before writing the HTML and i dont have to echo every line. If you do it like this then the table will render correctly in Dreamweaver's design view also

red_rose
03-12-2007, 05:52 PM
dj, thank you so much for your help. i am now trying to follow your style

there is a problem :

i have two dropdowns, $category and $subcategory

when i select both dropdowns and try echoing the value, i'm getting the wrong value

for example, i have a category : institution and it's subcategory :by name
:by location

let say i select institution and by name, the result i can see is "institution and institution" instead of "institution and by name"

i think this is because of :


<select name="category" onChange="document.forms[0].submit()">
<option value="">[Select a category]</option>
<option value="Accommodation" <?php if(!(strcmp("Accommodation", $category))){echo "selected";}?>>Accommodation</option>
<option value="Student" <?php if(!(strcmp("Student", $category))){echo "selected";}?>>Student</option>
<option value="Landlord" <?php if(!(strcmp("Landlord", $category))){echo "selected";}?>>Landlord</option>
<option value="Institution" <?php if(!(strcmp("Institution", $category))){echo "selected";}?>>Institution</option>
</select>

<select name="subcategory">
<option value="">[Select a subcategory]</option>
<?php do { ?>
<option value="<?php echo $row['subctgyID']; ?>"><?php echo $row['subCategory']; ?></option>
<?php }while ($row = mysql_fetch_array($result)); ?>
</select>


see this line : <option value="<?php echo @$row['subctgyID']; ?>"><?php echo @$row['subCategory']; ?></option>

how do i modify this so that i get the value of $row['subCategory']; and not $row['subctgyID'];

i really need help on this. thank you so much dj

red_rose
03-15-2007, 11:39 PM
anyone know how to solve this?