【发布时间】:2014-01-20 05:44:11
【问题描述】:
我正在使用 codeIgniter。我创建了一个模型来从表中获取所有记录。在控制器中,我将数据发送到数组中的视图,并在视图中打印form fields 中的所有数据。
我面临的问题是我无法理解如何将images(在另一个数组中)的数据关联到 3 个不同的表单字段中。
这就是我回显图像时得到的结果
foreach ($results as $fetch_data)
{
echo $image1=$fetch_data->images;
}
输出:-
/images/img22.jpg/images/img23.jpg/images/img24.jpg/images/img25.jpg
它将所有数组打印在一行中,我无法理解如何离开它们。 请指导我。以下是我的完整代码。
型号
public function fill_form($id)
{
$this->db->select("products.product_id , products.price, products.name,
products.color, products.size, product_images.product_id,
product_images.images, product_images.image_id ");
$this->db->from('products');
$this->db->join('product_images',
'products.product_id = product_images.product_id');
$this->db->where('products.product_id',$id);
$query= $this->db->get();
return $query->result();
}
控制器
public function fill_controller()
{
$product_id=$this->uri->segment(3);
$data['results']=$this->products_model->fill_form($product_id);
$this->load->view('header');
$this->load->view('update_product',$data);
$this->load->view('footer');
}
查看
<?php
foreach ($results as $fetch_data)
{
$product_id= $fetch_data->product_id;
$product_name= $fetch_data->name;
$price= $fetch_data->price;
$color= $fetch_data->color;
$size= $fetch_data->size;
echo $image1= $fetch_data->images;
}
HTML
Front Image:
<input type="file" name="userfile_1">
<img src="<?php echo base_url().$image1; ?>" width="50" height="50">
<br>
Right Sleeves Images:
<input type="file" name="userfile_2"> <br>
Left Sleeves Images
<input type="file" name="userfile_3"> <br>
数组结果
Array
(
[0] => stdClass Object
(
[product_id] => 76
[price] => 6000
[name] => MyName
[color] => Blue
[size] => Small/Medi
[images] => /images/img22.jpg
[image_id] => 21
)
[1] => stdClass Object
(
[product_id] => 76
[price] => 6000
[name] => MyName
[color] => Blue
[size] => Small/Medi
[images] => /images/img23.jpg
[image_id] => 22
)
[2] => stdClass Object
(
[product_id] => 76
[price] => 6000
[name] => MyName
[color] => Blue
[size] => Small/Medi
[images] => /images/img24.jpg
[image_id] => 23
)
[3] => stdClass Object
(
[product_id] => 76
[price] => 6000
[name] => MyName
[color] => Blue
[size] => Small/Medi
[images] => /images/img25.jpg
[image_id] => 24
)
)
【问题讨论】:
-
显示你的结果数组做
echo "<pre>";print_r($data['results']); -
我不认为你的意思是
echo $image1=$fetch_data->images;从这一行中删除echo?并显示var_dump($image1);的输出 -
我没有看到他的代码有任何问题。只需删除 $image1 之前的回声,看看会发生什么
-
我已经更新了我的问题..请检查一下
-
那么你的问题是什么?您能否展示一些示例输出作为您想要的以及您得到的?
标签: php html arrays codeigniter