Put URL in variable if another variable is empty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ourmaninparis
Forum Newbie
Posts: 21
Joined: Wed Apr 18, 2012 8:09 am

Put URL in variable if another variable is empty

Post by ourmaninparis »

Hello,

I have a difficult one. I've built an RSS feed and included the 'enclosures' tag. I need to include a full URL rather than just the path for the enclosure so I've prefixed it with my site name and then added the path name from the database. The issue I have is that the site url displays in RSS readers when an RSS item does not have an attachment. I therefore need a way of just including URLs that contain files in the enclosures tag.

My current code is

Code: Select all

for($i=0;$i<$count; $i++) {

$subject = mysql_result($result,$i,'t1.Notice_Title');
$description = mysql_result($result,$i,'t1.Notice_Text');
// Clean the description
$description = str_replace ("&","",htmlspecialchars(strip_tags($description)));
$link = mysql_result($result,$i,'t1.Notice_Link');
//to record when the feed was published
$pubdate = mysql_result($result,$i,'t2.publish_up');

//format the date
$format = 'D, d M Y H:i:s O'; //or what ever format you choose (see the reference below)
$pubdate = date_format(date_create($pubdate), $format);
//get the image path then remove the first part of absolute path
$image = mysql_result($result,$i,'t1.Image');
$cropimagepath = substr($image, 26);
//get the enclosure path then remove the first part of absolute path
$attachment = mysql_result($result,$i,'t1.Attachment');
$cropattachpath = substr($attachment, 26);

echo '
<item>
<title>'.$subject.'</title>
<link>'.$link.'</link>
<description><![CDATA[<img src="http://www.mysite.com'.$cropimagepath.'" align=left>]]>
'.$description.'</description>
<pubDate>'.$pubdate.'</pubDate>
<enclosure url="http://www.mysite.com'.$cropattachpath.'" length="" type="application/pdf" />
</item>
';
I was thinking of querying the variable $attachment to see if it was empty. If it isn't empty then I can build my url and include the final variable in the enclosure area. This is as far as I've got (needless to say it does not work):

Code: Select all

if (!empty($attachment) {
$cropattachpath = substr($attachment, 26);
$attachfinal = "http://www.mysite.com/'.$cropattachpath.'";
}
Then I include $attachfinal in the enclosure:

Code: Select all

<enclosure url="'.$attachfinal.'" length="200" type="application/pdf" />
I am not incredibly good at this type of thing (marketing person actually!!) and am stuck now.

Any ideas?

Shaun
ourmaninparis
Forum Newbie
Posts: 21
Joined: Wed Apr 18, 2012 8:09 am

Re: Put URL in variable if another variable is empty

Post by ourmaninparis »

Got it working. Had missed a closing bracket and a semi colon. Also added an else to be sure that attachfinal would be empty:

Code: Select all

if (!empty($attachment)) 
{
$cropattachpath = substr($attachment, 26);
$attachfinal = "http://www.mysite.com/.$cropattachpath.";
}
else 
{
$attachfinal = "";
}
Post Reply