我有一个表菜单,其中包含(recipe_id,ingredient_id,category_id),成分w / c包含(ingredient_id,name),category(category_id,name)和category_ingredient w / c由(ingredient_id,category_id)组成。我想得到食谱的所有成分,但我的代码不起作用。请帮帮我。
查看:
<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>
MODEL:
function getIngredientsInCategory($category_id)
{
$this->db->select('ingredient_id');
$this->db->where('category_id', $category_id);
$query = $this->db->get('category_ingredient');
foreach($query->result() as $row)
{
$ingredient[] = $row->ingredient_id;
}
$this->db->where_in('ingredient_id', $ingredient);
$query = $this->db->get('ingredient');
return $query->result();
}
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;
}
答案 0 :(得分:0)
在getRecipeIngridients()
功能中,您只返回ingredient_id
而不是name
。
第二个问题是,您正在打印ingredient_id
作为对象,它是模型函数中的数组$ingredient_id[] = $row->ingredient_id;
修改功能:
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;
}
代码在foreach循环中:
<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>