I have a table menu which consists of (recipe_id,ingredient_id,category_id),ingredient w/c consist of(ingredient_id,name),category(category_id,name) and category_ingredient w/c consist of (ingredient_id,category_id). I want to get all the ingredients of a recipe but my code won't work.
VIEW:
<form method="post">
<table class="table-responsive table table-hover">
<tr>
<td><strong>Recipe Ingredients</strong></td>
</tr>
<?php
foreach ($products as $product){
$product_id = $product['recipe_id'];
?>
<tr>
<td>
<?php foreach($this->products_model->getRecipeIngridients($product['recipe_id']) as $ing): ?>
<div class="col-sm-10"><li name="ingredients[]" value="<?php echo $ing->ingredient_id ?>" <?php echo (in_array($ing->ingredient_id)) ?>><?php echo $ing->name ?></li></div>
<?php endforeach; ?>
</td>
</tr>
<?php
}
?>
</table>
</form>
MODEL:
function getRecipeIngridients($recipe_id)
{
$this->db->where('recipe_id', $recipe_id);
$query = $this->db->get('menu');
foreach($query->result() as $row)
{
$ingredient_id[] = $row->ingredient_id;
}
return $ingredient_id;
}
In getRecipeIngridients()
function you are only return ingredient_id
not name
.
And second issue it that, you are printing ingredient_id
as object which is an array in your model function $ingredient_id[] = $row->ingredient_id;
Modified Function:
function getRecipeIngridients($recipe_id)
{
$this->db->where('recipe_id', $recipe_id);
$query = $this->db->get('menu');
$result = $query->result(); // return all data in object form.
return $result;
}
Code Inside the foreach loop:
<div class="col-sm-10">
<li name="ingredients[]" value="<?php echo $ing->ingredient_id ?>">
<?php echo $this->db->get_where("ingredient",array("ingredient_id"=>$ing->ingredient_id))->row()->name; ?>
</li>
</div>