Reputation: 347
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.
<?php
define('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.
Upvotes: 0
Views: 208
Reputation: 736
I think your immediate problem is that you lack the closing >
in the CTYPE
so you get a parse error for that. It's also not recommended to use the PHP closing tag ?>
at the end of the file, especially when working with XML format which is rather sensitive to extra white spaces.
I also suggest you wrap your functionality in functions, so it's easier to read and debug. Here's a suggested implementation (I have replaced your database requests with a function that returns one "row", because I have no access to your database).
<?php
define('PAGE', 'site');
function rss_date($time) {
return gmdate('r', $time);
}
$feed_limit = (int) $_GET['l'];
if ($feed_limit <= 0 || $feed_limit >= 100) {
$feed_limit = 500;
}
render($_GET['r']);
/**
* Renders the page
* @param $type
*/
function render($type) {
$items = [];
render_header();
switch($type) {
case 'newpost':
$items[] = getItemFromDatabase(); // Replace with database request
break;
case 'category':
$items[] = getItemFromDatabase(); // Replace with database request
break;
default:
$items[] = getItemFromDatabase(); // Replace with database request
}
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) {
$url = null; // Declare $url to something!
$rssDate = rss_date($createdAt);
echo <<< RSS
<item>
<title><![CDATA[$postTitle]]></title>
<description>$postExcerp</description>
<link>https://www.akhbar1.com/{$url->file($postId, $postTitle)}</link>
<pubDate>$rssDate</pubDate>
</item>
RSS;
}
/**
* Just for test purposes, remove and fetch from DB
* @return array
*/
function getItemFromDatabase() {
return [
"post_id" => 1,
"post_title" => "my item",
"post_exerp" => "my description",
"created_at" => 1221412212
];
}
Upvotes: 1