Reputation: 43
Why doesn't this code work when i call it via function? It only works for the outer loop but the inner loop does not return values?
<?php populateEnrollment($products); ?>
<?php
function populateEnrollment($value){
foreach($value as $productid => $prod) if ($productid ==101)
{
echo '<tr>';
echo '<td width=300 class="tah11">'. $prod["name"] .'</td>' ;
echo '<td width=100 class="tah11"><div align="center"> <select name="enrollName"id="enrollNameId" >';
foreach ($prod["membershipType"] as $type)
{
echo '<option value="' .$type["price"]; $typeIdPrice = $typeId["price"] .'">'.$type["name"] . ' at $' . $typeIdPrice . '</option>';
echo '</select>';
echo '</td>';
} // end of foreach membershipTypen
} // end of products foreach
} // end of function populateEnrollment
?>
Upvotes: 0
Views: 137
Reputation: 3855
Please add echo </select></td>
out side your inner foreach loop.
Also the way you are echoing <option>
is also wrong please check here
Changed $typeId to $type .. minor typo update
<?php
function populateEnrollment($value){
foreach($value as $productid => $prod) if ($productid ==101){
echo '<tr>';
echo '<td width=300 class="tah11">'. $prod["name"] .'</td>' ;
echo '<td width=100 class="tah11"><div align="center"> <select name="enrollName"id="enrollNameId" >';
foreach ($prod["membershipType"] as $type){
echo '<option value="' .$type["price"] .'">'.$type["name"] . ' at $' . $type["price"] . '</option>';
} // end of foreach membershipTypen
echo '</select>';
echo '</td>';
} // end of products foreach
} // end of function populateEnrollment
?>
Upvotes: 2
Reputation: 19882
You can do it like this
<?php populateEnrollment($products); ?>
function populateEnrollment($value)
{
foreach($value as $productid => $prod)
{
if ($productid ==101)
{
?><tr><?php
?><td width=300 class="tah11"><?=$prod["name"]?></td><?php
?><td width=100 class="tah11"><div align="center">
<select name="enrollName"id="enrollNameId" >
<?php
foreach ($prod["membershipType"] as $type)
{
echo '<option value="' .$type["price"]; $typeIdPrice = $typeId["price"] .'">'.$type["name"] . ' at $' . $typeIdPrice . '</option>';
}
?></td><?php
?></tr><?php
}
}
}
Upvotes: 0