Forum Moderators: coopster
I was hoping someone could help me with this.
Im trying to automatically fill a dropdown list with data from MYSQL table.
Sizes Table
--------
ProductId ¦ size1 ¦ size2 ¦ size3 ¦ size4 ¦ size4 ¦ size6 ¦
* the 'size' rows hold a numeric value for the quantity I have of each
I was hoping to only list the sizes that have a greater number than 0.
I have used an auto fill select code on another project but I'm just not sure how to fill in the blanks.
$query = mysql_query("SELECT * FROM sizes WHERE `productCode`='$productCode' AND? size1 23456 >= 1 "); // autofill dropdown list 'select' with options from DB
$r = mysql_fetch_array($query);
echo "<select name=option1>";
for(?)
{
echo "<option value=?>?</option>";
}
echo "</select>";
Any help would be GREATLY apppreciated. :)
$query = mysql_query("SELECT * FROM sizes WHERE `productCode`='$productCode'");
$r = mysql_fetch_array($query);echo "<select name=option1>";
for ($i=1; $i<7; $i++)
{if ($r['size'.$i]>0)
{
echo "<option value='".$r['size'.$i]."'>$r['size'.$i]</option>";
}}
echo "</select>";
Not tested, but should work ok I think.
dc
I got a parse error on that one, somewhere here:
echo "<option value='".$r['size'.$i]."'>$r['size'.$i]</option>";
Will your code fill my select box if it finds a matching productId, listing the data held in each size column if the numeric value is over 0?
ProductId ¦ size1 ¦ size2 ¦ size3 ¦ size4 ¦ size4 ¦ size6 ¦
I am using a content management system (custom built) to add products to the website. I am using another table called 'products' this contains rows like:
Id (auto inc.)
Product id
name
price
description
date
etc
On the page / script that I use to add the products to the DB, I have options for sizes and quantity i have in stock for each size:
Size 1 - (input field for quantity)
Size 2 - ("")
Size 3 - ("")
Size 4 - ("")
Size 5 - ("")
Size 6 - "")
My script currently adds the size/qty data above into the sizes table and the rest of the data into the products table.
Any ideas on how I should best do this?
if ($r['size'.$i]>0)
{
$current = $r['size'].$i;
echo "<option value=\"".$current."\">$current</option>";
}
Maybe that will help.
The other suggestion is missing a period (otherwise the quote is in the wrong place) ;)
I think I may have structured my table the wrong way for what I need :(
What I wanted it to output was the size names into the dropdown list.
The names of the sizes arent listed anywhere except in the name of the rows:
size1 ¦ size2 ¦ size3 ¦ size4 ¦ size4 ¦ size6 ¦
The code you helped me with outputted the QTY of each size I have available.
Is this the cleanest way I can do this?
[PHP]<select name=option1>
<?
$query = mysql_query("SELECT * FROM sizes WHERE `productCode`='$productCode'");
$r = mysql_fetch_array($query);
$size1 = $r["size1"];
$size2 = $r["size2"];
$size3 = $r["size3"];
$size4 = $r["size4"];
$size5 = $r["size5"];
$size6 = $r["size6"];
if ($size1 >= 1) {
echo "<option value='size1'>Size 1</option>";
}
else {
echo "";
}
if ($size2 >= 1) {
echo "<option value='size2'>Size 2</option>";
}
else {
echo "";
}
if ($size3 >= 1) {
echo "<option value='size3'>Size 3</option>";
}
else {
echo "";
}
if ($size4 >= 1) {
echo "<option value='size4'>Size 4</option>";
}
else {
echo "";
}
if ($size5 >= 1) {
echo "<option value='size5'>Size 5</option>";
}
else {
echo "";
}
if ($size6 >= 1) {
echo "<option value='size6'>Size 6</option>";
}
else {
echo "";
}
?>
</select>[/PHP]
<?
$query = mysql_query("SELECT * FROM sizes WHERE `productCode`='$productCode'");
$r = mysql_fetch_array($query);
$size1 = $r["size1"];
$size2 = $r["size2"];
$size3 = $r["size3"];
$size4 = $r["size4"];
$size5 = $r["size5"];
$size6 = $r["size6"];
echo '<select name=option1>';
$counter = 1;
while(isset(${'size' . $counter})) {
if (${'size' . $counter} >= 1) {
echo "<option value='size",$counter,"'>Size ",$counter,"</option>";
} else {
echo "";
}
$counter++;
}
echo '</select>';
?>
you could also set the while to be explicit
while($counter <= 6) {
that should work, didn't test it though