Search code examples
phpmysqlsqlfeed

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. 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:

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.


Solution

  • 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.