Peter Knight Posted June 13, 2016 Posted June 13, 2016 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.
LostKobrakai Posted June 13, 2016 Posted June 13, 2016 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>"; } 1
Peter Knight Posted June 13, 2016 Author Posted June 13, 2016 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.
kongondo Posted June 13, 2016 Posted June 13, 2016 What's the "?" for. Is that some PHP shorthand for "if" ? yeah...ternary operator.. https://davidwalsh.name/php-shorthand-if-else-ternary-operators
DaveP Posted June 13, 2016 Posted June 13, 2016 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. 1
Peter Knight Posted July 13, 2016 Author Posted July 13, 2016 Very cool. I've shortened some massive if/else statements since reading this. 1
LostKobrakai Posted July 13, 2016 Posted July 13, 2016 Shortening and possibly avoiding if/else statement is such a great improvement to code 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now