Checking if a variable exists within foreach if statement - php

In the below code, when checking foreach(get_sub_field("items") as $x):
How do I check if $x['item_name']; is empty, do this...
<?php $food = 23; ?>
<?php while(has_sub_field("menu_items", $food)): ?>
<?php if(get_row_layout() == 'food_items'): ?>
<?php if (get_sub_field('category')){?>
<h2>
<?php the_sub_field('category');?>
</h2>
<?php } ?>
<div>
<?php if(get_sub_field("items")): ?>
<?php foreach(get_sub_field("items") as $x): ?>
<div> <?php echo $x['item_name']; ?> <?php echo $x['item_price']; ?>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>

You can try this :
<?php if (empty($x['item_name'])) : ?>
<p>It's empty</p>
<?php endif; ?>

try empty()
<?php echo (!empty($x['item_name']) ? $x['item_name'] : 'do this'); ?>
or
if(!empty($x['item_name'])) {
echo $x['item_name'];
}
else {
// do your stuff
}

Related

Hide / show the part of code using php, if the value contains the letters

I have a problem with this scenario. There is a certain field (textarea) - $ COMMENTS. If the information in the $COMMENTS starts with a number (the first is a number and then any letter) - the script works fine (hide / show "some HTML and PHP code"), but if the $COMMENTS starts with the letter - the code does not work at all. Please tell me what is my mistake. I suffer already the 5th day.
Here is the script code, unfortunately it did not work correctly:
<?php $comments=$hm->Zb('rs:def:comments');
if ($comments!=0): ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php else : ?>
<div class="sorry">Sorry, there is no comments</div>
<?php endif; ?>
also didn't work like this:
<?php $comments=$hm->Zb('rs:def:comments');
if ($comments!=""): ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php else : ?>
<div class="sorry">Sorry, there is no comments</div>
<?php endif; ?>
Thank you all in advance for your help.
use isset() with if()
<?php $comments=$hm->Zb('rs:def:comments');
if (isset($comments)): ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php else : ?>
<div class="sorry">Sorry, there is no comments</div>
<?php endif; ?>
EDIT
Please try below code.
<?php $comments=$hm->Zb('rs:def:comments');
echo $comments; //for debugging to ensure $comments have value.
if ($comments){ ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php } else { ?>
<div class="sorry">Sorry, there is no comments</div>
<?php } ?>
http://php.net/manual/en/function.isset.php
try it : empty()
<?php $comments=$hm->Zb('rs:def:comments');
if (!empty($comments)): ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php else : ?>
<div class="sorry">Sorry, there is no comments</div>
<?php endif; ?>

PHP loop interject

Consider the following loop:
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value != ''): ?>
<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</div>
<?php endif; ?>
<?php endforeach; ?>
I wanted to wrap the first 12 items in a div and then the last 2 items in a div. The problem is there are not always 12 items exactly in the first div. There can be between 2 AND 12 items.
How would I manipulate this loop to achieve such? Many thanks
Just use a counter inside the loop to see how many times you've been through it.
<?php $count = 1; ?>
<?php $break= count($this->item->extra_fields) - 2; ?>
<?php echo "<div>"; ?>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value != ''): ?>
<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</div>
<?php $count++; ?>
<?php endif; ?>
<?php if ($count == $break) : ?>
<?php echo "</div><div>"; $count ==0; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php echo '</div>'; ?>

How to add "tags" in joomla 3.x blog layout

I would like to add the article tags in my blog layout in joomla 3.x.
I overwrote the joomla layout files and tried to add the code below in blog_style_default_item_title.php as it is in article
<?php if ($params->get('show_tags', 1) && !empty($this->item->tags)) : ?>
<?php $this->item->tagLayout = new JLayoutFile('joomla.content.tags'); ?>
<?php echo $this->item->tagLayout->render($this->item->tags->itemTags); ?>
<?php endif; ?>
but it did not work. I guess variable name is not the good one. Any ideas?
My knowledges in php language are pretty weak but I had a look and tried a few think.
I finaly got something by adding code below in /com_content/category/blog_items.php
<?php $this->item->tagLayout = new JLayoutFile('joomla.content.tags'); ?>
<?php echo $this->item->tagLayout->render($this->item->tags->itemTags); ?>
but i would like to add "tags" on the title line so in blog_style_default_item_title.phpfile
<?php
defined('_JEXEC') or die;
// Create a shortcut for params.
$params = $displayData->params;
$canEdit = $displayData->params->get('access-edit');
JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
JHtml::_('behavior.framework');
?>
<?php if ($params->get('show_title') || $displayData->state == 0 || ($params->get('show_author') && !empty($displayData->author ))) : ?>
<div class="page-header">
<?php if ($params->get('show_title')) : ?>
<h2>essai
<?php if ($params->get('link_titles') && $params->get('access-view')) : ?>
<a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($displayData->slug, $displayData->catid)); ?>">
<?php echo $this->escape($displayData->title); ?></a>
<?php else : ?>
<?php echo $this->escape($displayData->title); ?>
<?php endif; ?>
</h2>
<?php endif; ?>
<?php $this->item->tagLayout = new JLayoutFile('joomla.content.tags'); ?>
<?php echo $this->item->tagLayout->render($this->item->tags->itemTags); ?>
<?php if ($displayData->state == 0) : ?>
<span class="label label-warning"><?php echo JText::_('JUNPUBLISHED'); ?></span>
<?php endif; ?>
</div>
<?php endif; ?>
But i have a error???
Here is how they are added to individual weblinks
<?php $tagsData = $item->tags->getItemTags('com_weblinks.weblink', $item->id); ?>
<?php if ($this->params->get('show_tags', 1)) : ?>
<?php $this->item->tagLayout = new JLayoutFile('joomla.content.tags'); ?>
<?php echo $this->item->tagLayout->render($tagsData); ?>
<?php endif; ?>
What you would want to do is something similar but for com_content.article and make sure the $item and $this->params references match what you have.
Here a picture of the design I would like
And he below the way the page id made of (as i understood it)
in blog_item.php there is this line which call the
...
<?php echo JLayoutHelper::render('joomla.content.blog_style_default_item_title', $this->item); ?>
....
and so in blog_style_default_item_title.php
....
<div class="page-header">
<?php if ($params->get('show_title')) : ?>
<h2>essai
<?php if ($params->get('link_titles') && $params->get('access-view')) : ?>
<a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($displayData->slug, $displayData->catid)); ?>">
<?php echo $this->escape($displayData->title); ?></a>
<?php else : ?>
Did I make a mistake??

Error in template(SJ Financial Joomla) in sj_financial/html/com_content/category/blog_item.php

i installed SJ Financial Template joomla on my website caindiagroup.com,
but when i open article blog on my website, it show an error on sj_financial/html/com_content/category/blog_item.php file..
Fatal error: Class 'JLayoutHelper' not found in /home/caind126/public_html/templates/sj_financial/html/com_content/category/blog_item.php on line 25
Fatal error: Class 'JLayoutHelper' not found in /home/caind126/public_html/templates/sj_financial/html/com_content/category/blog_item.php on line 27
Fatal error: Class 'JLayoutFile' not found in /home/caind126/public_html/templates/sj_financial/html/com_content/category/blog_item.php on line 103
Fatal error: Call to a member function render() on a non-object in /home/caind126/public_html/templates/sj_financial/html/com_content/category/blog_item.php on line 104
My file Code is
`<?php
/**
* #package Joomla.Site
* #subpackage Layout
*
* #copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* #license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;?>
<?php
// Create a shortcut for params.
$params = $this->item->params;
$images = json_decode($this->item->images);
JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
$canEdit = $this->item->params->get('access-edit');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.framework');
?>
<?php if ($this->item->state == 0) : ?>
<span class="label label-warning"><?php echo JText::_('JUNPUBLISHED'); ?></span>
<?php //intro images ?>
<?php echo JLayoutHelper::render('joomla.content.intro_image', $this->item); ?>
<div class="article-text">
<?php echo JLayoutHelper::render('joomla.content.blog_style_default_item_title, $this->item'); ?>
<?php if (!$params->get('show_intro')) : ?>
<?php echo $this->item->event->afterDisplayTitle; ?>
<?php endif; ?>
<?php echo $this->item->event->beforeDisplayContent; ?>
<?php if ($params->get('show_intro')) : ?>
<?php echo $this->item->introtext; ?>
<?php endif; ?>
<div class="item-headinfo">
<?php if (!$params->get('show_intro')) : ?>
<?php echo $this->item->event->afterDisplayTitle; ?>
<?php endif; ?>
<?php echo $this->item->event->beforeDisplayContent; ?>
<?php // to do not that elegant would be nice to group the params ?>
<?php if (($params->get('show_author')) or ($params->get('show_category')) or ($params->get('show_create_date')) or ($params->get('show_modify_date')) or ($params->get('show_publish_date')) or ($params->get('show_parent_category')) or ($params->get('show_hits'))) : ?>
<dl class="article-info muted">
<!--<dt class="article-info-term"><?php //echo JText::_('COM_CONTENT_ARTICLE_INFO'); ?></dt>-->
<?php endif; ?>
<?php if ($params->get('show_parent_category')) : ?>
<dd class="parent-category-name">
<?php $title = $this->escape($this->item->parent_title);
$url = '' . $title . ''; ?>
<?php if ($params->get('link_parent_category')) : ?>
<?php echo JText::sprintf('COM_CONTENT_PARENT', $url); ?>
<?php else : ?>
<?php echo JText::sprintf('COM_CONTENT_PARENT', $title); ?>
<?php endif; ?>
</dd>
<?php endif; ?>
<?php if ($params->get('show_create_date')) : ?>
<dd class="create">
<i class="icon-calendar"></i>
<?php echo JText::sprintf( JHTML::_('date',$this->item->created, JText::_('DATE_FORMAT_LC'))); ?>
</dd>
<?php endif; ?>
<?php if ($params->get('show_modify_date')) : ?>
<dd class="modified">
<i class="icon-calendar"></i>
<?php echo JText::sprintf('COM_CONTENT_LAST_UPDATED', JHTML::_('date',$this->item->modified, JText::_('DATE_FORMAT_LC'))); ?>
</dd>
<?php endif; ?>
<?php if ($params->get('show_publish_date')) : ?>
<dd class="published">
<i class="icon-calendar"></i>
<?php echo JText::sprintf('COM_CONTENT_PUBLISHED_DATE', JHTML::_('date',$this->item->publish_up, JText::_('DATE_FORMAT_LC'))); ?>
</dd>
<?php endif; ?>
<?php if ($params->get('show_author') && !empty($this->item->author )) : ?>
<dd class="createdby">
<?php $author = $this->item->author; ?>
<?php $author = ($this->item->created_by_alias ? $this->item->created_by_alias : $author);?>
<?php if (!empty($this->item->contactid ) && $params->get('link_author') == true):?>
<?php echo JText::sprintf('COM_CONTENT_WRITTEN_BY' ,
JHTML::_('link',JRoute::_('index.php?option=com_contact&view=contact&id='.$this->item->contactid),$author)); ?>
<?php else :?>
<?php echo JText::sprintf('COM_CONTENT_WRITTEN_BY', $author); ?>
<?php endif; ?>
</dd>
<?php endif; ?>
<?php if ($params->get('show_hits')) : ?>
<dd class="hits">
<?php echo JText::sprintf('COM_CONTENT_ARTICLE_HITS', $this->item->hits); ?>
</dd>
<?php endif; ?>
<?php if ($this->params->get('show_tags', 1)) : ?>
<dd class="item-tags">
<?php $this->item->tagLayout = new JLayoutFile('joomla.content.tags'); ?>
<?php echo $this->item->tagLayout->render($this->item->tags->itemTags); ?>
</dd>
<?php endif; ?>
<?php if (($params->get('show_author')) or ($params->get('show_category')) or ($params->get('show_create_date')) or ($params->get('show_modify_date')) or ($params->get('show_publish_date')) or ($params->get('show_parent_category')) or ($params->get('show_hits'))) :?>
</dl>
<?php endif; ?>
</div>
<?php if ($params->get('show_readmore') && $this->item->readmore) :
if ($params->get('access-view')) :
$link = JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid));
else :
$menu = JFactory::getApplication()->getMenu();
$active = $menu->getActive();
$itemId = $active->id;
$link1 = JRoute::_('index.php?option=com_users&view=login&Itemid=' . $itemId);
$returnURL = JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid));
$link = new JURI($link1);
$link->setVar('return', base64_encode($returnURL));
endif; ?>
<a class="readmore" href="<?php echo $link; ?>">
<?php if (!$params->get('access-view')) :
echo JText::_('COM_CONTENT_REGISTER_TO_READ_MORE');
elseif ($readmore = $this->item->alternative_readmore) :
echo $readmore;
if ($params->get('show_readmore_title', 0) != 0) :
echo JHtml::_('string.truncate', ($this->item->title), $params->get('readmore_limit'));
endif;
elseif ($params->get('show_readmore_title', 0) == 0) :
echo JText::sprintf('COM_CONTENT_READ_MORE_TITLE');
else :
echo JText::_('COM_CONTENT_READ_MORE');
echo JHtml::_('string.truncate', ($this->item->title), $params->get('readmore_limit'));
endif; ?>
<i class="ico-arrow-right"></i>
</a>
<?php endif; ?>
</div>
<?php if ($this->item->state == 0) : ?>
</div>
<?php endif; ?>
<?php echo $this->item->event->afterDisplayContent; ?>`
Plz help me..
I'm not sure if it helps, but you can try to add following line just after defined('_JEXEC') or die;
require_once(JPATH_ROOT . '/libraries/cms/layout/helper.php');

Using if with my foreach structure

I am using the following structure for my foreach how would I use an if statement within the same format?
<?php foreach($property as $section):?>
if(!empty($section))
{
echo <p>data here </p>;
}else{
html
}
<?php endforeach;?>
<?php foreach($property as $section):
if(!empty($section)):
echo "<p>data here </p>";
endif;
endforeach;?>
OR
<?php foreach($property as $section):
if(!empty($section)):
echo "<p>data here </p>";
else:
echo "html";
endif;
endforeach;?>
See: Alternative Syntax
Not sure what you mean, but you don't have valid php.
foreach($property as $section) {
if(!empty($section))
{
echo '<p>data here </p>';
}else{
echo 'html';
}
}
I think you are looking for this.
<?php foreach($property as $section): ?>
<?php
if(!empty($section)) :
echo <p>data here </p>;
else :
html
endif;
endforeach; ?>
I think best solution for this is:
<?php
foreach($property as $section) {
if(!empty($section))
{
echo '<p>data here </p>';
}
else
{
?>
<div id="dosome">really crazy html stuff here</div>
<?php
}
}
?>
This is an example containing }elseif{ also:
<?php foreach($property as $section):?>
<?php if(!empty($section)): ?>
<p>data here </p>
<?php elseif ([another condition]): ?>
...
<?php else: ?>
html
<?php endif: ?>
<?php endforeach;?>
The entire documentation is here: http://php.net/manual/en/control-structures.alternative-syntax.php
SHORTHAND :
<?php foreach($property as $section):?>
<?php if(!empty($section)):?>
<p>data here </p>;
<?php else: ?>
html
<?php endif;?>
<?php endforeach;?>
OTHER:
<?php foreach($property as $section)
{
if(!empty($section))
{
echo '<p>data here </p>';
}else{
echo 'html';
}
}
?>

Categories