hane Posted January 28, 2009 Share Posted January 28, 2009 I need help once again I cant seem to get this to work.I've got 3 drop down menus that is populated from mysql database.Category, Subcategory and AreaYou select Category and Subcategory gets populated, Area is not related to Category & Subcategory.I want visitors to be able to select 'all' from drop downs and then all the info from database must be displayede.g Visitor select "Used Equipment" from Category then "All" from Sybcategory and "Area Name" from AreaResults are: All Used Equipment from Area Name.All=ID1 in all my tablesHere is drop down menu code: <script language=JavaScript>function reload(form){var val=form.cat.options[form.cat.options.selectedIndex].value; self.location='classified.php?cat=' + val;}</script> <?php$con = mysql_connect("*******","*****","******");if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("****", $con); $quer2=mysql_query("SELECT DISTINCT c_category_name,c_category_id FROM c_category order by c_category_name"); $cat=$_GET['cat']; if(isset($cat) and strlen($cat) > 0){ $quer=mysql_query("SELECT DISTINCT c_subcategory_name,c_subcategory_id FROM c_subcategory where c_category_id=$cat order by c_subcategory_name"); } else{$quer=mysql_query("SELECT DISTINCT c_subcategory_name, c_subcategory_id FROM c_subcategory order by c_subcategory_name"); } echo "<form method=post name=f1 action='classified_show.php'>"; echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select Category</option>"; while($noticia2 = mysql_fetch_array($quer2)) { if($noticia2['c_category_id']==@$cat){ echo "<option selected value='$noticia2[c_category_id]'>$noticia2[c_category_name]</option>"."<BR>";} else{echo "<option value='$noticia2[c_category_id]'>$noticia2[c_category_name]</option>";} } echo "</select>"; echo "<br /><br />"; echo "<select name='subcat'><option value=''>Select Subcategory</option>"; while($noticia = mysql_fetch_array($quer)) { echo "<option value='$noticia[c_subcategory_id]'>$noticia[c_subcategory_name]</option>"; } echo "</select>"; echo "<br /><br />"; $query="SELECT area_name,area_id FROM area order by area_name"; $result = mysql_query ($query); echo "<select name='area' > <option value=''>Select Area</option>"; while($nt=mysql_fetch_array($result)){ echo "<option value=$nt[area_id]>$nt[area_name]</option>"; } echo "</select>"; echo "<br /><br />"; echo "<input type=submit value=Submit>"; echo "</form>"; mysql_close($con);?> If you need the display code just let me know that I can post that too. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 28, 2009 Share Posted January 28, 2009 Why wouldn't it work already? If "all" has a database ID like any other option, if they chose "all" wouldn't it just use that ID and search in the database that way? Or do you need to detect if they selected all and change the SQL query? Link to comment Share on other sites More sharing options...
hane Posted January 28, 2009 Author Share Posted January 28, 2009 I'm not sure how to write the query for when "all" id_1 is selceted I'm very new to php started on sunday. I dont think the change have to be made in the code that i posted. I think that i have to write a if elseif statement in the display page. Here is the code for the display page. $cat=$_POST['cat'];$subcat=$_POST['subcat'];$area=$_POST['area'];//echo " cat=$cat <br> subcat=$subcat <br> area= $area ";$con = mysql_connect("****","***","****");if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("***", $con);$result = mysql_query("SELECT * FROM c_listing INNER JOIN c_company ON c_listing.c_company_id = c_company.c_company_id WHERE c_subcategory_id=$subcat AND area_id=$area");$count=0; while($row = mysql_fetch_array($result)) { echo "<table border='0' width='100%'> "; echo "<tr>"; echo "<td rowspan='4'>"; echo "<a href='classifieds/images/photo/" . $row['c_item_ref'] . ".jpg'><img src='classifieds/images/photo/" . $row['c_item_ref'] . ".jpg' width='150'></a>"; echo "</td>"; echo "<th>Description</th>"; echo "<th align='right'>Price: R " . $row['c_item_price'] . "</th>"; echo "<tr>"; echo "<td colspan='2'>" . $row['c_item_description'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td colspan='2'><b>Contact:</b> " . $row['c_company_contact_detail'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td colspan='2'>"; echo "<b>Email: </b>"; echo "<a href='mailto:" . $row['c_company_email'] . "?subject=Listing on Hefty Description: " . $row['c_item_description'] . " Price: R " . $row['c_item_price'] . "'>" . $row['c_company_email'] . "</a></td>"; echo "</tr>"; echo "<tr><td colspan='3'> </td></tr>";$count++; } if ($count==0) { echo 'Sorry no record was found.Please try again'; } echo "</table>";mysql_close($con);?> I really need some help Link to comment Share on other sites More sharing options...
justsomeguy Posted January 28, 2009 Share Posted January 28, 2009 Change this part:$result = mysql_query("SELECT * FROM c_listing INNER JOIN c_company ON c_listing.c_company_id = c_company.c_company_id WHERE c_subcategory_id=$subcat AND area_id=$area");to this: if ($subcat != "1"){ $result = mysql_query("SELECT * FROM c_listing INNER JOIN c_company ON c_listing.c_company_id = c_company.c_company_id WHERE c_subcategory_id=$subcat AND area_id=$area");}else{ $result = mysql_query("SELECT * FROM c_listing INNER JOIN c_company ON c_listing.c_company_id = c_company.c_company_id WHERE area_id=$area");} The second one leaves out the subcat part if the value is "1". Link to comment Share on other sites More sharing options...
hane Posted January 28, 2009 Author Share Posted January 28, 2009 Thanks now just one more thing. If i want to select subcat=all and area=all how must I change the code do I use elseif? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 28, 2009 Share Posted January 28, 2009 You need another if inside each block checking for area. if ($subcat ..){ if ($area ..) { } else { }}else{ if ($area ..) { } else { }} Link to comment Share on other sites More sharing options...
hane Posted January 29, 2009 Author Share Posted January 29, 2009 Thanks i would never have thought of that with my little know how. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.