返回属性的值并在每个属性下显示这些值(php codeigniter)

时间:2014-01-21 17:14:34

标签: php codeigniter

是否可以返回在属性名称下选择的属性的值,以便值可以显示在它们所属的属性下?

数组

Array ( [product_id] => 41 [attributes] => Array ( [0] => Colour [1] => Size )

这是我尝试过的 控制器

$data2['product_id'] = $this->input->post('id');
$data2['attributes'] = $this->input->post('pAttr');
$data2['values'] = $this->inventory_model->get_attribute_values($data2);
$this->load->view('add_attr_value_view', $data2);

模特

// this will get values for the attributes in the array    
extract($data2);

$this->db->select('attribute_value.value');
$this->db->from('attribute_value');
$this->db->join('attributes', 'attributes.id = attribute_value.attribute_id');
$this->db->where_in('attributes.attribute', $attributes);
$result = $this->db->get();

for($i = 0; $i < count($attributes); $i++) {
    foreach($result->result() as $row) {
        $entry = array();
        $entry[$attributes] = $row->value;

        $data[] = $entry;
    }
}
return $data;

视图

foreach ($attributes as $res) 
{
    echo    "<br />" ;
    echo $res['attributes'];
    echo    "<br />" ;

    foreach ($values as $res)
    {
        $val = $res['values'];
        echo "<input type='checkbox' name='aVal[]' value='$val'>" .$val. "<br />" ;
    }
}

属性表

____________________
|  id  | attribute |
--------------------
|  1   |  Colour   |
|  2   |  Size     |

attribute_value表

______________________________
|  id  | attribute_id | value |
-------------------------------
|  1   |       1      | Red   |
|  2   |       1      | Blue  |

product_attr_value表

___________________________________________
|  id  | product_id   | attribute | value  |
--------------------------------------------
|  1   |       21     | Colour    | Red    |
|  2   |       21     | Size      | Medium |

这就是我想要实现的目标

Colour  
checkbox Red  
checkbox Blue 
.  
.  

Size  
checkbox Small  
.  
.  

0 个答案:

没有答案