Jump to content

FOREACH Help


trichards

Recommended Posts

I am trying to combine the size and quantity array as a selection dropdown that displays (Size - Quantity) Ex: (Small - 10).  Can I get some help writing the foreach loop.

 

Table:

Sizes: small,medium,large,xl

Quantity: 10,10,0,10

 

Code:

$q = mysql_query("SELECT * FROM products WHERE id='3'"); 
$r = mysql_fetch_array($q);

// Get Sizes
$sizes = $r['size'];
$explode_size = explode(",", $sizes);

// Get Quantity
$quantity = $r['in_stock'];
$explode_quantity = explode(",", $quantity);

foreach ($explode_size as $sizes) && foreach ($explode_quantity as $quantity) {
echo "<option value=\"\">".$sizes." - ".$quantity."</option>" 
}

Link to comment
https://forums.phpfreaks.com/topic/201883-foreach-help/
Share on other sites

FIXED:

Used array_combine

 

<select name=""> 
<? 
$q = mysql_query("SELECT * FROM products WHERE id='3'"); 
$r = mysql_fetch_array($q);

$sizes = $r['size'];
$explode = explode(",", $sizes);

$quantity = $r['in_stock'];
$explodeq = explode(",", $quantity);

$array = array_combine($explode, $explodeq);
//print_r($array);

foreach ($array as $explode => $explodeq)  { ?>

    <option value="<? echo $explodeq; ?>"><? echo $explode; ?> - <? echo $explodeq; ?></option>
    
<? } ?>
</select>

 

 

Link to comment
https://forums.phpfreaks.com/topic/201883-foreach-help/#findComment-1058834
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.