Jump to content

Drop down assistance


noobstar

Recommended Posts

Hi everyone,

I was wondering if anyone has a little time in showing me in how to make a drop down list which would contain data from a sql query from my database and it would insert this into the databse. I am not to advanced in PHP and Mysql but i have a general knowledge of it.
I have tried to find some tutorials that could show me this but they are either related to one thing not the other, i tried combining each but it only turned into disaster :(.

Basically i am tring to make a drop down whereby a user can choose a 'Category' of an item and on submit it would insert that chosen category along with some other details into my setup database.

It is fairly simple i know (im still a php noob when it comes to new things :S). If anyone can just give me a quick example or point me to a tutorial that explains this i would be Thankful :)
Link to comment
https://forums.phpfreaks.com/topic/28710-drop-down-assistance/
Share on other sites

Would the catagories come from a table or will it be an array you make up.

For database use
[code]<?php
echo "<select name=catagory>";
$sql = "SELECT id, name FROM catagories";
  $res = mysql_query($res) or die (mysql_error());
    while($r = mysql_fetch_assoc($res)){
      echo '<option value="'.$r['id'].'">'.$r['name'].'</option>';
    }
echo "</select>";
?>[/code]
If you want to insert the actual name of the catagory instead of the id then change the value above to $r['name']

For manual array use with id
[code]<?php
echo "<select name=catagory>";
$cat = array(1 => "cat1", 2 => "cat2"); //if just want to insert name then use next line one or the other ONLY
foreach($cat as $key => $val){
   echo '<option value="'.$key.'">'.$val.'</option>';
}
echo "</select>";
?>[/code]

For manual array with no id
[code]<?php
echo "<select name=catagory>";
$cat = array("cat1", "cat2", "cat3");
foreach($cat as $val){
   echo '<option value="'.$val.'">'.$val.'</option>';
}
echo "</select>";
?>[/code]

Ray

Link to comment
https://forums.phpfreaks.com/topic/28710-drop-down-assistance/#findComment-131527
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.