Skip to content
Advertisement

populating php dropdown options with mysql result values shows “Array” when I sort() the array

Edit: When I commented out the sort() line, now it is populating properly. PHP sort looks in-place so I don’t know what happened.

I also had to insert a redundant <div> right above the form to either get the array of option values from a session variable or calling the querying function to get it populated.

<?php
                    $food_options_arr = [];
                    if(!isset($_SESSION['food-options-arr'])){
                        $food_options_arr = get_food_options();
                    }
                    else{
                        $food_options_arr = get_food_options();
                    }
                    for($i=0; $i<count($food_options_arr); $i+=1){
                        //echo "{$food_options_arr[$i]}";
                    }
                    ?>

Coming from Python, this feels very weird and somewhat clunky.

First I thought it was my problem using associative arrays so I used a linear array to store my query results. My query:

$sql = "SELECT species from lunchbox WHERE 1=1";

I saw somewhere on S.O. that 1=1 will not hurt performance so left it in.

When I call the querying function in my view page and step through on debugger, I can clearly see the array holds the correct query results:

food_array in debugger

And I also checked each value of $food in the array to make sure it was there.

Then I try to populate the dropdown following this: https://stackoverflow.com/a/5189689/13865853 or this: https://stackoverflow.com/a/43334388/13865853

But all I get are values of “Array” as dropdown options.

Array only

My code:

                  <form action="">
                    <label class="dropdown-label">Choose Food:</label>
                    <br>
                    <select id="food-dropdown" name="food-option">
                        <?php
                        $food_options_arr = array();
                        if(!isset($_SESSION['food-options-arr'])){
                            $food_options_arr = get_food_options();
                        }
                        else{
                            $food_options_arr = $_SESSION['food-options-arr'];
                        }
                        //$food_options_arr = array_unique($food_options_arr);
                        sort($food_options_arr);
                        foreach($food_options_arr as $food){
                            echo "<option value='". $food ."'>" . $food . "</option>";
                        }
                        ?>

                        <option selected="selected">
                            <?php echo "$food_options_arr[0]";?>
                        </option>
                    </select>
                </form>

Advertisement

Answer

What ended up working was:

                    foreach($metabollite_options_arr as $sci_name){
                            $trim_name = trim($sci_name, "[]?");
                            str_replace("/", " ", $trim_name);
                            if($trim_name != ""){
                                echo "<option value={strtolower($trim_name)}>$trim_name</option>";
                            }
                        }
                        ?>

so using {} to enclose the variable. However, I was unable to trim() nor str_replace() the /‘s.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement