...

View Full Version : Is this right for making a php rss feed?



Andy92
07-26-2008, 05:47 PM
Hey,

I have a blog, and i let people subscribe via rss feed through feedburner.

Now my rss feed is a php file, which basically connects to my blog database, and pulls out the latest 10 posts and displays them in a feed format.

Now sometimes my feed kinda mucks up, and only displays one post or so and not 10, and sometimes it doesnt display at all.

Can someone just check thorugh my script (below), and see if i have made any errors somewhere...



<?
$sqltable = "blog";
$sqlretrieve = "10";

header("Content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";

mysql_connect("localhost", "database_username", "database_password") or die(mysql_error());
mysql_select_db("database_name") or die(mysql_error());

echo '<rss version="2.0">';

echo '<channel>
<title>Kedoa Blog</title>
<link>http://www.kedoa.com/blog</link>
<description>Learn how to make money online, increase your website popularity and various marketing techniques by reading the Kedoa Blog.</description>
<language>en-us</language>
<copyright>Copyright: (C) Kedoa</copyright>';

$data = mysql_query("SELECT * FROM `$sqltable` ORDER BY id DESC LIMIT $sqlretrieve");

while($row = mysql_fetch_array($data)) {

$page = $row[category];

if ($page == 1) { ($blog = "Apps")&&($blogcategory = "1")&&($section_link = "apps");
} else if ($page == 2) { ($blog = "Blogging")&&($blogcategory = "2")&&($section_link = "blogging");
} else if ($page == 3) { ($blog = "Design")&&($blogcategory = "3")&&($section_link = "design");
} else if ($page == 4) { ($blog = "Events")&&($blogcategory = "4")&&($section_link = "events");
} else if ($page == 5) { ($blog = "Flash")&&($blogcategory = "5")&&($section_link = "flash");
} else if ($page == 6) { ($blog = "Making Money")&&($blogcategory = "6")&&($section_link = "making-money");
} else if ($page == 7) { ($blog = "News")&&($blogcategory = "7")&&($section_link = "news");
} else if ($page == 8) { ($blog = "Ramblings")&&($blogcategory = "8")&&($section_link = "ramblings");
} else if ($page == 9) { ($blog = "Resources")&&($blogcategory = "9")&&($section_link = "resources");
} else if ($page == 10) { ($blog = "Reviews")&&($blogcategory = "10")&&($section_link = "reviews");
} else if ($page == 11) { ($blog = "Social Media")&&($blogcategory = "11")&&($section_link = "social-media");
} else if ($page == 12) { ($blog = "Technology")&&($blogcategory = "12")&&($section_link = "technology");
} else if ($page == 13) { ($blog = "Tips & Tricks")&&($blogcategory = "13")&&($section_link = "tips-tricks");
} else if ($page == 14) { ($blog = "Web")&&($blogcategory = "14")&&($section_link = "web");
}

date_default_timezone_set('Europe/London');
$timestamp = $row[posted];
$pub = date('D\, j M Y H\:i',$timestamp);
$newrow = html_entity_decode($row[full]);

echo '<item> // begins the item
<link>http://www.kedoa.com/blog/'.$section_link.'/'.$row['url'].'</link>
<title>'.$row['title'].'</title>
<description><![CDATA['.$newrow.']]></description>
<category>'.$blog.'</category>
<pubDate>'.$pub.'</pubDate>
</item>';
}

echo '</channel></rss>';
?>

scoop_987
07-26-2008, 06:48 PM
The only thing i can see needing changed is the big if elseif thing, use this instead:



<?
$sqltable = "blog";
$sqlretrieve = "10";

header("Content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";

mysql_connect("localhost", "database_username", "database_password") or die(mysql_error());
mysql_select_db("database_name") or die(mysql_error());

echo '<rss version="2.0">';

echo '<channel>
<title>Kedoa Blog</title>
<link>http://www.kedoa.com/blog</link>
<description>Learn how to make money online, increase your website popularity and various marketing techniques by reading the Kedoa Blog.</description>
<language>en-us</language>
<copyright>Copyright: (C) Kedoa</copyright>';

$data = mysql_query("SELECT * FROM `$sqltable` ORDER BY id DESC LIMIT $sqlretrieve");

while($row = mysql_fetch_array($data)) {

$page = $row[category];

//Build up an array of what each $blog is in relation to $blogcatagory:
$blog_cats = array(1 => "Apps",
2 => "Blogging",
3 => "Design",
4 => "Events",
5 => "Flash",
6 => "Money-Making",
7 => "News",
8 => "Ramblings",
9 => "Resources",
10 => "Reviews",
11 => "Social-Media",
12 => "Technology",
13 => "Tips-Tricks",
14 => "Web");

//Add a small loop that will be faster than an if else eqivilent:
for($i = 1; $i <= count($blog_cats); $i++){

//Check the page against the variable i
if($i == $page && $found != 1){
$blog = $blog_cats[$i];
$blogcategory = $i;
$section_link = str2lower($blog_cats[$i]);
$found = 1;
}
}

date_default_timezone_set('Europe/London');
$timestamp = $row[posted];
$pub = date('D\, j M Y H\:i',$timestamp);
$newrow = html_entity_decode($row[full]);

echo '<item> // begins the item
<link>http://www.kedoa.com/blog/'.$section_link.'/'.$row['url'].'</link>
<title>'.$row['title'].'</title>
<description><![CDATA['.$newrow.']]></description>
<category>'.$blog.'</category>
<pubDate>'.$pub.'</pubDate>
</item>';
}

echo '</channel></rss>';
?>


That should be less labour intensive for the server

Andy92
07-26-2008, 07:52 PM
Sweet, o you split it up into an array. I see what you have done. Thanks!

So will this load alot faster now then?

scoop_987
07-26-2008, 09:03 PM
not gonna say that, but it SHOULD be faster. But try and see, watch your server logs to see how much of a performance difference.

and as for the little thing about not having any results, i couldn't find it at all.

But try this instead, ive removed the SQL from the mysql_query and added the variable that i set before it with the SQL data in it, i always do that, easier for debugging.



<?
$sqltable = "blog";
$sqlretrieve = "10";

header("Content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";

mysql_connect("localhost", "database_username", "database_password") or die(mysql_error());
mysql_select_db("database_name") or die(mysql_error());

echo '<rss version="2.0">';

echo '<channel>
<title>Kedoa Blog</title>
<link>http://www.kedoa.com/blog</link>
<description>Learn how to make money online, increase your website popularity and various marketing techniques by reading the Kedoa Blog.</description>
<language>en-us</language>
<copyright>Copyright: (C) Kedoa</copyright>';
$SQL = "SELECT * FROM `".$sqltable."` ORDER BY `id` DESC LIMIT ".$sqlretrieve;
$data = mysql_query($SQL);

while($row = mysql_fetch_array($data)) {

$page = $row[category];

//Build up an array of what each $blog is in relation to $blogcatagory:
$blog_cats = array(1 => "Apps",
2 => "Blogging",
3 => "Design",
4 => "Events",
5 => "Flash",
6 => "Money-Making",
7 => "News",
8 => "Ramblings",
9 => "Resources",
10 => "Reviews",
11 => "Social-Media",
12 => "Technology",
13 => "Tips-Tricks",
14 => "Web");

//Add a small loop that will be faster than an if else eqivilent:
for($i = 1; $i <= count($blog_cats); $i++){

//Check the page against the variable i
if($i == $page && $found != 1){
$blog = $blog_cats[$i];
$blogcategory = $i;
$section_link = str2lower($blog_cats[$i]);
$found = 1;
}
}

date_default_timezone_set('Europe/London');
$timestamp = $row[posted];
$pub = date('D\, j M Y H\:i',$timestamp);
$newrow = html_entity_decode($row[full]);

echo '<item> // begins the item
<link>http://www.kedoa.com/blog/'.$section_link.'/'.$row['url'].'</link>
<title>'.$row['title'].'</title>
<description><![CDATA['.$newrow.']]></description>
<category>'.$blog.'</category>
<pubDate>'.$pub.'</pubDate>
</item>';
}

echo '</channel></rss>';
?>

Andy92
08-01-2008, 04:44 PM
Ok ive put that code above.

But when i view the file, it doesnt display any data?

Can you see anything...

http://www.kedoa.com/feedblog.php

??

EDIT: Ah, i have looked at the source code and it says...



<b>Fatal error</b>: Call to undefined function str2lower() in <b>/home/kedoacom/public_html/feedblog.php</b> on line <b>49</b><br />

I will try and fix this.

Andy92
08-01-2008, 04:50 PM
Ok, i put strtolower instead of str2lower and it works great!

Can you quickly go to...

http://www.kedoa.com/feedblog.php

And see if you can count 10 items?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum