Jump to content

list/menu passing to another


bcraig

Recommended Posts

How do i get a list/menu to display records form DB depending on the selection on a previous list/menu?

 

The first list has main categories and  the second list has sub categories conected to the main

 

Heres a small example of my db table:

categoies

cat_id    |  parent_id  |  cat_name

    1                0            MainOne

    2                0            MainTwo

    3                1            SubOneA

    4                1            SubOneB

    5                2            SubTwoA

    6                2            SubTwoB

 

 

this is what i have for the first list

 

 

<?php
$mysqli = mysqli_connect("localhost", "root", "", "jobsnz");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {

$sqlcat_drop = "SELECT cat_name FROM categories WHERE parent_id = 0";
$rescat_drop = mysqli_query($mysqli, $sqlcat_drop);
?>
<select name="categories">
<?php
if ($rescat_drop) {
	while ($newArray = mysqli_fetch_array($rescat_drop, MYSQLI_ASSOC)) {
		$catname  = $newArray['cat_name'];
		echo "<option>".$catname."</option>";
   	}
} else {
	printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
}
?>	
</select>

Link to comment
Share on other sites

1. Change your first query so that it gets the cat_id as well as the category name.

2. Change this

$catname  = $newArray['cat_name'];

echo "<option>".$catname."</option>";

 

to something like

 

$categoryId = $newArray['cat_id'];
echo "<option value="$categoryId">$catname</option>";

 

3. Set up a condition in the appropriate place to test if your categories drop down has been posted back. (I presume you've got that select inside a form that can be submitted!).

if (isset($_POST['categories']) and intval($_POST['categories']) > 0) {
    
   $parentId = intval($_POST['categories']);

  //run query to get records where parent_id = $parentId
// so if user chooses MainOne, $parentId = 1 and the resulting query should return records 3 & 4

}

4. display new records in much the same way you displayed the first set....

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.