如何在PHP中分离数组

时间:2014-01-20 05:44:11

标签: php html arrays codeigniter

我正在使用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
        )

)

3 个答案:

答案 0 :(得分:2)

您可以尝试将查询中的GROUP_CONCAT与PHP中的爆炸结合使用。

 $this->db->select("products.product_id , products.price, products.name,
    products.color, products.size, GROUP_CONCAT(product_images.images) AS images,
    GROUP_CONCAT(product_images.image_id) AS  image_id");

在视图中

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;
            $image  =       $fetch_data->images;
            $images = explode(",",$image);
}

HTML

 Front Image: <input type="file" name="userfile_1">    
    <?php if(!empty($images[2])){ ?>       
          <img src="<?php echo base_url().$images[0];  ?>" width="50" height="50">
    <?php } ?>
    <br>

    Right Sleeves Images: <input type="file" name="userfile_2">    
    <?php if(!empty($images[2])){ ?>     
        <img src="<?php echo base_url().$images[1];  ?>" width="50" height="50">
    <?php } ?>
    <br>

    Left Sleeves Images : <input type="file" name="userfile_3">         
    <?php if(!empty($images[2])){ ?>
       <img src="<?php echo base_url().$images[2];  ?>" width="50" height="50">
    <?php } ?>
    <br>

答案 1 :(得分:1)

使用GROUP CONCAT怎么样?然后,您可以在循环中使用explode来分隔数据

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

答案 2 :(得分:1)

我认为它是一个访问修饰符问题你的$ image1变量范围只是在foreach循环中你无法从循环中得到它

访问它结束你在页面结束循环并打印它...也打印在foreach循环中的HTML

在您的视图中

<?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;
    $image1 =       $fetch_data->images;           
?>
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>



<?php
   }
   ?>