0

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
?>
5
  • 1
    Try print_r($prod["membershipType"]); to check whether it contains anything Commented Dec 20, 2013 at 10:51
  • Functions are mainly used for repetitive codes, I don't think this code is repetitive so why use a function? Commented Dec 20, 2013 at 10:52
  • Have you debug anything so far ? Check your output for print_r($prod["membershipType"]); before starting inner loop. Commented Dec 20, 2013 at 10:52
  • echo '<option value="' .$type["price"]; $typeIdPrice = $typeId["price"] .'">'.$type["name"] . ' at $' . $typeIdPrice . '</option>'; this line is problematic. your <option> tag will never be closed like this. Commented Dec 20, 2013 at 10:54
  • when you use a flow control without proper curly braces you're gonna have a bad time. Also, a kitten, a puppy and a bunny die horribly Commented Dec 20, 2013 at 10:57

2 Answers 2

2

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
?>
Sign up to request clarification or add additional context in comments.

6 Comments

Is this a answer ? Can you add your explanation if you have change or corrected anything in code posted by OP.
@Rikesh Too hurry! I am doing that. :P
This worked ... so the reason why it did not work was because I was not echoing <option> properly ... Thanks a bunch .. I am new to PHP and I totally missed that!
@user3122466 On left side of my answer you can see tick option.
I have a follow up question, should I ask it here or add a new question? It is related to updating a textbox with the value the user selects from the dropdown without refreshing the page or using the $GET
|
0

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
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.