显示具有照片的相册(PHP) [英] Display Album with Photos (PHP)
问题描述
要开始,这真的不是CodeIgniter具体的。我很难理解一个想法,所以任何知道PHP / SQL(或我的问题是)的人都可以跳。
我有2个表,'照片'和相册。
相册列:ID TITLE USER_ID CREATED MODIFIED
照片列:ID ALBUM_ID TITLE LOC CREATED MODIFIED
我正在使用查询
$ this-> db-> select('*');
$ this-> db-> from('album');
$ this-> db-> join('photo','album.id = photo.album_id');
$ this-> db-> limit(10);
return $ this-> db-> get();
这我认为是
SELECT * FROM album JOIN photo ON album.id = photo.album_id LIMIT 10;
无论如何,我不明白是如何显示每个相册和相关的照片。 p>
如下...
< div class = >
< img src =image1/>
< img src =image2/>
< / div>
< div class =album>
< img src =image3/>
< / div>
基本上如何回声出正确的专辑及其相关图片?据我所知,foreach循环只是写出每个图像,而不区分图像属于哪个专辑。
我是否应该使用JOIN查询?
UPDATE:
哇,ok tilman,你是亲。非常感谢帮助。对于任何引用这个的人,请确保你改变$ array [$ row ['id']] [] = $ row;无论您的相册ID是什么。在我的case,它的album_id。我最后的连接语句是覆盖id与album_id。
是,加入两个表格是一种方式。
$ query = $ this-> db-> query('
SELECT *
FROM album a
JOIN photo p ON a.id = p.album_id
LIMIT 10
');
foreach($ query-> result_array()as $ row){
$ array [$ row ['id']] [] = $ row;
}
foreach($ array as $ album){
echo'< div class =album>';
foreach($ album as $ photo){
echo'< img src ='。$ photo ['src']。'/>';
}
echo'< / div>';
}
我黑客了这段代码,希望这对你有用。
我们从db中取出所有的数据,把它们放在一个多重嵌套的数组中,按照专辑分组。
然后迭代数组, html。
可能不是最漂亮的解决方案,但应该完成工作)
我使用Doctrine与Codeigniter,它提供这种功能开箱即用,它被称为阵列水化器。
可能想看看它。
如果您需要帮助设置,请告诉我。
To start off, this really isn't CodeIgniter specific. I'm having trouble grasping an idea, so anyone that knows PHP/SQL (or whatever my problem is) can jump in.
I have 2 tables, 'Photo' and 'Album'.
Album columns : ID TITLE USER_ID CREATED MODIFIED
Photo columns : ID ALBUM_ID TITLE LOC CREATED MODIFIED
I'm using the query
$this->db->select('*');
$this->db->from('album');
$this->db->join('photo', 'album.id = photo.album_id');
$this->db->limit(10);
return $this->db->get();
which I think translates into
SELECT * FROM album JOIN photo ON album.id = photo.album_id LIMIT 10;
Anyways, what I don't understand is how to display each album with the related photos.
So something like...
<div class="album">
<img src="image1" />
<img src="image2" />
</div>
<div class="album">
<img src="image3" />
</div>
Basically how do I "echo" out the correct albums and their relative images? As far as I can tell, a foreach loop is just going to write out each image without discriminating between which album the image belongs to.
Should I even be using a JOIN query?
UPDATE:
Wow, ok tilman, you are pro. Thanks a lot for the help. For anyone else referencing this, make sure you change 'id' in $array[$row['id']][] = $row; to whatever your album id is. In my case, its album_id. My final join statement was overwriting id with album_id.
Yes, JOINing the two tables is the way to go.
$query = $this->db->query('
SELECT *
FROM album a
JOIN photo p ON a.id = p.album_id
LIMIT 10
');
foreach ($query->result_array() as $row) {
$array[$row['id']][] = $row;
}
foreach ($array as $album) {
echo '<div class="album">';
foreach ($album as $photo) {
echo '<img src="' . $photo['src'] . '"/>';
}
echo '</div>';
}
I hacked up this piece of code, I hope this works for you. We pull all the data from the db, put them in a multi-nested array, grouped by albums.
Then iterate over that array and display the final html.
Probably not the prettiest solution, but should get the job done ;)
I am using Doctrine with Codeigniter, which provides this functionality out-of-the-box, it's called the array hydrator. Might want to have a look at it.
Let me know if you need help setting it up.
这篇关于显示具有照片的相册(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!