How to add else into PHP Code line? - php

I have this following code
<td>
<?php if($rec->telegram){ ?><img class="social" src="<?php echo base_url(); ?>social/telegram.jpg" /><?php } ?>
<?php if($rec->google_pluse){ ?><img class="social" src="<?php echo base_url(); ?>social/g.jpg" /><?php } ?>
<?php if($rec->instagram){ ?><img class="social" src="<?php echo base_url(); ?>social/insta.jpg" /><?php } ?>
<?php if($rec->facebook){ ?><img class="social" src="<?php echo base_url(); ?>social/f.jpg" /><?php } ?>
</td>
I would like to add "else" into this code that if ALL those icons facebook, instagram, google plus and facebook weren't available then it will show another image file like "nophoto.jpg"
Note: If all those icons (4 icons) weren't available then it will show nophoto.jpg
I highly appreciate if someone guide me how to add else into above code.
Regards

It'll be easier to just add an extra if:
if (!($rec->telegram || $rec->instagram || $rec->facebook || $rec->google_plus)) { //etc
But really you want to attach that kind of logic to the $rec object, so that it's neatly separated and it's easier to add new types of social media in the future. Hopefully your $rec object is indeed backed by a class and not just a stdClass from a database result or cast.
public function hasNoSocialIcon() {
return !($rec->telegram || $rec->instagram || $rec->facebook || $rec->google_plus);
}
And then use that in your template:
<?php if ($rec->hasNoSocialIcon()) { ?>
<img class="social" src="<?php echo base_url(); ?>social/nophoto.jpg" />
<?php } ?>

You should take the use of PHP's conditional operators like this:
<?php
$icons_available = (
!empty($rec->telegram) &&
!empty($rec->google_plus) &&
!empty($rec->instagram) &&
!empty($rec->facebook)
);
?>
<td>
<?php if(!$icons_available) { ?>
<a href="default-pic.jpg">
<img class="social" src="default-pic.jpg" />
</a>
<?php } else { ?>
<?php $url = echo base_url() . 'social/telegram.jpg'; ?>
<a href="<?php echo $url; ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php $url = echo base_url() . 'social/google_plus.jpg'; ?>
<a href="<?php echo $url ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php $url = echo base_url() . 'social/instagram.jpg'; ?>
<a href="<?php echo $url ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php $url = echo base_url() . 'social/facebook.jpg'; ?>
<a href="<?php echo $url ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php } ?>
</td>
Hope this helps!

Related

Wordpress PHP grid layout presentation

I want to change the layout of a grid of videos on a site. It's Wordpress.
Currently the PHP displays the grid in a 3/2 layout over two rows and then duplicates it depending on how many videos you choose to display.
I understand the code enough to know that we are setting 5, and splitting into 3/2. And i can set it to 6 and split into 3/3 or 4/2.....and so on
But I need to change the layout so we have a row of 3, then 3 but with the first two of the second row stacked on top of each other 1/3rd width and the third 2/3rd width. like below.
new layout to be acheived
Current PHP that does the layout: ( I understand that CSS might have to be changed but I can do that)
<article class="col-1-<?php if(($i%5==0)||(($i+1)%5==0)){ echo "2"; } else { echo "3"; } ?>" id="post-<?php echo $page->ID; ?>">
Full block:
<article class="col-1-<?php if(($i%5==0)||(($i+1)%5==0)){ echo "2"; } else { echo "3"; } ?>" id="post-<?php echo $page->ID; ?>">
<a href="<?php echo get_page_link($page->ID); ?>">
<?php if ( $detect->isMobile() ) {
if (has_post_thumbnail( $page->ID ) ){ ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo $title; ?>" width="160" height="90">
<?php } ?>
<?php } else { ?>
<?php if(get_field('looping_thumb_preview', $page->ID)){ ?>
<video playinline preload="auto" loop="yes">
<source src="<?php echo get_field('looping_thumb_preview', $page->ID); ?>" type="video/mp4">
Your browser does not support the video tag.
</video>
<?php } else if(get_field('looping_thumb', $page->ID)){ ?>
<video playinline preload="auto" loop="yes">
<source src="<?php echo get_field('looping_thumb', $page->ID); ?>" type="video/mp4">Your browser does not support the video tag.</video>
<?php } else if (has_post_thumbnail( $page->ID ) ){ ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo $title; ?>">
<?php } else { ?>
<img src="<?php bloginfo( 'template_url' ); ?>/images/blank.png" alt="<?php echo $title; ?>">
<?php } ?>
<?php } ?>
<div class="center-content">
<h1<?php if(get_field('subtitle', $page->ID)){ echo ' class="hassubtitle"';} else { echo ' class="nosubtitle"'; }?>><?php echo $title;?></h1>
<div class="underline"></div>
<?php if(get_field('subtitle', $page->ID)) { echo "<h2>" . get_field('subtitle', $page->ID) . "</h2>"; }
?>
</div>
</a>
</article>
Thanks in advance for any help

How to add if else into PHP code?

I have 4 social icons and if all of them weren't available then I want to show default.jpg , currently in the column, it's showing 5 icons including default.jpg too.
I added $icons_available, but not working, here is my complete code:
<?php
$icons_available = (
!empty($rec->telegram) &&
!empty($rec->google_plus) &&
!empty($rec->instagram) &&
!empty($rec->facebook)
);
?>
<td>
<?php if(!$icons_available) {?><img class="social" src="<?php echo base_url(); ?>social/default.jpg" /><?php }?>
<?php if($rec->telegram){ ?><img class="social" src="<?php echo base_url(); ?>social/telegram.jpg" /><?php } ?>
<?php if($rec->google_pluse){ ?><img class="social" src="<?php echo base_url(); ?>social/g.jpg" /><?php } ?>
<?php if($rec->instagram){ ?><img class="social" src="<?php echo base_url(); ?>social/insta.jpg" /><?php } ?>
<?php if($rec->facebook){ ?><img class="social" src="<?php echo base_url(); ?>social/f.jpg" /><?php } ?>
</td>
Here is my question:
**
If all of 4 icons weren't available together, then it will show
default.jpg otherwise it won't show default.jpg, How to make it?
**
try below code: (replace you above mention code and try with this)
<td>
<?php
if((!empty($rec->telegram) && !empty($rec->google_plus) && !empty($rec->instagram) && !empty($rec->facebook))) { ?>
<img class="social" src="<?php echo base_url()."social/telegram.jpg"; ?>" />
<img class="social" src="<?php echo base_url()."social/g.jpg"; ?>" />
<img class="social" src="<?php echo base_url()."social/insta.jpg"; ?>" />
<img class="social" src="<?php echo base_url()."social/f.jpg"; ?>" /><?php
}else{
$defaultSrc = base_url()."social/default.jpg";
for($i=0; $i < 4;$i++){
echo '<img class="social" src="'.$defaultSrc.'"/>';
}
}
?>
</td>
try this
<? php
$icons_available = (!empty($rec - > telegram) &&
!empty($rec - > google_plus) &&
!empty($rec - > instagram) &&
!empty($rec - > facebook)
); ?>
< td >
<? php
if (!$icons_available) { ?> < img class = "social"
src = "<?php echo base_url(); ?>social/default.jpg" / >
<? php
} else if ($rec - > telegram) { ?> < a href = "<?php echo $rec->telegram; ?>" > < img class = "social"
src = "<?php echo base_url(); ?>social/telegram.jpg" / > < /a>
<?php }else if($rec->google_pluse){ ?><img class="social" src="<?php echo base_url(); ?>social/g.jpg " />
<?php }else if($rec->instagram){ ?><a href=" <? php echo $rec - > instagram; ?> "><img class="
social " src=" <? php echo base_url(); ?> social / insta.jpg " /></a>
<?php }else if($rec->facebook){ ?><a href=" <? php echo $rec - > facebook; ?> "><img class="
social " src=" <? php echo base_url(); ?> social / f.jpg " /><?php } ?></a>
</td>
Rather use a PHP switch http://php.net/manual/en/control-structures.switch.php You will then put default.jpg in the default section of the switch.

PHP: Foreach with "if-else" statement

I'm doing some changes on an online store that is running OpenCart 2.2.
When browsing below the categories on the left side, there are 2 carousels that show different promotions. I want to modify it that before the second carousel there is a description text. To do that I should edit the template file. And here is where I get lost..
This is the code in the template file:
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
From I see in the browser inspector and in my case, this code generates 2 banners with the ids - "banner0" and "banner1". The description text should go at the top of the code (right before ). If I use a simple paragraph, it gets displayed twice - above each banner.. How should I change it so that it will display the paragraph only above the second banner (id - banner1)?
I was thinking about an if-else statement, but I'm not sure if that will work... Could anyone help out a bit? My knowledge in PHP isn't much... :S
Thanks in advance!
Best regards,
Tsvetko Krastev
Then you need to know that this is the second time round the foreach loop. So change the foreach to include the index like this, and add a test inside the loop for $i == 1
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php //foreach ($banners as $banner) {
foreach ($banners as $i => $banner) {
?>
<div class="item">
<?php if ($banner['link']) {
if ( $i == 1 ) {
echo 'YOUR HTML CONTAINING SOME TEXT HERE';
}
?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
If I get the code right, $module is 0/1 (if id's get values banner0 and banner1), then this should work:
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<?php if ($module == "1") { ?>
<p>Your description</p>
<?php } ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
Thank you so very much guys!!! Tried both versions and they throw a error, but looking more carefully into the code and both your solutions, I came up with this:
<div id="desc<?php echo $module; ?>">
<?php if ($module == 1) { ?>
<p>Description</p>
<?php } ?>
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
</div>
Thank you very much again for the quick responses and the help! :) :3
Best regards,
Tsvetko Krastev

Show specific logo for user group in Joomla 3

I can't seem to see a logical way to do this, I've tried many different functions, some of which i get blank page errors, other which it seems to work but just skips and goes to the else function everytime.
In a nutshell, I'm trying to have it so that certain logos will be displayed depending on the User Group ID.
At the moment the code below is producing a blank error page, and I can't see why.
Could anyone help me with this? Joomla 3.1 by the way.
<?php $user = JFactory::getUser();
$usergroup=$user->getAuthorisedGroups();
if ($usergroup == '10') : ?>
<a href="<?php echo JURI::root(); ?>" id="gkLogo">
<img src="/images/fordlogo.png" alt="<?php echo $this->API->getPageName(); ?>" />
</a>
<?php elseif ($usergroup == '7') : ?>
<a href="<?php echo JURI::root(); ?>" id="gkLogo">
<img src="/images/tescologo.png" alt="<?php echo $this->API->getPageName(); ?>" />
</a>
<?php else; ?>
<a href="<?php echo JURI::root(); ?>" id="gkLogo">
<img src="<?php echo $logo_image; ?>" alt="<?php echo $this->API->getPageName(); ?>" />
</a>
<?php endif; ?>
Try something like this,
$user = JFactory::getUser();
$usergroup = $user->getAuthorisedGroups();
if(in_array('10',$usergroup)){
echo '<a href="'.JURI::root().'" id="gkLogo">
<img src="/images/fordlogo.png" alt="'.$this->API->getPageName().'" />
</a>';
}elseif(in_array('7',$usergroup)){
echo '<a href="'.JURI::root().'" id="gkLogo">
<img src="/images/fordlogo.png" alt="'.$this->API->getPageName().'" />
</a>';
}else{
echo '<a href="'.JURI::root().'" id="gkLogo">
<img src="/images/fordlogo.png" alt="'.$this->API->getPageName().'" />
</a>';
}
Hope this will help you.

Output image src with SimpleXML

I'm trying to output an image with SimpleXML, but the image tag doesn't appear in the source code.
Can anyone help me outpout this image:
Here's my XML and code:
<?php foreach($xml->Event as $event) { ?>
<li>
<a href="<?php echo $event->link; ?>">
<?php if ($event->Media['url'] == !null) { ?>
<img src="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
<h3><?php echo $event->title; ?></h3>
<p><strong><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></strong></p>
<p><?php echo $event->location; ?></p>
</a>
</li>
<?php } ?>
Your issue is here:
<?php if ($event->Media['url'] == !null) { ?>
<img src="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
You're trying to access url as though it were an attribute, you need to access it as a child element by using ->url instead.
<?php if ($event->Media->url != null) { ?>
<img src="<?php echo $event->Media->url;?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
EDIT: By the way, == !null works as you expect, but != null is a bit friendlier and less confusing
Your if statement is incorrect. It should be:
if ($event->Media['url'] != null)

Categories