[英]Select datas from two separate tables for a news feed in MySQL/PHP
I would like to select datas from two separate tables to make a newsfeed like facebook which would display both photos, news or reviews (or anything) in one list and ordered by their date. 我想从两个单独的表中选择数据,以制作类似Facebook的新闻源,该新闻源将在一个列表中显示照片,新闻或评论(或任何内容),并按其日期排序。 Right now, I have two lists on my website which show photos and news separately.
现在,我的网站上有两个列表,分别显示照片和新闻。 I'd like to have just one list which would contain all the items.
我只想列出一个包含所有项目的列表。
Here's a example: 这是一个例子:
news 1 : june 20
photo 1 : june 15
photo 2 : june 13
news 2 : june 12
photo 3 : june 9
review 1: june 5
I tried this SQL query: 我试过这个SQL查询:
SELECT n.id, n.title, n.date, t.id_photo, t.url, t.date_photo
FROM news AS n, photos AS t
ORDER BY n.date DESC, t.date_photo DESC
LIMIT 0,30
But this didn't work. 但这没有用。 Any idea?
任何想法?
NB: There's no link between those tables. 注意:这些表之间没有链接。 There're completely different.
完全不同。
try this: 尝试这个:
<?php
$newsPage = isset($_GET['newspage']) ? $_GET['newspage'] : 0;
$photosPage = isset($_GET['photospage']) ? $_GET['photospage'] : 0;
$selectNews = mysqli_query("SELECT `news`.`id`, `news`.`title`, `news`.`date`
FROM `news`
ORDER BY `news`.`date` DESC
LIMIT ".$newsPage."0,10;");
while($news = mysqli_fetch_assoc($selectNews)) {
echo $news['title'].'<br />';
}
$selectPhotos = mysqli_query("SELECT `photos`.`id_photo`, `photos`.`url`, `photos`.`date_photo`
FROM `photos`
ORDER BY `photos`.`date_photo` DESC
LIMIT ".$photosPage."0,10;");
while($photos = mysqli_fetch_assoc($selectPhotos)) {
echo '<img src="'.$photos['url'].'" alt="Image" title="Image" /><br />';
}
Lemme know how it goes. 莱姆知道情况如何。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.