【问题标题】:make first uploaded gallery image as profile image将第一个上传的图库图片作为个人资料图片
【发布时间】:2017-06-22 07:54:59
【问题描述】:

我正在使用 Codeigniter,我想要做的是当用户将图片上传到图库时,第一个上传的图片应该被选为个人资料图片。

我的数据库结构

项目表

|    id    |  slug  |  user_id  |  post_date |

|    12    |  test  |    111    |  12/5/2017 |

项目库

| gallery_id |  item_id  |     image     | 

|     121    |    12     |  profile.png  |   -- i want this record selected
|     122    |    12     |  gallery.png  | 

我使用此代码从图库表中选择随机图片。

    public function shopItems($id) {

    $this->db->select("*,MIN(item_gallery.gallery_id)");
    $this->db->from('items');
    $this->db->join('item_gallery', 'items.id = item_gallery.item_id', 'left');
    $this->db->where('items.user_id', $id);     
    $this->db->order_by('items.post_date', 'Desc');
    $this->db->group_by("item_gallery.item_id");
    $query = $this->db->get();
    return $query->result();
}

【问题讨论】:

    标签: mysql codeigniter activerecord codeigniter-3


    【解决方案1】:

    通过添加和额外的自动增量列更改item_gallery 表结构。

    | gallery_id |  item_id  |     image     | 
    
    |     121    |    12     |  profile.png  |   -- i want this record selected
    |     122    |    12     |  gallery.png  |     
    

    现在试试下面的 sn-p:

    public function shopItems($id)
    {
    
        $this->db->select('id');
        $this->db->from('items');
        $this->db->where('user_id', $id);
        $query = $this->db->get();
        $item_id = $query->row('id');
    
        $this->db->select("*,(SELECT image FROM item_gallery WHERE item_id = '$item_id' ORDER BY gallery_id ASC LIMIT 1) AS profile_img");
        $this->db->from('items');
        $this->db->join('item_gallery', 'items.id = item_gallery.item_id', 'left');
        $this->db->where('items.user_id', $id);     
        $this->db->order_by('items.post_date', 'Desc');
        $this->db->group_by("item_gallery.item_id");
        $query = $this->db->get();
        return $query->result();
    }
    

    现在shopItems 返回用户上传的第一张图片。

    【讨论】:

    • items 不能被限制为一个 bcz 我在表中有很多项目,而它们是按 $this->db->order_by('items.post_date', 'Desc'); 排序的。
    • 您将如何识别用户的特定图像?表示item_gallery 表中没有user_id。
    • 项目表中的用户 ID
    • 与 item_galley 图像链接的项目表用于不为用户提供的项目,项目表有 user_id,它不是用户配置文件,项目配置文件图像
    • 我更改了这部分,但它在所有项目中显示相同的图片 $this->db->select("*,(SELECT image FROM item_gallery WHERE item_id = item_gallery.item_id ORDER BY gallery_id DESC LIMIT 1)作为个人资料图片");
    【解决方案2】:

    像这样简单地写查询

    public function shopItems($id) {
        $sql = "Select * from items a left join (Select item_id, image from items_galary group by item_id limit 1) b on a.id = b.item_id and a.id = '$id'";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    

    试试这个

    【讨论】:

    • 您的代码不起作用。因为它 $this->db->limit(1);我的桌子上有很多东西要显示,我想要物品表格和图像表格 itemgalley 中的所有东西。
    • 请试试这个查询:Select * from items a left join (Select item_id, image from items_galary group by item_id limit 1) b on a.id = b.item.id
    • 请编辑代码并给我看..!关于你的答案
    • 怎么把别人给的可以用活动记录的方式。
    • $this->db->join('item_gallery', 'items.id=(SELECT item_id FROM item_gallery WHERE item_gallery.item_id=' . $item . ' ORDER BY gallery_id ASC LIMIT 1)' , '剩下');任何人都可以修改此代码以适应 - 这会提取所有记录
    【解决方案3】:

    我终于想出了解决方案,谢谢大家的帮助。

    public function shopItems($id, $limit, $start) {
    
        date_default_timezone_set('Asia/Colombo');
        $expire = date('Y-m-d');
        $this->db->select("*,(SELECT image FROM item_gallery WHERE item_id = items.id ORDER BY gallery_id ASC LIMIT 1) AS profile_img");
        $this->db->from('items');
        $this->db->join('item_gallery', 'items.id = item_gallery.item_id', 'left');
        $this->db->where('items.exp_date >', $expire);
        $this->db->group_start();
        $this->db->where("items.status = 'yes'");
        $this->db->or_where("items.status = 'edit'");
        $this->db->group_end();
        $this->db->where('items.user_id', $id);
        $this->db->limit($limit, $start);
        $this->db->order_by('items.post_date', 'Desc');
        $this->db->group_by("item_gallery.item_id");
        $query = $this->db->get();
        return $query->result();
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-30
      • 2020-01-10
      • 2014-04-14
      • 1970-01-01
      • 2015-05-22
      • 2019-10-17
      • 2020-07-15
      • 2018-01-08
      • 1970-01-01
      相关资源
      最近更新 更多