cahamilton Posted April 25, 2009 Share Posted April 25, 2009 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155618-solved-if-statement-problems/ Share on other sites More sharing options...
DjMikeS Posted April 25, 2009 Share Posted April 25, 2009 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/155618-solved-if-statement-problems/#findComment-819049 Share on other sites More sharing options...
RussellReal Posted April 25, 2009 Share Posted April 25, 2009 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155618-solved-if-statement-problems/#findComment-819052 Share on other sites More sharing options...
cahamilton Posted April 25, 2009 Author Share Posted April 25, 2009 Yeh some of it is left out, its part of of a foreach loop, but I only changed that specific segment last night. I couldn't remember what I had done, so I only posted that bit. Ill give it a shot now once my FTP decides to start working... Quote Link to comment https://forums.phpfreaks.com/topic/155618-solved-if-statement-problems/#findComment-819055 Share on other sites More sharing options...
cahamilton Posted April 25, 2009 Author Share Posted April 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/155618-solved-if-statement-problems/#findComment-819065 Share on other sites More sharing options...
DjMikeS Posted April 25, 2009 Share Posted April 25, 2009 Where does $news_content2 come from ? Please post that code... Quote Link to comment https://forums.phpfreaks.com/topic/155618-solved-if-statement-problems/#findComment-819066 Share on other sites More sharing options...
cahamilton Posted April 25, 2009 Author Share Posted April 25, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/155618-solved-if-statement-problems/#findComment-819067 Share on other sites More sharing options...
cahamilton Posted April 25, 2009 Author Share Posted April 25, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/155618-solved-if-statement-problems/#findComment-819074 Share on other sites More sharing options...
DjMikeS Posted April 25, 2009 Share Posted April 25, 2009 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>";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/155618-solved-if-statement-problems/#findComment-819076 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.