簡體   English   中英

CodeIgniter:在視圖中顯示兩個表中的數據-使用一個循環?

[英]CodeIgniter: Displaying data from two tables in a view - use one loop?

請給我一些幫助。 我有這兩個數據庫表,關系為1-n(一個相冊有很多圖像):

  albums            images
----------       -------------
album_id            img_id
title               album_id
description         img_file

現在,我想做的是在我的視圖中顯示所有帶有其圖像的專輯 ,類似這樣 ,請參見第二張屏幕截圖(作品集)。

這就是我的控制器中的內容:

// fetch all album data 
$albums = $this->album_model->get();

// fetch images for each album
foreach ($albums as $album ) {
 $images = $this->albumimage_model->get_by('album_id', $album->album_id);
}

$data = array(
 'albums'  =>  $albums, 
 'images'  =>  $images, 
);
$this->load->view('album_listing', $data); 

如果我在視圖中遍歷相冊數據,則會得到以下信息:

<?php foreach($albums as $album): ?>
<div class="album">
<p><?php echo $album->title; ?></p>
<p><?php echo $album->description; ?></p>
<div>
<?php endforeach; ?> 

到目前為止,還不錯,但是現在這里來了令人頭疼: 我想顯示屬於每個專輯的圖像 ,所以基本是這樣的:

<?php foreach($albums as $album): ?>
<div class="album">
    <p><?php echo $album->title; ?></p>
    <p><?php echo $album->description; ?></p>

    // show all images of the album
    <div class="images"><img src="uploads/<php? echo $album->title;  ?>.'/'.<php? echo $image->image_file;  ?> " /></div>

<div>
<?php endforeach; ?>

根據CodeIgniter的慣例,我該怎么做?

一些其他信息:1.我還啟用了CodeIgntier的Profiler,並且我注意到正在運行的查詢以這種方式遞增。 如果我有很多相冊(50-100張帶有圖像的相冊),這不會增加內存嗎???

  1. 我還嘗試僅使用一個帶有JOIN的查詢來執行此操作,如下所示:

    $ albums = $ this-> db-> select('albums。*')-> select('images.img_id,images.img_file')-> join('images','images.album_id = albums.album_id') -> order_by('albums.album_id')-> get('albums')-> result_array();

問題是當我遍歷數據時(假設我的數據庫中有兩個相冊),我在頁面上兩次獲取數據,例如:

album1 - images of album 1
album1 - images of album 1
album2 - images of album 2
album2 - images of album 2
$query = $this->db->get('albums');
$albums = $query->result_array();

$query = $this->db->get('images');
$images =$query->result_array();

foreach ($albums as $album)
{

    foreach ($images as $image)
    { 
        if($image['album_id'] == $album['album_id']){
            $data[$album['album_id']]['images'] = $image['album_id'];
        }
    }
}

var_dump($data);

如果要消除重復的條目,請嘗試在查詢中使用SELECT DISTINCT ,這具有類似的作用。 另外,我不太了解您的意思是:

運行的查詢像這樣遞增

你可以做一些跟從

控制器-

// fetch all album data <br>
$data['albums']= $this->album_model->get_albums();
// fetch all images<br>
$data['images']=  = $this->album_model->get_images();
$this->load->view('album_listing', $data);<br>

查看(album_listing.php)

foreach ($albums as $album)
{

    foreach ($images as $image)
    { 
        if($image->album_id == $album->album_id)
        echo "Album - ".$album->album_id.", Title".$album->title.", Description".$album->description." Image ID - ".$image->img_id."Img File - ".$image->img_file;
    }
}

在您的模型中

    public function get_images(){
    { 
       $sql ="select a.album_id, a.description, i.img_id, i.img_file from album a, image i where a.album_id = i.album_id"
       $query = $this->db->query($sql);
       $data   =   array();
       foreach ($query->result() as $row) {
        $data[] = $row;
       }
    return $data;
    }

調節器

 $data['albums_img']= $this->album_model->get_images();
 $this->load->view('album_listing', $data);

視圖

 foreach ($albums_img as $album)
 { 

    echo "Album - ".$album->album_id.", Title".$album->title.", Description".$album->description." Image ID - ".$album->img_id."Img File - ".$album->img_file;
 }

希望這對您有幫助。

暫無
暫無

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

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