2016-02-09 31 views
1

我有一個表格菜單,它由(recipe_id,ingredient_id,category_id),成分w/c組成(ingredient_id,name),category(category_id,name)和category_ingredient w/c由(ingredient_id,category_id)組成。我想獲得配方的所有成分,但我的代碼不起作用。請幫幫我。在codeigniter中獲取配方的成分

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> 

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

重複的問題??? – Kunal

回答

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> 
+0

它表示未定義的屬性名稱:( – MSE

+0

@MSE:你在菜單表中有成分標識和名稱嗎??? – devpro

+0

only ingredient_id。 – MSE