SimplePie RSS Not Getting pubDate - php

I am using SimplePie RSS to aggregate 4 feeds and they are being sorted by the date (descending) and in the code it is set to echo out the pubDate but it is not showing it. It just prints a blank element.
For sanaties sake (as the code file is tens of lines long I have it in a *.txt file on my server which can be found here: http://feeds.powercastmedia.net/feeds.php.txt
I am completely lost.
Cheers!,
Phill

Try placing other information in the echo calls to ensure that those lines are actually being called, and that the output is being displayed in the expected manor -
<title><? echo "Title: ".$item->get_title(); ?></title>
<link><? echo "Permalink: ".$item->get_permalink(); ?></link>
<pubDate><? echo "PubDate: ".$item->get_date(); ?></pubDate>
<description><? echo "Description: ".$item->get_description(); ?></description>
This kind of "debug output" can help with debugging all sorts of things. It should help you figure out exactly where the problem is originating from.
Also, I noticed that you have numerous unnecessary PHP opening and closing tags, where multiple lines could be consolidated into a single, cleaner code block (Ex:)
<?php if ($success): ?>
<? $itemlimit=0; ?>
<?php foreach($feed->get_items() as $item): ?>
<? if ($itemlimit==10) { break; } ?>
Could be cleaned up to be:
<?php
if($success)
{
$itemlimit = 0;
$items = $feed->get_items(); // This might also help, as PHP sometimes has issues when iterating through arrays returned directly from functions
foreach($items as $item)
{
if($itemlimit == 0) break;
...
In fact, most of the file could be within one pair of PHP tags.
Just a suggestion.

Related

insert line break php function

I have an issue where I'm showing boxes of some posts from my database in a foreach loop.
The length of the post title affects the design. For example if one post has a long title next to one with a short title, it will push the link i have below down. This makes it look uneven.
Therefor I'm trying to write a function that checks if the length is too short, it should insert a line break.
This is what I have so far.
function insert_line_break($text){
if (strlen($text) < 10 ) {
echo "<br>";
}
}
<?= insert_line_break($entry["title"]) ?>
However that seems to replace the title with a linebreak.
What am I missing?
You have missed to echo the text itself.
function insert_line_break($text){
if (strlen($text) < 10 ) {
return "<br>";
}
}
<?php echo $entry["title"] . insert_line_break($entry["title"]); ?>
Note:
I changed the function to return a value instead of echoing it to the output, so it will be much more reusable in the future.
And I expanded the short tag <? to <?php and made the = to verbose echo which is much clearer and readable way of coding.
And of course I closed the line with semicolon ; as it should be.
EDIT:
<?php echo $entry["title"] . insert_line_break($entry["title"]); ?>
echo prints the content of $entry["title"] and then ask function insert_line_break($entry["title"]) to decide whether it contains string shorter than 10 characters, if so it returns <br> that is echoed. And that's it.

code for xml feed not working correctly

I'm trying to print an xml feed into my php page,but this code is not working correctly, and i have no idea why. It just shows the code as it is on the browser from xpath to ?> . Can anyone help me with this please
<html>
<head>
<title>XML FEED</title>
</head>
<body>
<ul>
<?php
$dom = simplexml_load_file("http://feeds.bbci.co.uk/news/rss.xml");
foreach ($dom->xpath("/channel/item") as $item)
{
print "<li>";
print $item->title;
print "</li>";
}
?>
</ul>
</body>
</html>
Consider adjusting your XPath with double forward slashes as channel is not the root element:
foreach ($dom->xpath("//channel/item") as $item) {
...
}
Alternatively, use the root, rss, element in expression:
foreach ($dom->xpath("/rss/channel/item") as $item) {
...
}
I suspect your server is not configured to allow the short PHP tags (which is discouraged anyway) so Always use the full opening tag.
Since PHP is not parsing your code, the browse gets sent the following...
<? . . . . . foreach ($dom->
Which is not valid HTML, so it is not displayed, but if you view the source of your page, you will see more of your PHP code.
Simply starting your code with <?php will trigger PHP parsing, and things should work.

Reason for not able to identify pointer in localhost

I am going to retrieve some data from database and display it in php file.
It seems like it can connect to my localhost's database,because code like:
<?php foreach($goods as item ):?>
haven't occur any errors.
but when it comes to the codes like,
<?php echo $item->logo;?>
on my browser, it simply display
logo;?>
what's wrong with my codes or setting.
As the file is quite big and I think the system config problem(I have reinstalled wampserver), I just show a little bit
my code:
<?php echo sizeof($goods);?>
<td class="td_f"><IMG src="http://127.0.0.1:8020/UB_real//public/photos/frontimg/<?php echo $item->logo?>"> </td>
<?php endforeach; ?>
You have an error in your code:
Where you said:
<?php foreach($goods as item ):?>
Must be:
<?php foreach($goods as $item ); ?>
Note the missing $ before item, and the semicolon ; not the :
If you need to list all the returned values in $goods (for testing purposes), you can always do:
echo '<pre>';
print_r($goods);
echo '</pre>';

Extracting basic PHP echo from IF statement

The following code is for a Wordpress plugin, it displays points and tank of a user:
<?php
if(function_exists('cp_displayPoints') && $authordata->ID){
echo '<span class="cubepoints_buddypress">'; cp_displayPoints($authordata->ID); echo '</span>';
if(function_exists('cp_module_ranks_getRank')) echo ' <span class="cupepoints_buddypress_rank">'.cp_module_ranks_getRank($authordata->ID).'</span>';
}
?>
I am trying to extract these two echo functions from the If statement but only succeeded with one of them. I can echo the points like this:
<?php cp_displayPoints($authordata->ID); ?>
Works fine. Now I tried doing the same with the second echo:
<?php cp_module_ranks_getRank($authordata->ID); ?>
But it did not work. Obviously, there is some basic thing that I am missing here. Do you know what it is?
The first one likely prints directly to output, while the second returns its value. So, you need to echo() the second one, just as they're doing in your sample code:
<?php echo cp_module_ranks_getRank($authordata->ID); ?>

Nesting Echo's or output variable differently?

I am trying to check to make sure a custom-field is not blank before echoing the custom-field.
This is what I have
<?php
$key = 'one_line_summary';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta != '') {
echo '<blockquote><?php echo get_post_meta($post->ID, one_line_summary, true); ?></blockquote>';
}
?>
But it out puts the "get_post_meta($post->ID, one_line_summary, true);" literally rather than the contents of the variable one_line_summary.
I am a beginner but I feel like I need to either use nested echo's somehow or change the second echo all together?
Thanks in advance.
You have nested <?php ?> inside an existing set of PHP tags, which is not allowed. Remove those, and concatenate in the function call to get_post_meta(). What happened here is that the inner <?php ?> tags were output as strings to the browser, but not rendered onscreen (since the browser treated them as unknown HTML tags).
echo '<blockquote>' . get_post_meta($post->ID, one_line_summary, true) . '</blockquote>';
As a note, these kinds of issues are considerably easier to spot with proper code indentation as was done when your post was edited above.

Categories