Hi,

I need to populate a drop down box with size and colour values from my db. I have got it working but it just seems to be too clunky. I'm going to be marked on it and so would like to know where I'm going wrong. What I do not like is that I loop through the array and then need to tell it to go one more time.

Thanks in advance.

$query3 = "SELECT size, in_stock, colour, itemline_id
  		  			 FROM itemline
  		  			 WHERE item_id = '" . $row['item_id'] . "'";
  $result3 = mysql_query($query3);
  $row3 = mysql_fetch_array($result3);

    echo "test" . mysql_num_rows($result3);
	echo "<form method='post' action='ppp.php'><select name='id'>";
$size = $row3['size'];
		$colour = $row3['colour'];
		$in_stock = $row3['in_stock'];
    	$id = $row3['itemline_id'];
	while ($row3 = mysql_fetch_array($result3)) {


		if ($in_stock != 0){
    		echo "<option value='" . $id . "'>Size: " . $size . " -- Colour: " . $colour . " -- In Stock: " . $in_stock . "</option>";
   		}
		else{
       		echo "<option disabled='disabled'>Size: " . $size . " -- Colour: " . $colour . " -- OUT OF STOCK</option>";
   		}
   		$size = $row3['size'];
				$colour = $row3['colour'];
				$in_stock = $row3['in_stock'];
    	$id = $row3['itemline_id'];

   }
   if ($in_stock != 0){
       		echo "<option value='" . $id . "'>Size: " . $size . " -- Colour: " . $colour . " -- In Stock: " . $in_stock . "</option>";
      		}
   		else{
          		echo "<option disabled='disabled'>Size: " . $size . " -- Colour: " . $colour . " -- OUT OF STOCK</option>";
   		}



   echo "</select><input name='action' type='hidden' value='addtocart' /><input type='hidden' name='id' value ='".$row['item_id']."' /><p><input type='submit' value='Add to basket' ></p></form>";

The values you are taking from db should be with in the loop. not out side and why are you taking db objects two time

$query3 = "SELECT size, in_stock, colour, itemline_id
  		  			 FROM itemline
  		  			 WHERE item_id = '" . $row['item_id'] . "'";
  $result3 = mysql_query($query3);
  $row3 = mysql_fetch_array($result3);

    echo "test" . mysql_num_rows($result3);
	echo "<form method='post' action='ppp.php'><select name='id'>";

                $size = $row3['size'];
		$colour = $row3['colour'];
		$in_stock = $row3['in_stock'];
    	        $id = $row3['itemline_id'];
	while ($row3 = mysql_fetch_array($result3)) {


		if ($in_stock != 0){
    		echo "<option value='" . $id . "'>Size: " . $size . " -- Colour: " . $colour . " -- In Stock: " . $in_stock . "</option>";
   		}
		else{
       		echo "<option disabled='disabled'>Size: " . $size . " -- Colour: " . $colour . " -- OUT OF STOCK</option>";
   		}
   		$size = $row3['size'];
				$colour = $row3['colour'];
				$in_stock = $row3['in_stock'];
    	$id = $row3['itemline_id'];

   }

   if ($in_stock != 0){
       		echo "<option value='" . $id . "'>Size: " . $size . " -- Colour: " . $colour . " -- In Stock: " . $in_stock . "</option>";
      		}
   		else{
          		echo "<option disabled='disabled'>Size: " . $size . " -- Colour: " . $colour . " -- OUT OF STOCK</option>";
   		}



   echo "</select><input name='action' type='hidden' value='addtocart' /><input type='hidden' name='id' value ='".$row['item_id']."' /><p><input type='submit' value='Add to basket' ></p></form>";

you are taking them out side the while loop

$size = $row3['size'];
$colour = $row3['colour'];
$in_stock = $row3['in_stock'];
$id = $row3['itemline_id'];

try it for while loop

$query3 = "SELECT size, in_stock, colour, itemline_id
  		  			 FROM itemline
  		  			 WHERE item_id = '" . $row['item_id'] . "'";
  $result3 = mysql_query($query3);
  $row3 = mysql_fetch_array($result3);

    echo "test" . mysql_num_rows($result3);
	echo "<form method='post' action='ppp.php'><select name='id'>";

	while ($row3 = mysql_fetch_array($result3)) {
                $size = $row3['size'];
		$colour = $row3['colour'];
		$in_stock = $row3['in_stock'];
    	        $id = $row3['itemline_id'];

		if ($in_stock != 0){
    		echo "<option value='" . $id . "'>Size: " . $size . " -- Colour: " . $colour . " -- In Stock: " . $in_stock . "</option>";
   		}
		else{
       		echo "<option disabled='disabled'>Size: " . $size . " -- Colour: " . $colour . " -- OUT OF STOCK</option>";
   		}
   	
   }



   echo "</select><input name='action' type='hidden' value='addtocart' /><input type='hidden' name='id' value ='".$row['item_id']."' /><p><input type='submit' value='Add to basket' ></p></form>";

try it for while loop

Hi,

The reason I have stuff twice is because it doesn't work otherwise. I pasted your code and it leaves out a value. That's the problem I had in the first place. The only way I can get that last value is by doing it once more outside of the loop.

Cheers,

Roy

I just checked the script and very buggy in my opinion. I've debuged it and is as follows:

$query3 = "SELECT size, in_stock, colour, itemline_id FROM itemline WHERE item_id = '" . mysql_real_escape_string($row['item_id']) . "'";
  $result3 = mysql_query($query3) or die(mysql_error());
  
    echo "test" . mysql_num_rows($result3);
	echo "<form method='post' action='ppp.php'><select name='id'>";
	while ($row3 = mysql_fetch_array($result3)) {
        $size = $row3['size'];
		$colour = $row3['colour'];
		$in_stock = $row3['in_stock'];
    	$id = $row3['itemline_id'];

		if ($in_stock != 0){
    		echo "<option value='" . $id . "'>Size: " . $size . " -- Colour: " . $colour . " -- In Stock: " . $in_stock . "</option>";
   		}
		else{
       		echo "<option disabled='disabled'>Size: " . $size . " -- Colour: " . $colour . " -- OUT OF STOCK</option>";
   		}
   		
   }
   if ($in_stock != 0){
       		echo "<option value='" . $id . "'>Size: " . $size . " -- Colour: " . $colour . " -- In Stock: " . $in_stock . "</option>";
      		}
   		else{
          		echo "<option disabled='disabled'>Size: " . $size . " -- Colour: " . $colour . " -- OUT OF STOCK</option>";
   		}

   echo "</select><input name='action' type='hidden' value='addtocart' /><input type='hidden' name='id' value ='".$row['item_id']."' /><p><input type='submit' value='Add to basket' ></p></form>";

I just checked the script and very buggy in my opinion. I've debuged it and is as follows:

No doubt it's buggy, mate. This is my first PHP project so I'm sure its full of them!

Well, you're code did the trick. You are now on my Xmas card list:icon_smile: Just one thing in case anyone else reads your code and wonders, you left in the extra run through the array. Take that out and voila! I shall now take the time to compare my code and yours to see where I was going wrong.

Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.