How to simplify this PHP conditional statement? - php

I am trying to understand a simple / better way of coding something like this php conditional statement.
<?php if (count($foo_bar) > 1) : ?>
<div class="myDiv1">
Hello!
</div>
<?php endif; ?>
<?php if (count($foo_bar) == 1) : ?>
<div class="myDiv2">
Goodbye!
</div>
<?php endif; ?>
Looking for example, and explanation as to why it may be better. Thanks!

Quite simply in this specific situation you dont need the second if as it can be accomplished with a simple if else
<?php if (count($foo_bar) > 1) : ?>
<div class="myDiv1">
Hello!
</div>
<?php else: ?>
<div class="myDiv2">
Goodbye!
</div>
<?php endif; ?>

<?php
$class = 'myDiv2';
$msg = 'GoodBye!';
if (count($foo_bar) > 1) {
$class = 'myDiv1';
$msg = 'Hello!';
}
?>
<div class="<?php echo $class; ?>">
<?php echo $msg; ?>
</div>

Try using an elseif like below. A bit more compact than the 2 statements.
<?php if (count($foo_bar) == 1) : ?>
<div class="myDiv2">
Goodbye!
</div>
<?php elseif(count($foo_bar) > 1): ?>
<div class="myDiv1">
Hello!
</div>
<?php endif; ?>

First, you should the count function only once.
Second, reduce the duplicated HTML blocks.
<?php
$countFooBar = count($foo_bar);
if ($countFooBar == 1){
$message = 'Goodbye! !';
$cssClass = 'class1';
}elseif( $countFooBar > 1){
$message = 'Hello!'
$cssClass = 'class2';
}
?>
<div class="<?php echo $cssClass ?>">
<?php echo $message; ?>
</div>

Related

cutting title character in joomla contant module

i need your help in this case; i have module in joomla and want to cutting title with specified a limit from joomla library string.php. i change this code :
<?php if ($params->get('show_title', 1)) : ?>
<h3 itemprop="name">
<?php if ($params->get('link_titles') && $params->get('access-view')) : ?>
<?php echo $this->escape($displayData->title); ?>
<?php else : ?>
<?php echo $this->escape($displayData->title); ?>
<?php endif; ?>
</h3>
to this code :
<?php if ($params->get('show_title', 1)) : ?>
<h3 itemprop="name">
<?php if ($params->get('link_titles') && $params->get('access-view')) : ?>
<a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($displayData->slug, $displayData->catid)); ?>" itemprop="url">
<?php
$limit =100;
if (strlen($this->item->text) > $limit) {
echo (substr($this->item->text, 0, $limit)) . " ... ";
}
else {
echo $this->escape($displayData->title); } ?></a><?php else : ?>
<?php endif; ?>
</h3>
but not work.
thanks for your attention guys.
Joomla has a built-in JHtmlString/truncate method which you can use, I've had good success using it with some of our templates and overrides.
This method would let you simplify your code and you could replace you entire last php block with something like the following
<?php
$limit =100;
echo JHTML::_('string.truncate', ($this->item->text), $limit, false, false);
?>
More about JHtmlString/truncate: https://docs.joomla.org/API16:JHtmlString/truncate
Some example code which might be helpful:
https://gist.github.com/2dpi/a540527a64f9f0093392
https://hotexamples.com/examples/-/JHtmlString/truncateComplex/php-jhtmlstring-truncatecomplex-method-examples.html
Good luck!

This php if else code on my website is not working [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
<?php if (get_the_author_meta('description')) { ?>
<?php
$author_ID = get_the_author_meta('ID');
$username = get_the_author_meta('display_name', $author_ID); ?>
<div class="mm-author-box">
<figure class="mm-author-box-avatar">
<?php echo get_avatar($author_ID, 70); ?>
</figure>
<?php if ($author_ID === 4) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
</div>
<?php } ?>
<?php else { ?>
<?php if ($author_ID === 9) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
</div>
<?php } ?>
<?php } ?>
It is a code to display author name and hyper link it.
From the first if statement if author has a description then go inside
Second if statement: if authors ID is 4 then execute the code below if not then
Else, there is one extra div inside the else statement which is for <div class="mm-author-box"> which is outside the if statement.
The problem is that when I put this code it breaks the page.. Only the header of the website loads and the content below it doesn't because i have placed this code in the php file which is a template for the page content.
I think there is some syntax problem coz I used the code without else statement and it worked.
You missed to close a curly bracket in the end. Add below line as a last line and try :
<?php } ?>
I don't know how strict that template engine is, but this could be the problem;
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
</div>
<?php } ?>
You're closing the div inside the if which was started outside the div. So , place that last </div> below the <?php } ?> and see if that helps
<?php if (get_the_author_meta('description')) {
$author_ID = get_the_author_meta('ID');
$username = get_the_author_meta('display_name', $author_ID); ?>
<div class="mm-author-box">
<figure class="mm-author-box-avatar">
<?php echo get_avatar($author_ID, 70); ?>
</figure>
<?php if ($author_ID === 4) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
<?php } else if ($mh_author_ID === 9) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
<?php } ?>
</div>
<?php } ?>
And as AnkiiG pointed out, you missed a closing tag, which I fixed without knowing while formatting my answer
Your if else structure is like this,
<?php if (get_the_author_meta('description')) { ?>
<?php if ($author_ID === 4) { ?>
<?php } ?>
<?php else { ?>
<?php if ($mh_author_ID === 9) { ?>
<?php } ?>
You're not closing the first if statement

ACF / PHP Repeater count

I have a repeater using Advanced Custom fields, which when a count reaches a total (of 3) based on an iterative count, I would like to output a div, then reset the counter.
Here's the code, at the moment it's not outputting as it should and I don't know enough about math to get it to work.
Appreciate your assistance :-)
<?php if(have_rows('flexible_row')) : ?>
<div class="row">
<?php if(empty($count)){ $count=1;} while(have_rows('flexible_row')) : the_row(); ?>
<?php if(get_sub_field('column_width') == "One column") { $count = $count+1; ?>
<div class="col-sm-4">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $count; ?>
<hr/>
</div>
<?php } if(get_sub_field('column_width') == "Two columns") { $count = $count+2; ?>
<div class="col-sm-8">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $counter; ?>
<hr/>
</div>
<?php } else { $count = $count+3; ?>
<div class="col-sm-12">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $counter; ?>
<hr/>
</div>
<?php } if ($count == 3) { ?></div><div class="row"><?php $count = 0; } ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
Think I'd got my logic mixed up a bit — I wasn't using the else clauses correctly. Here's the final working code in case anyone stumbles across this in the future:
<?php if(have_rows('flexible_row')) : ?>
<div class="row">
<?php if(empty($count)){ $count=0;} while(have_rows('flexible_row')) : the_row(); ?>
<?php if(get_sub_field('column_width') == "One Column") { $count = $count+1; ?>
<div class="col-sm-4">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } elseif(get_sub_field('column_width') == "Two Columns") { $count = $count+2; ?>
<div class="col-sm-8">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } else { $count = $count+3; ?>
<div class="col-sm-12">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } if ($count == 3) { ?></div><div class="row"><?php $count = 0; } ?>
<?php endwhile; ?>
</div>
You could of course use switch statements too..

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

How can I better this code?

I have this code :
<div class="boxContentScroll">
<?
while($row=mysql_fetch_array($query, MYSQL_NUM)) {
?>
<div class="boxAddCategory<? if($row[1]==1) echo "Yes"; else echo "No"; ?>">
<div class="boxAddCategory1">
<? echo $row[0]."<br />"; ?>
</div>
<div class="boxAddCategory2">
<?
if((isset($_SESSION['admin'])) && ($_SESSION['admin']==1)) {
?>
<input type="checkbox" <? if($row[1]==1) echo "checked='checked'" ?> value="categories[]" />
<?
} else echo " "
?>
</div>
</div>
<?
}
?>
</div>
but is not so good read it (just watch the number of ?> or <?). What can you suggest to improve it? Thanks
If you want this code to run on all environments, don't use short_tags (<?). But if you're running this on your own server, you can disregard it.
Use PHP's alternative syntax for control structures. This will make it much more readable.
Don't mix your business logic with your view logic. Either setup your own MVC stack or use a templating engine if you want.
Well I would start with replacing if with short version:
<?php echo ($row[1]==1) ? "Yes" : "No"; ?>
You could write this:
<? if($row[1]==1) echo "Yes"; else echo "No"; ?>
like this:
<?= $row[1] == 1 ? 'Yes' : 'No' ?>
And you could replace this:
if((isset($_SESSION['admin'])) && ($_SESSION['admin']==1)) {
with this:
if ( $admin ) {
and put this before the while loop:
$admin = isset($_SESSION['admin']) && $_SESSION['admin'] == 1;
Any time you're in a loop and have a static condition to check like this:
if((isset($_SESSION['admin'])) && ($_SESSION['admin']==1))
Define it in a variable it outside the loop. If it won't change within the loop, you don't have to check each time, only once. This becomes relevant when doing resource intensive checks and function calls that won't produce a different result outside the loop.
<div class="boxContentScroll">
<?php while($row=mysql_fetch_array($query, MYSQL_NUM)) : ?>
<div class="boxAddCategory <?=($row[1] == 1 ? 'Yes' : 'No')?>">
<div class="boxAddCategory1">
<?=$row[0].'<br />'?>
</div>
<div class="boxAddCategory2">
<?php if((isset($_SESSION['admin'])) && ($_SESSION['admin']==1)) : ?>
<input type="checkbox" <?=($row[1]==1 ? 'checked="checked"' : '')?> value="categories[]" />
<?php endif; ?>
</div>
</div>
<?php endwhile; ?>
</div>
Using a HEREDOC:
<div class="boxContentScroll">
<?php
while ($row=mysql_fetch_array($query, MYSQL_NUM)) {
$class = ($row[1]==1) ? 'Yes' : 'No';
$checked = ($class) ? 'checked="checked"' : '';
$data = $row[0];
$admin = (isset($_SESSION['admin']) && $_SESSION['admin']==1) ? 1 : 0;
echo <<< HTML
<div class="boxAddCategory{$class}">
<div class="boxAddCategory1">
{$data}
</div>
HTML;
if ($admin) {
echo <<< HTML
<div class="boxAddCategory2">
<input type="checkbox" value="categories[]" {$checked} />
</div>
HTML;
}
echo <<< HTML
</div>
HTML;
}
?>
</div>

Categories