sruupl / batflat

Lightweight, fast and easy CMS for free. Bootstrap ready. https://batflat.org
MIT License
134 stars 54 forks source link

Randomize Feed (Ask) #102

Closed swanteadikrisna closed 3 years ago

swanteadikrisna commented 3 years ago

Hello, in directory inc/modules/blog/site.php i found this code:

public function _generateRSS($lang) { header('Content-type: application/xml'); $this->setTemplate(false);

    $rows = $this->db('blog')
                ->where('status', 2)
                ->where('published_at', '<=', time())
                ->where('lang', $lang)
                ->limit(5)
                ->desc('published_at')
                ->toArray();

My question is: how to generate rss feed with 1 feed item and its randomized, every time we open it.

Sorry my English is not good.

klocus commented 3 years ago
$this->core->db()->pdo()->query("SELECT * FROM blog ORDER BY RANDOM() LIMIT 1;");

Edited: Change exec to query.

swanteadikrisna commented 3 years ago

Hello, thank you for your attention.

My code is:

public function _generateRSS($lang)
{
    header('Content-type: application/xml');
    $this->setTemplate(false);

    $rows = $this->core->db()->pdo()->exec("SELECT * FROM blog ORDER BY RANDOM() LIMIT 1;");

    if (!empty($rows)) {
        foreach ($rows as &$row) {
            if (!empty($row['intro'])) {
                $row['content'] = $row['intro'];
            }

            $row['content'] = preg_replace('/{(.*?)}/', '', html_entity_decode(strip_tags($row['content'])));
            $row['url'] = url('blog/post/'.$row['slug']);
            $row['cover_url'] = url(UPLOADS.'/blog/'.$row['cover_photo']).'?'.$row['published_at'];
            $row['published_at'] = (new \DateTime(date("YmdHis", $row['published_at'])))->format('D, d M Y H:i:s O');

            $this->filterRecord($row);
        }

        echo $this->draw('feed.xml', ['posts' => $rows]);
    }
}

The result is not error, but the rss on my site is blank.

Am i wrong?

This is the rss link.

klocus commented 3 years ago

Sorry, it's my fault. In my earlier comment I meant the "query" method, not "exec".

https://www.php.net/manual/en/pdo.query.php

Also please be sure to use DEV_MODE.

swanteadikrisna commented 3 years ago

Hello, thank you very much for your attention.

I have change the script to: $rows = $this->core->db()->pdo()->query("SELECT * FROM blog ORDER BY RANDOM() LIMIT 1;");

$rows = $this->core->db()->pdo()->query("SELECT * FROM blog ORDER BY RANDOM() LIMIT 1;"); foreach ($conn->query($sql) as $row)

I also change the scripts with the other script that i thinks is similiar with developer mode on. But, my apologize, maybe my knowledge in PHP is too bad, because i am is a law student, i learn HTML, CSS and PHP just by self taught. Can you help me to randomize feeds in batflat?

Thank you very much.

johannesmateboer commented 3 years ago

Not sure if you still need help on this, but there's a line with a problem:

foreach ($conn->query($sql) as $row)

This is a loop, you loop through every row. But your loop doesn't do that, it does something with $conn->query, which is a command, not a list with rows.

So you can use the part you had at first:

foreach ($rows as &$row) {
            if (!empty($row['intro'])) {
                $row['content'] = $row['intro'];
            }

            $row['content'] = preg_replace('/{(.*?)}/', '', html_entity_decode(strip_tags($row['content'])));
            $row['url'] = url('blog/post/'.$row['slug']);
            $row['cover_url'] = url(UPLOADS.'/blog/'.$row['cover_photo']).'?'.$row['published_at'];
            $row['published_at'] = (new \DateTime(date("YmdHis", $row['published_at'])))->format('D, d M Y H:i:s O');

            $this->filterRecord($row);
        }

With this, you loop through every row, which you already have ($rows = $this->core ..etc)