*** Disclaimer, If you are the type of person who thinks the internet owes you everything then this post is probably not for you. Read a book for a change ***
Just adding these disclaimers, as this blog is more my thought process as I am teaching myself php. I make no claims for anything being the best or right way to do things.
One of the main reason for these posts, is so I cam easily copy pasta bits no matter where I am.
Anyhow, going back to my recipe section on my site that I'm building, I want on the form a multiple select box for the ingredients. I have the following table on my database
CREATE TABLE IF NOT EXISTS `ingredients` (
`iId` int(11) NOT NULL auto_increment,
`type` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`notes` varchar(255) NOT NULL,
PRIMARY KEY (`iId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=31 ;
Just a note about why I have type in my ingredients. This is one of my bugbears with a lot of recipe listingsis they never group the ingredients by type. I am including type so that you can copy down the recipe and go to the supermarket and be less likely to miss anything off.
The Function for the multibox select is as follows
Function listIngredients()
{
$html ='<select name="Ingr[]" multiple="multiple">';
$sql=" SELECT `iId` , `name`
FROM `ingredients`
ORDER BY TYPE , name
LIMIT 0 , 30 ";
$result = mysql_query($sql)
or die(mysql_error());
if (mysql_num_rows($result) == 0)
{
echo "<em>No created.</em>";
}
else
{
while ($row = mysql_fetch_array($result))
{
$html .= '<option value='. $row['rId'].'>'.$row['name'].'</option>';
}
}
$html .= '</select>';
echo $html;
}
The blog behind 5YLAC.com and thedeadadventurersclub.com
May also contain the odd ramble, the odd review and whatever else I feel like throwing up here. If you are a grammar w*nker, then this site is not for you.
Showing posts with label Dropdown. Show all posts
Showing posts with label Dropdown. Show all posts
Thursday, 12 November 2009
Monday, 9 November 2009
Adding items to a dropdown from a MYSQL Database
*** Disclaimer I do not know if this is the best method or is even correct, if you are the type of person who reads things on the internet, tries them out and then gets annoyed when things don't work out. Then maybe this post is not for you, go outside instead and enjoy some fresh air ***
My next php project is, I'm building a small recipe section for my chance1234.com site. I've started on working on a form to allow myself to add recipes to the database.
One of the options I want when adding recipes is to be able to clarify recipes by type. ie pasta dishes, soups etc
What I did was set up a table on mysql database called rtype
CREATE TABLE IF NOT EXISTS `rType` (
`rId` int(11) NOT NULL auto_increment,
`type` varchar(255) NOT NULL,
`Notes` varchar(255) NOT NULL,
PRIMARY KEY (`rId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
Then on my php file, I created the following function
Function listRecipesTypes()
{
$html ='<select name="'.$name.'">';
$sql="SELECT rID,type FROM rType";
$result = mysql_query($sql)
or die(mysql_error());
if (mysql_num_rows($result) == 0)
{
echo "<em>No created.</em>";
}
else
{
while ($row = mysql_fetch_array($result))
{
$html .= '<option value='. $row['rId'].'>'.$row['type'].'</option>';
}
}
$html .= '</select>';
echo $html;
}
?>
I adapted the code from a Make a Website post. Ideally what I want to do is adapt the function so I can chuck any array at it.
My next php project is, I'm building a small recipe section for my chance1234.com site. I've started on working on a form to allow myself to add recipes to the database.
One of the options I want when adding recipes is to be able to clarify recipes by type. ie pasta dishes, soups etc
What I did was set up a table on mysql database called rtype
CREATE TABLE IF NOT EXISTS `rType` (
`rId` int(11) NOT NULL auto_increment,
`type` varchar(255) NOT NULL,
`Notes` varchar(255) NOT NULL,
PRIMARY KEY (`rId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
Then on my php file, I created the following function
Function listRecipesTypes()
{
$html ='<select name="'.$name.'">';
$sql="SELECT rID,type FROM rType";
$result = mysql_query($sql)
or die(mysql_error());
if (mysql_num_rows($result) == 0)
{
echo "<em>No created.</em>";
}
else
{
while ($row = mysql_fetch_array($result))
{
$html .= '<option value='. $row['rId'].'>'.$row['type'].'</option>';
}
}
$html .= '</select>';
echo $html;
}
?>
I adapted the code from a Make a Website post. Ideally what I want to do is adapt the function so I can chuck any array at it.
Subscribe to:
Posts (Atom)