簡體   English   中英

Codeigniter 2.1-一對多加入

[英]Codeigniter 2.1 - join one to many

我要表:

圖庫 -> id_gallery,名稱,描述,數據

和:

圖片 -> gallery_id,名稱

一個圖庫可以包含許多圖像。 我需要選擇所有畫廊和所有圖像,並在查看頁面上顯示所有畫廊和包含該畫廊的所有圖像。 怎么辦呢?

我現在有這樣的代碼:

$this->db->select('*')->from('image')->join('gallery','id_gallery = gallery_id');
$q = $this->db->get();
return $q = $q->result_array();

編輯:

<?php foreach ($gallery as $gal):  ?>
<figure>
<a href="<?php echo IMG ?>galerija/<?php echo $gal['name'] ?>/<?php echo $gal['path'] ?>" rel="galerija[slike]" class="figure_img">
<img src="<?php echo IMG ?>galerija/<?php echo $gal['naziv'] ?>/thumbs/<?php echo $gal['path'] ?>" >
<figcaption><?php echo $gal['name']; ?></figcaption>
</a>
</figure>
<?php endforeach; ?>

此foreach循環將產生5個div標簽而不是一個(例如,如果畫廊有5張圖像)。

如果要從圖像表中為所有畫廊選擇所有圖像..........

$this->db->select('*');
$this->db->from('images');
$this->db->join('gallery', 'gallery.id_gallery = images.gallery_id');
$query = $this->db->get();
return $q = $query->result_array();

或以其他方式。 寫一個模型函數

public function get_images()

 {
    $res=$this->db->query("select * from images and gallery where gallery.id_gallery = images.gallery_id");
    if($res->num_rows()>0){
        return $res->result("array");
    }

    return array();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM