Jump to content

if else failing (but working in HannaCode)


Peter Knight
 Share

Recommended Posts

Hi guys

The following works properly within a HannaCode when I hit "Save and Test" and view the test results in the back end (HannaCode has a preview feature).

However, when I view the page on the website, I get no output and no errors

<?php
$news = $pages->find("template=news-item, sort=-post-date");
foreach ($news as $new)
    
if ($new->body){
	echo"
        <div class='uk-width-1-1 news-headline-wrap'>
        <i class='uk-icon-newspaper-o uk-icon-justify'></i><a href='{$new->url}'>{$new->title}</a>
        </div>
        ";
}
		
else {
	
	echo"
        <div class='uk-width-1-1 news-headline-wrap'>
        <i class='uk-icon-newspaper-o uk-icon-justify'></i>{$new->title}
        </div>
        ";
        
}
	
?>

I might be outputting a few stray brackets but then again - no errors!

Any ideas? If I don't get any progress, I'll try calling it directly within a template instead.

Link to comment
Share on other sites

You're mostly missing the curly braces for the foreach, which is only allowed if only a single following statement is supposed to be foreached.

$news = $pages->find("template=news-item, sort=-post-date");
foreach ($news as $newsItem){
        $title = $newsItem->body
                ? "<a href='{$newsItem->url}'>{$newsItem->title}</a>"
                : $newsItem->title;
        $icon = "<i class='uk-icon-newspaper-o uk-icon-justify'></i>";
        echo "<div class='uk-width-1-1 news-headline-wrap'>{$icon}{$title}</div>";
}
  • Like 1
Link to comment
Share on other sites

Thanks LostKobrakai

I've never seen your code example in the wild before. It looks clever, interesting and less duplicate than mine.

What's the "?" for. Is that some PHP shorthand for "if" ?

My code worked once I took it out of a HannCode and applied it directly into a template instead.

It might be something funky going on with the body field as I know HannaCode has a (probably unrelated issue with body fields.

Link to comment
Share on other sites

LostKobrakai's example uses the ternary operator, which isn't the easiest thing to get your head round when used simply, and can very quickly get out of hand. (Nested ternaries, anyone?)

<?php
$news = $pages->find("template=news-item, sort=-post-date");
foreach ($news as $new){
    
   if ($new->body){
	echo"
        <div class='uk-width-1-1 news-headline-wrap'>
        <i class='uk-icon-newspaper-o uk-icon-justify'></i><a href='{$new->url}'>{$new->title}</a>
        </div>
        ";
		
   } else {
	
	echo"
        <div class='uk-width-1-1 news-headline-wrap'>
        <i class='uk-icon-newspaper-o uk-icon-justify'></i>{$new->title}
        </div>
        ";
        
   }
}	
?>

Just add a couple of curly brackets as above and you're good to go.


Looks like Kongondo and I were in synch there.  ^-^

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...