PDA

View Full Version : Force object or div to load last?


John84
12-07-2006, 03:35 AM
Hello,

I just added an rss feed using a free service from rssfeedreader.com to my health site (http://www.perdiem.info); found on the left hand column under the menu. Problem is, it has seriously slowed the site's load time. Is there a way to force it to load last? This way the content still loads quickly for the viewer.

BonRouge
12-07-2006, 04:56 AM
That's quite strange, isn't it?
Can you post your code here?
Where is the feed coming from?

John84
12-07-2006, 12:24 PM
This is the code givin to me by rssfeedreader....<?php
$olderror_reporting =error_reporting(0);
include ("http://www.rssfeedreader.com/rss3/rss.php?url=http%3A%2F%2Fwww.medicalnewstoday.com%2Frss%2Fcancer-oncology.xml%2Chttp%3A%2F%2Fwww.medicalnewstoday.com%2Frss%2Fsmoking.xml%2Chttp%3A%2F%2Fwww.medicaln ewstoday.com%2Frss%2Fallergy.xml%2Chttp%3A%2F%2Fwww.medicalnewstoday.com%2Frss%2Fpregnancy.xml%2Chtt p%3A%2F%2Fwww.medicalnewstoday.com%2Frss%2Fmental_health.xml&newpage=1&chead=&atl=&desc=1&owncss=&eleminate=&auth=&dts=&width=125&max=4&tlen=17&rnd=1&bt=1&bs=None&nmb=&ntb=&naf=&nst=&nwd=0&nht=0&dlen=110&lstyle=4&lc=%230033cc&bg=%239999FF&bc=%230033cc&spc=&ims=&tc=&ts=8&tfont=Arial,+Helvetica,+Sans-serif&rf=".$HTTP_SERVER_VARS['SERVER_NAME'].$HTTP_SERVER_VARS['PHP_SELF']."&phpout=1");
error_reporting($olderror_reporting);
?>


These particular feeds are coming from medicalnewstoday.com. However I noticed that any feed will slow the page down. I guess the 'lag' comes from rssfeedreader.com. Are rss feeds easy enouhg to implement that I can just lose using their service and do it myself?

Thanks.

BonRouge
12-08-2006, 06:50 AM
Here's one of your pages with an xmlparser that I've configured to suit your needs: http://bonrouge.com/test/perdiem.php

You'll get four random stories from the feed.

The parser looks like this:

xmlparser.php
<?php


$insideitem = false;
$title="";
$description = "";
$link = "";
$pubdate = "";
$tag ="";
$news=array();
$i=0;

function startElement($parser, $name, $attrs) {
global $insideitem ,$title,$description,$link,$pubdate,$tag;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}

function endElement($parser, $name) {
global $insideitem ,$title,$description,$link,$pubdate,$tag, $news;
if ($name == "ITEM") {
$pubdate=preg_replace("/00:00:00 (GMT|PST)/", "", $pubdate);

array_push($news, array($link,$title,$description,$pubdate));
$insideitem = false;
$title="";
$description = "";
$link = "";
$pubdate = "";
}


}

function characterData($parser, $data) {
global $insideitem ,$title,$description,$link,$pubdate,$tag;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;

break;
case "LINK":
$link .= $data;
break;

case "PUBDATE":
$pubdate .= $data;
break;
}
}
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen($uri,"r")
or die("Error reading data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);

$i=0;
shuffle($news);


function wordLimit($string, $length, $ellipsis = '...') {
return count($words = preg_split('/\s+/', ltrim($string), $length + 1)) > $length ?
rtrim(substr($string, 0, strlen($string) - strlen(end($words)))) . $ellipsis :
$string;
}


foreach ($news as $story) {
if ($i<$number) {
echo "
<li><a href=\"$story[0]\" class=\"newstitle\">".wordLimit($story[1], $headlinelength)."</a>
<ul>
<li class=\"description\">".wordLimit($story[2], $storylength)."</li>
<li class=\"pubdate\">$story[3]</li>
</ul>
</li>";
}
$i++;
}

?>


I replaced what you had on the page with this:<ul id="rss">

<?php
$uri="http://www.medicalnewstoday.com/medicalnews.xml";
$storylength=20;
$headlinelength=3;
$number=4;
include 'xmlparser.php';
?>

</ul>


And I added this to the style sheet:#rss {
height:400px;
overflow:hidden;
}
#rss, #rss ul {
list-style:none;
font: normal 11px Arial, Helvetica, sans-serif;
}
#rss, #rss li, #rss ul {
margin:0;
padding:0;
}
#rss ul {
margin:3px 0;
}
.pubdate {
display:none;
}

You could try it out and see if it works faster than what you're doing now. I think it will.

I hope that helps.