Quantcast
Channel: Create RSS feeds with PHP and Mysqli - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Create RSS feeds with PHP and Mysqli

$
0
0

I does not have rss feed for categories so i am trying to create 'rss php' file for new post and posts by categories

update

so far i did this with help from @jonasdev.

<?phpdefine('PAGE', 'site');// Create connection $con=mysqli_connect("akhbar1.com","user","pass" ,"DB");// Check connection if (mysqli_connect_errno($con)) { echo "Database connection failed!: " . mysqli_connect_error(); }function rss_date($time) {    return gmdate('r', $time);}$feed_limit = (int) $_GET['l'];if ($feed_limit <= 0 || $feed_limit >= 100) {    $feed_limit = 500;} $sql = "SELECT * FROM in_posts ORDER BY post_id DESC LIMIT $feed_limit"; $query = mysqli_query($con,$sql);render($_GET['r']);/** * Renders the page * @param $type */function render($type) {    $items = [];    render_header();    switch($type) {        case 'newpost':            $items[] = getItemFromDatabase();             break;        case 'category':            if(isset($_GET['id'])){               $get_idy = $_GET['id'];                $items[] = getCategoryItemFromDatabase();                } else {               echo "failed";            }            break;        default:            $items[] = getItemFromDatabase();     }    foreach($items as $item) {        render_item($item["post_id"], $item["post_title"], $item["post_excerp"], $item["created_at"]);    }    render_footer();}/** * Renders opening RSS */function render_header() {    header ('Content-type: text/xml');    echo <<< RSS<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0"><channel><title>title</title><link>https://www.akhbar1.com/</link><description>description</description>RSS;}/** * Renders closing RSS */function render_footer() {    echo <<< RSS</channel></rss>RSS;}/** * Renders one RSS Item *  * @param $postId * @param $postTitle * @param $postExcerp * @param $createdAt */function render_item($postId, $postTitle, $postExcerp, $createdAt) {    $rssDate = rss_date($createdAt);    echo <<< RSS<item><title><![CDATA[$postTitle]]></title><description>$postExcerp</description><link>https://www.akhbar1.com/news-$postId.html</link><pubDate>$rssDate</pubDate></item>RSS;}/** *  * @return array */function getItemFromDatabase() { while($row = mysqli_fetch_array($con,$query)){   $title=$row["post_title"];   $link=$row["post_id"];   $description=$row["post_content"];   $createdAt=$row["created_at"];    return ["post_id" => $link,"post_title" => $title,"post_exerp" => $description,"created_at" => $createdAt,    ];    }}function getCategoryItemFromDatabase() { $sel = mysqli_query($con,"SELECT * FROM in_posts WHERE post_category_id='$get_idy' ORDER BY post_id DESC LIMIT $feed_limit"); while($row = mysqli_fetch_array($con,$sel)){   $title=$row["post_title"];   $link=$row["post_id"];   $description=$row["post_content"];   $createdAt=$row["created_at"];    return ["post_id" => $link,"post_title" => $title,"post_exerp" => $description,"created_at" => $createdAt,    ];    }}

feed for new posts https://www.akhbar1.com/rss.php?r=newpost&l=10

feed by categories https://www.akhbar1.com/rss.php?r=category&id=1&l=10

I'm not getting anything from database

I do not know what i am doing wrong.

posts table enter image description herecategories tableenter image description here


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images