Jump to content

Dropdown from mysql table


dropbop

Recommended Posts

Hi,

 

I don know how to explain this in words too well, so i have create 2 images to show what I need.

 

What I need to do is use whats in the first image (table.jpg)(the database) to create whats in the second image (dropdown.jpg)(the drop-down menu)

 

The results of the form will be entered into the database.  category_id will be the value thats inserted.

 

Many kind regards

 

Eoin

 

 

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Okay this is achieved using while loops,

 

here is the php code to do what you asked, i havn't done it to look like your Jpg but i have done it so it will show the categories and sub-categories in order.

 

hopefully you can modify this to your needs.

 

<?php

//This selects all rows where Parent_id = 0 and ORDERS by category_name
$parent_query = mysql_query("SELECT * FROM cat WHERE parent_id=0 ORDER BY category_name ASC");
while($parent=mysql_fetch_array($parent_query)){
    $parent_id = $parent['category_id'];
    echo $parent['category_name'];
    echo "<br/>";
    
    //This gets all the rows where the PARENT_ID = the parents category_id
    $child_query = mysql_query("SELECT * FROM cat WHERE parent_id=$parent_id ORDER BY category_name ASC");
while($child=mysql_fetch_array($child_query)){
    echo $child['category_name'];
    echo "<br/>";
}
echo "<br/>";
}

?>

 

Seany123

Link to comment
Share on other sites

Thanks you Seany,

 

I'm very grateful for your help. This looks like it could work perfectly.. now to get creating the dropdown... :)

 

When I get the dropdown working, I will post the code here in case anyone else comes across a similar issue.

 

Cheers.

Link to comment
Share on other sites

Ok, here is the finished product...

 

Using the code Seany provided (which I am very grateful for), I have created the dropdown as follows... Below is a jpg of the finished dropdpown

<select style="width:150px;" name="category">
<option value="">Select a Category</option>
<?php

//This selects all rows where Parent_id = 0 and ORDERS by category_name
$parent_query = mysql_query("SELECT * FROM categories WHERE parent_id=0 ORDER BY category_name ASC");
while($parent=mysql_fetch_array($parent_query)){
    $parent_id = $parent['category_id'];
$parent_name = $parent['category_name'];
echo "<option style=\"font-weight:bold;\">".$parent_name."</option>";
    
    //This gets all the rows where the PARENT_ID = the parents category_id
    $child_query = mysql_query("SELECT * FROM categories WHERE parent_id=$parent_id ORDER BY category_name ASC");
while($child=mysql_fetch_array($child_query)){
$child_id = $child['category_id'];
$child_name = $child['category_name'];

echo "<option style=\"padding-left:15px;\" value=\"".$child_id."\">".$child_name."</option>";
  }
}

?>
</select>

 

 

 

[attachment deleted by admin]

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.