• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

AAC Php and database fetching

Sprrw

Well-Known Member
Joined
Jun 22, 2021
Messages
100
Reaction score
55
Location
Sweden
Hola Otland!
Im working on a website for my project and on my homepage I wanted to display a "latest news section" something like:
1679500624522.png
Pic from : Wayfinder - Join the Closed Beta on PlayStation® and PC (https://www.playwayfinder.com/en)

Well anyways so I came up with this code :
PHP:
            <?php $fetch_from_db = true;

            $news_db = $db->query('SELECT `title`,`body`,`category`, `date`, `player_id`, `id`, `article_image` FROM `myaac_news` ORDER BY `date` DESC LIMIT 1');

            $news = array();
            foreach ($news_db as $news)
                $news[] = array('title' => $news['title'], $news['player_id'], 'article_image' => $news['article_image'], 'body' => $news['body'], 'category' => $news['category'], 'date' => $news['date']);

            $query = $db->query('SELECT `name` FROM `players` WHERE id = ' . ($news['player_id']) . ' LIMIT 1');
            if ($query->rowCount() > 0) {
                $query = $query->fetch();
                $author = $query['name'];
            }
            echo '<a href="' . BASE_URL . '?news/archive/' . $news['id'] . '><div class="news-card">' . date("d-m-Y H:i:s", $news["date"]) . '<h2>' . $news["title"] . '</h2><h3>Author: ' . $author . '</h3></div></a>'
This does work, but I got no clue on how to make it 3, since now Im only displaying one you know. And I could just repeat the code 3 times but that feels like a bad way, could probably do some loop thing but Im very newbie when it comes to php.
So in short. Im asking for some feedback on how I would maybe improve this code + loop it 3 times outputting the 3 latest news instead of just the latest news.

Information: Im using latest MyAAC and Tfs 1.4

As always. Thanks a lot for reading! Have an amzing rest of your day! <3
 
in your news_db query, you currently have LIMIT 1, try change it to 3
Yea I also thought about this but all this does is using the third from last news. So instead of displaying all 3 like you would asume. It displays the third. And this is how you could do it by having 3 fetches one with LIMIT 1, 2 and 3 but this seems like a lot of code for such a small thing. Must be a simpler solution.

But thanks a lot for your answer! <3
 
Hi!

Your whole news can be pulled using one query with JOIN.

Like this:
PHP:
$news_db = $db->query('SELECT `title`, `body`, `category`, `date`, `player_id`, `article_image`, `p`.`name` FROM `myaac_news` `n` LEFT JOIN players p ON n.player_id = p.id ORDER BY `date` DESC LIMIT 3;');

Then just loop over the values:
PHP:
foreach ($news_db as $news) {
    echo '<a href="' . BASE_URL . '?news/archive/' . $news['id'] . '><div class="news-card">' . date("d-m-Y H:i:s", $news["date"]) . '<h2>' . $news["title"] . '</h2><h3>Author: ' . $news['name'] . '</h3></div></a>';
}
 
Hi!

Your whole news can be pulled using one query with JOIN.

Like this:
PHP:
$news_db = $db->query('SELECT `title`, `body`, `category`, `date`, `player_id`, `article_image`, `p`.`name` FROM `myaac_news` `n` LEFT JOIN players p ON n.player_id = p.id ORDER BY `date` DESC LIMIT 3;');

Then just loop over the values:
PHP:
foreach ($news_db as $news) {
    echo '<a href="' . BASE_URL . '?news/archive/' . $news['id'] . '><div class="news-card">' . date("d-m-Y H:i:s", $news["date"]) . '<h2>' . $news["title"] . '</h2><h3>Author: ' . $news['name'] . '</h3></div></a>';
}
Is it weird that I love you? 😅
Thanks a lot, worked like a charm!
 
Back
Top