Rob
Rob

Reputation: 1

Why does my foreach loop create separate drop down lists?

I'm using the function below, getAllMifs() which uses PDO::FETCH_OBJ to return a stdClass object called $mifs.

Here is my model:

public function getAllMifs()
{
    $sql = "SELECT id, color, L, A, B FROM pantonePlus2010";
    $query = $this->db->prepare($sql);
    $query->execute();
    return $query->fetchAll();
}

Here is my controller:



Class Mifs extends Controller
{
    public function index()
    {

        echo 'Message from Controller: You are in the Controller: Mifs, using the method index().';

        $mifs_model = $this->loadModel('MifsModel');
        $mifs = $mifs_model->getAllMifs();


        // load views. within the views we can echo out $mifs and $amount_of_mifs easily
        require 'application/views/_templates/header.php';
        require 'application/views/mifs/index.php';
        require 'application/views/_templates/footer.php';
    }

Here is my View file:

<div>
    <?php foreach ($mifs as $mif) { ?>
        <form name="mifselect" action="<?php echo URL . 'mifs/downloadmif/' . $mif->id; ?>">
            <select name="mifselect">
                <option value="<?= $mif->id ?>"><?= $mif->color ?></option>                    
            </select>
            <input type="submit" value="Submit">
        </form>
    <?php } ?>
</div>

The problem I can't figure out is how to get the result into one drop down menu. I tried to attach a screenshot that shows the stdClass array and five drop down list the foreach ($mifs as $mif) creates but this is my first post and Stackoverflow.com requires me to have 10 reputations to post images...

I have tried this with PDO_ASSOC as well as PDO_OBJ but I still get the five drop down lists.

This must be the problem: id ?>">color ?> code but I can't find the correction to get just the one drop down list.

Any help is appreciated, thanks in advance!

Upvotes: 0

Views: 134

Answers (1)

abdullacm
abdullacm

Reputation: 636

please place options only inside loop

<div>
    <form name="mifselect" action="<?php echo URL . 'mifs/downloadmif/' . $mif->id; ?>">
    <select name="mifselect">
    <?php foreach ($mifs as $mif) { ?>    
                <option value="<?= $mif->id ?>"><?= $mif->color ?></option>                    

    <?php } ?>
    </select>
    <input type="submit" value="Submit">
    </form>
</div>

Upvotes: 1

Related Questions