【问题标题】:How to separate an Array in PHP如何在 PHP 中分隔数组
【发布时间】: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 "&lt;pre&gt;";print_r($data['results']);
  • 我不认为你的意思是echo $image1=$fetch_data-&gt;images; 从这一行中删除echo?并显示var_dump($image1); 的输出
  • 我没有看到他的代码有任何问题。只需删除 $image1 之前的回声,看看会发生什么
  • 我已经更新了我的问题..请检查一下
  • 那么你的问题是什么?您能否展示一些示例输出作为您想要的以及您得到的?

标签: php html arrays codeigniter


【解决方案1】:

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

 $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>

【讨论】:

  • 非常感谢兄弟:)
【解决方案2】:

如何使用 GROUP CONCAT。然后你可以在你的循环中使用explode来分离数据

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

【讨论】:

    【解决方案3】:

    我认为这是一个访问修饰符问题,您的 $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
       }
       ?>
    

    【讨论】:

    • 您错过了foreach 循环的关闭,并且$image= 前面的echo 不是必需的(可能会弄乱html)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    相关资源
    最近更新 更多