chained selects with one table
Posted
by
Owen
on Stack Overflow
See other posts from Stack Overflow
or by Owen
Published on 2012-10-13T03:32:30Z
Indexed on
2012/10/13
3:36 UTC
Read the original article
Hit count: 156
I know I am going about this in an unusual way, every tut I've seen uses multiple tables, but due to the way the rest of my site works I would like to create a chained select which operates using a single table.
My table structure is:
----------------------
|Catagory|SubCategory|
|01|cat1 |subcat1 |
|02|cat1 |subcat2 |
|03|cat2 |subcat1 |
|04|cat2 |subcat2 |
----------------------
The code I have so far looks like:
<tr>
<td class="shadow"><strong>Category:</strong> </td>
<td class="shadow">
<select id="category" name="category" style="width:150px">
<option selected="selected" value="<?php echo $category ?>"><?php echo $category?></option>
<?php
include('connect.php');
$result1 = mysql_query("SELECT DISTINCT category FROM categories")
or die(mysql_error());
while($row = mysql_fetch_array( $result1 )) {
$category = $row['category'];
echo "<option value='". $row['category'] ."'>". $row['category'] ."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td class="shadow"><strong>Sub Category:</strong> </td>
<td class="shadow">
<select id="sub_catgory" name="sub_category" style="width:150px;">
<option selected="selected" value="<?php echo $sub_category ?>"><?php echo $sub_category ?></option>
<?php
include('connect.php');
$result2 = mysql_query("SELECT sub_category FROM categories WHERE ")
or die(mysql_error());
while($row = mysql_fetch_array ($result2 )){
echo "<option value='" . $row['sub_category'] . "'>". $row['sub_category']. "</option>";
}
?>
</select>
</td>
</tr>
On the second select I am not sure how to state the WHERE clause. I need it to display the subcategories which have the same category as selected in the first select.
PART 2 how would I include AJAX in this to preload the data so i don't need to refresh the page.
Could someone either help me finish what I've started here or point me to a good tutorial.
thanks
© Stack Overflow or respective owner