Jump to content

Recommended Posts

Can anyone figure out why this IF statement isn't currently working. I was working on part of my website last night and altered the code slightly. Now for some reason, I cant figure out what I did and how to fix it.

 

		<?php 

		if ($news_content2 != "") { 	

				echo "
				<ul class=\"news_item\">
					<li><h2>$news_title2</h2></li>
					<li><span class='news_date'>$news_date2</span></li>
					<li>$news_content2...</li> 
					<li><a href=\"$news_article_url\" title='$news_title2' class='readmore' target='_blank'>Read More</a></li>
				</ul>";	}		
			  					

		elseif ($news_content2 == "") { 	

				echo "<p class='error'>Sorry, News For This Artist is Currently Unavailable.</p>";
				echo "<img src='images/error.gif' alt='Error' title='Error' class='center' /> ";
				echo "<p class='error'>Please try again Later</p>"; }


		?> 
    
            <?php } ?>

Link to comment
https://forums.phpfreaks.com/topic/155618-solved-if-statement-problems/
Share on other sites

you really write your code like that ? or did you just leave out stuff ?

 

<?php 
if ($news_content2 != "") {    
    echo "<ul class=\"news_item\">
            <li><h2>$news_title2</h2></li>
            <li><span class='news_date'>$news_date2</span></li>
            <li>$news_content2...</li> 
            <li><a href=\"$news_article_url\" title='$news_title2' class='readmore' target='_blank'>Read More</a></li>
            </ul>";
} elseif ($news_content2 == "") {      
    echo "<p class='error'>Sorry, News For This Artist is Currently Unavailable.</p>
            <img src='images/error.gif' alt='Error' title='Error' class='center' />
            <p class='error'>Please try again Later</p>"; 
}
?>

 

Seems like a whole lot cleaner doesn't it ?

I just rewrote it, I wouldn't see an error in this... other than 1 too many closing brackets

<?php
if ($news_content2 != "") {            
echo <<<A
<ul class="news_item">
	<li><h2>$news_title2</h2></li>
	<li><span class='news_date'>$news_date2</span></li>
	<li>$news_content2...</li> 
	<li><a href="$news_article_url" title='$news_title2' class='readmore' target='_blank'>Read More</a></li>
</ul>
A;
}
elseif ($news_content2 == "") {    
echo "<p class='error'>Sorry, News For This Artist is Currently Unavailable.</p>";
echo "<img src='images/error.gif' alt='Error' title='Error' class='center' /> ";
echo "<p class='error'>Please try again Later</p>";
}
?>

No it doesnt work. I really cant see why. Any item that contains data echoes out fine, but if there anything that doesnt include data, the error message isn't echoing out for some reason. I cant figure it out, it was working fine last night. I cant remember what part of it I changed, which would be quite nice to know lol.

 

Just an example

Artist with News

http://cahamilton.at-uclan.com/fyp/query.php?artist=Muse

 

Artist without News

http://cahamilton.at-uclan.com/fyp/query.php?artist=Bad+Astronaut+vs.+Armchair+Martian

 

As you can see, the news echoes out nicely without a problem. With the artist with no news, nothing happens. It should generate an error message like the one below it.

The whole page code

 

<?php 	
	$news_url = 'http://www.idiomag.com/api/artist/articles/rss?key=42306fb6d05692ad116515c562f48254&artist=';
	$artist_name = str_replace(" ","+",$artist_name);

	$artist_name_normalised = normaliser($artist_name);	 //takes out special charcters for news search
	$news_result = $news_url.$artist_name_normalised;

	// read feed into SimpleXML object
	$sxml_news = @simplexml_load_file($news_result);?>
      	<h1 class="news_title">News Content</h1>
      
    <div id="news_stream">   
      
      
	<?php


	if($sxml_news && $sxml_news)

        // iterate over entries in feed
        foreach ($sxml_news->channel->item as $news_entry) {

		// Get news title
		$news_title = $news_entry->title;
		$news_title2 = ucwords($news_title);		// sentence case


		// Date of publishment
		$news_date = $news_entry->pubDate;
		$news_date2 = str_replace(" +0100"," ",$news_date);  // take out +0100 timezone from date in entry

		// URL to the news article
		$news_article_url = $news_entry->source;

		// Author of Article
		$news_author = $news_entry->author; 

		$news_content = $news_entry->description;
		$news_content2 = strip_tags($news_content,'');
		$news_content3 = substr($news_content2,0,200); 


		?>
            						

                
            <?php 
		if ($news_content2 != "") {    
			echo   "<ul class=\"news_item\">
					<li><h2>$news_title2</h2></li>
					<li><span class='news_date'>$news_date2</span></li>
					<li>$news_content2...</li> 
					<li><a href=\"$news_article_url\" title='$news_title2' class='readmore' target='_blank'>Read More</a></li>
					</ul>";

		} elseif ($news_content2 == "") {      
			echo "<p class='error'>Sorry, News For This Artist is Currently Unavailable.</p>
				  <img src='images/error.gif' alt='Error' title='Error' class='center' />
				  <p class='error'>Please try again Later</p>";}

		?>    	

            <?php } ?>
</div> 

Bah fuck. I've figured it out. Thanks everyone for helping. If your interested, heres what fixed it:

 



<?php 
if ($news_content2 != "") {    
echo   "<ul class=\"news_item\">
<li><h2>$news_title2</h2></li>
<li><span class='news_date'>$news_date2</span></li>
<li>$news_content2...</li> 
<li><a href=\"$news_article_url\" title='$news_title2' class='readmore' target='_blank'>Read More</a></li>
</ul>";}
?>    	

<?php } ?>

<?php 
if ($news_content2 == "") {      
echo "<p class='error'>Sorry, News For This Artist is Currently Unavailable.</p>
<img src='images/error.gif' alt='Error' title='Error' class='center' />
<p class='error'>Please try again Later</p>";}
?>

</div> 

Why would you need to do an if and elseif statement ?

 

Seems to me that if news_content2 != "" it is automagically ""

So this should be enough:

 

<?php 
if ($news_content2 != "") {    
echo   "<ul class=\"news_item\">
<li><h2>$news_title2</h2></li>
<li><span class='news_date'>$news_date2</span></li>
<li>$news_content2...</li> 
<li><a href=\"$news_article_url\" title='$news_title2' class='readmore' target='_blank'>Read More</a></li>
</ul>";

} else {
echo "<p class='error'>Sorry, News For This Artist is Currently Unavailable.</p>
<img src='images/error.gif' alt='Error' title='Error' class='center' />
<p class='error'>Please try again Later</p>";}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.