Using if with my foreach structure - php

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';
}
}
?>

Related

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>'; ?>

Checking if a variable exists within foreach if statement

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
}

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??

php multiple if's but not related

I have a WP site but this is a PHP question, I have the following code:
f(!empty($c_dates1_new)) { ?>
<p class="text">Dates: From <?php echo $c_dates1_new; ?> to <?php echo $c_datee1_new; ?></p>
<?php } if(!empty($c_dates2_new)) { ?>
<p class="text">Dates 2: From <?php echo $c_dates2_new; ?> to <?php echo $c_datee2_new; ?></p>
<?php } if(!empty($c_dates3_new)) { ?>
<p class="text">Dates 3: From <?php echo $c_dates3_new; ?> to <?php echo $c_datee3_new; ?></p>
<?php } if(!empty($c_dates4_new)) { ?>
<p class="text">Dates 4: From <?php echo $c_dates4_new; ?> to <?php echo $c_datee4_new; ?></p>
<?php }
Now this only shows the content from $c_dates_1 where it should display the content from all of them.
I cannot workout what I need to put between them e.g. it's not an else, it's not an OR and it's not an AND as they are all separate checks, not related to each other.
any input is appreciated

How do I hide HTML tags with PHP?

How do I hide the html tags when the user is not logged on for the users name to be display?
<li><?php if (isset($_SESSION['user_id'])) { echo $_SESSION['first_name']; } ?></li>
<?php if (isset($_SESSION['user_id']) { ?>
<li><?php echo $_SESSION['first_name']; } ?></li>
<?php } ?>
or with short tags:
<? if (isset($_SESSION['user_id']) { ?>
<li><?= $_SESSION['first_name']; } ?></li>
<? } ?>
You can also use the alternate PHP control structures, which arguably make it more readable:
<?php if (isset($_SESSION['user_id']): ?>
<li><?php echo $_SESSION['first_name']; } ?></li>
<?php endif; ?>
With PHP you determine whether to spit out any content, you don't need to "hide" it per se like CSS...
<?php
if ( isset( $_SESSION['user_id'] ) ) {
?>
<li><?php echo $_SESSION['user_id'];?></li>
<?php
} ?>
Like this:
<?php if (isset($_SESSION['user_id'])) { echo "<li>"; echo $_SESSION['first_name']; echo "</li>"; } ?>
Like this.
<?php if ($_SESSION['user']['id']):?>
<p>
Hi <?=$_SESSION['user']['name'];?>, you are logged in!
</p>
<?php else:?>
Sign in or Register
<?php endif;?>

Categories