user16755419
user16755419

Reputation:

How to use foreach loop inside a foreach in codeigniter?

I am fetching data from the database and showing in the table like this.

        <?php foreach($blogs->result() as $blog): ?>
            <td><?php echo $blog->title;?></td>
            <td><?php echo $blog->description;?></td>
        <?php endforeach; ?>

But I want to show the drop-down from another table so how can i achieve that?

        <?php foreach($blogs->result() as $blog): ?>
            <td><?php echo $blog->title;?></td>
            <td><?php echo $blog->description;?></td>
            <td>
            <div class="form-group">
                    <select name="orderStatus" id="orderId" class="form-control">
                        <option value="<?php echo $name['user_id']; ?>"><?php  echo $name['user_name'];?></option>
                    </select>   
            </div>
            </td>

Upvotes: 0

Views: 130

Answers (1)

Guga Nemsitsveridze
Guga Nemsitsveridze

Reputation: 751

Create selectbox options string first. in this case, it is $optionsString, and suppose that $optionsList contains all the values you want to add to the selectbox

<?php
$optionsString = "";
foreach ($optionsList as $option) {
    $optionsString .= "<option value=" . $option['user_id'] . ">" . 
      $option['user_name'] . "</option>";
}
?>

next add this $optionsString to the HTML part of the snippet you provided in your question:

<div class="form-group">
    <select name="orderStatus" id="orderId" class="form-control">
        <?php echo $optionsString; ?>
    </select>
</div>

Upvotes: 0

Related Questions