0

So I have this PHP function:

function getHurrList() {
    $sql = "SELECT HURR
            FROM HURRDURR";
    return DBIface::connect()->query($sql);
}

and I want to make a custom drop-down box that will display the list of hurrs that are accessed from the database.

<select name="hurr">
<?php   
    foreach (getHurrList() as $hurrValue){
        echo '<option value="';
        echo $hurrValue;
        echo '">';
        echo $hurrValue;
        echo "</option>";
    }

?>
</select>

When this code is run, what happens is that I get a drop-down list, where each option comes up as 'Array'. How can I fix the foreach loop or replace it with something else so that each HURR in HURRDURR is displayed in the drop-down list on the page?

1 Answer 1

3

Since you are selecting the field named HURR, use that in your loop and not use the whole record array.

foreach (getHurrList() as $hurrValue)
   echo '<option value="{$hurrValue['HURR']}">{$hurrValue['HURR']}</option>';


Which is equivalent to

foreach (getHurrList() as $hurrValue){

    echo '<option value="';
    echo $hurrValue['HURR'];
    echo '">';
    echo $hurrValue['HURR'];
    echo "</option>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic. Absolutely fantastic.

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.