I would like to remove the else statement in the following code.
How shall I convert the if statement, so it still works (route is correct/ logo is being shown)?
<div class="logo a">
<a href="<?=$site_url;?>">
<?php if(isset($settings['site_logo_image']) && $settings['site_logo_image'] != '') { ?>
<img src="<?=$site_url;?>_img/logo.png" class="image_logo" border="0" height="40" alt="<?=(isset($settings['site_logo']) ? $settings['site_logo'] : 'Hello');?>" />
<?php } else { ?>
<?=(isset($settings['site_logo']) ? $settings['site_logo'] : 'Hello');?>
<?php } ?>
</a>
</div>
If I leave out the if prefix, it's not working.
I am not a pro in php and actually have to do with frontend stuff...
Thanks
It's a bit tricky to read php interspersed with HTML. Once you get the knack of it, though, it's straightforward. Delete everything from else { to the next }. Like this.
<div class="logo a">
<a href="<?=$site_url;?>">
<?php if(isset($settings['site_logo_image']) && $settings['site_logo_image'] != '') { ?>
<img src="<?=$site_url;?>_img/logo.png" class="image_logo" border="0" height="40" alt="<?=(isset($settings['site_logo']) ? $settings['site_logo'] : 'Hello');?>" />
<?php } ?>
</a>
</div>
Pro tip A language-aware IDE like VSCODE or PhpStorm will alert you immediately if you make a mistake doing this sort of thing.
Related
I've been editing the following block of code and now when I use it then the page will not load but I also don't get anything in the error log either. I am guessing this has happened due to a missing or misplaced character somewhere but have looked and looked over it and cannot spot what it is! does it stand out to anyone?
<?php if ($logourldecoded != "") { ?>
<?php if ($companyname != "") { ?>
<img src="images/site-logo-high-res.png" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php } else { ?>
<div class="companynameclass" style="font-size: <?php echo $data[font_size]; ?>;">
<?php echo $companyname; ?>
</div>
<?php } else { ?>
<img src="<?php echo $logourldecoded; ?>" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php } ?>
I also know it's not that any of the data I'm calling with $NAME_HERE being incorrect or missing as if i just echo them all indiviually they are exactly as expected and before my edits where showing.
Missing } to close the inner IF
<?php
if ($logourldecoded != "") {
?>
<?php
if ($companyname != "") {
?>
<img src="images/site-logo-high-res.png" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php
} else {
?>
<div class="companynameclass" style="font-size: <?php echo $data[font_size]; ?>;">
<?php echo $companyname; ?>
</div>
<?php
// the missing line
}
?>
<?php
} else {
?>
<img src="<?php echo $logourldecoded; ?>" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php
}
?>
You might consider not placing <?php ..?> tags on each line, it makes the code almost completely unreadable, and whats unreadable is by definition unmaintainable
I have an attribute on my product view and list pages that displays a preview video on a separate page. I'm pulling in the attribute using my themes product/view.phtml and product/list.phtml. It has an image button that shows preview now. The code is have is below.
<img src="/graphics/preview-now.png" align="absmiddle" style="margin-top: 10px;" alt="Preview Now">
The problem is that I don't want this image to display if the product attribute is null or blank. I tried the code below in a few variations but that wouldn't work.
< ?php if($_product->getvideo_src() != '') { ?>
<span><img src="/graphics/preview-now.png" align="absmiddle" style="margin-top: 10px;" alt="Preview Now"></span>
< ?php } ?>
Try using isset. Also your magic get formatting is off. You had getvideo_src() when it should be camel-cased like getVideoSrc().
<?php if(isset($_product->getVideoSrc())) { ?>
<span><img src="/graphics/preview-now.png" align="absmiddle" style="margin-top: 10px;" alt="Preview Now"></span>
<?php } ?>
Try
<?php if($_product->getVideoSrc()) : ?>
...
<?php endif; ?>
or
<?php if($_product->getData('video_src')) : ?>
...
<?php endif; ?>
this is my first post for help on here, and man do I really need it. This is the first time I've developed a client's site using multisite, and I'm having trouble applying the appropriate header image to it's site. There are six sites in all, and I'm using the same template for all six sites' front pages. Also, the front page is static and doesn't have a specific page selected.
The conditional below is my attempt at specifying specific images depending on which sub-site I'm on. It keeps throwing a syntax error, (sublime calls it a parse error). I would be so grateful for any help!
<?php
if( get_bloginfo('All in with Laila Ali')) {
<img src="<?php bloginfo('template_directory');?>/images/Banner-LailaAli.jpg" />
} elseif{
if( get_bloginfo('Jaimies 15 Minute Meals')) {
<img src="<?php bloginfo('template_directory');?>/images/Banner-JamieOliver.jpg" />
}
} elseif{
if( get_bloginfo('Lucky Dog')) {
<img src="<?php bloginfo('template_directory');?>/images/Banner-LuckyDog.jpg" />
}
} elseif{
if( get_bloginfo('Game Changers with Kevin Frazier')) {
<img src="<?php bloginfo('template_directory');?>/images/Banner-GameChangers.jpg" />
}
} elseif{
if( get_bloginfo('Recipe Rehab')) {
<img src="<?php bloginfo('template_directory');?>/images/Banner-RecipeRehab.jpg" />
}
} else {
<img src="<?php bloginfo('template_directory');?>/images/Banner-PetVet.jpg" />
}
?>
You are getting this error because you have consecutive <?php tags with HTML in between them. Things like <img src=" aren't valid php, but you are inside <?php tags, so PHP gives you an error.
One of the ways you can fix this is by ending the tags when you need to switch back to HTML. Like this:
<?php
if( get_bloginfo('All in with Laila Ali')) {
?>
<img src="<?php bloginfo('template_directory');?>/images/Banner-LailaAli.jpg" />
<?php
} elseif{
if( get_bloginfo('Jaimies 15 Minute Meals')) {
?>
<img src="<?php bloginfo('template_directory');?>/images/Banner-JamieOliver.jpg" />
<?php
}
}
?>
Another way, is to have your PHP echo the HTML you want. Which would look something like this:
<?php
if( get_bloginfo('All in with Laila Ali')) {
echo '<img src="' . bloginfo('template_directory') . '/images/Banner-LailaAli.jpg" />';
} elseif{
if( get_bloginfo('Jaimies 15 Minute Meals')) {
echo '<img src="'. bloginfo('template_directory') . '/images/Banner-JamieOliver.jpg" />';
}
}
?>
Basically, you have to remember that once you open a <?php tag, you can only put valid PHP until you close it with ?>.
I'm trying to build an if statement for when a variable is filled with a url it will display a button and link to the site, and when the variable is empty the link and button will not display. So far I got it to where it is displays the button and links but making it into an if statement keeps on breaking my site. If you see a problem with the code below, please help. Thanks
<div id="social_icon">
<?php if (isset($fburl))
{
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png"" width="30"
height="30"></a>;
}
else
{
//dont show anything
}
?>
</div>
You're trying to use HTML within your PHP code, so PHP sees this as an unexpected variable/string. Either use echo for this, or close the PHP statement, and then write your HTML.
Either:
<div id="social_icon">
<?php if(isset($fburl)){ ?>
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />
</a>
<?php }else{
//dont show anything
} ?>
</div>
Or:
<div id="social_icon">
<?php if (isset($fburl)){
echo '<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />';
}else{
//dont show anything
} ?>
</div>
Edit
Actually, I would assume it's not outputting anything because your if statement is checking for $fburl whereas you're echoing the link as $options['fburl']. If the facebook url is located at $options['fburl'], try:
<div id="social_icon">
<?php if(isset($options['fburl'])){ ?>
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />
</a>
<?php }else{
//dont show anything
} ?>
</div>
Edit 2
If the options are set but don't contain a link, you will also need check for that:
<div id="social_icon">
<?php if(isset($options['fburl']) && !empty($options['fburl'])){ ?>
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />
</a>
<?php }else{
//dont show anything
} ?>
</div>
Syntax error, change it to:
<?php if (isset($fburl))
{
//missed end tag here
?>
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png"" width="30"
height="30"></a>;
<?php
//add another php start tag
}
else
{
//dont show anything
}
?>
I am looking for some help using the PHP function. At the moment my website is structured like this:
index.php:
<?php
require_once( 'page_elements.php' );
?>
<body>
<?php echo content();?>
</body>
page_elements.php:
<?php
function content() {
?>
<div class='main'>
<img class='main' src="<?=$ImgName?>"> </img>
</div>
<?php
} ?>
if statement:
if (isset($_SESSION['basket_total']))
{
$basket_total = $_SESSION['basket_total'];
if($basket_total !== '0')
{
$ImgName = 'img/basket.php';
}
else
{
$ImgName = 'img/basket_empty.php';
}
}
What I want to do is to be able to define $ImgName in an if statement that isn't involved in the function content() but if i include it in another file, include 'if_statement.php' or in another function then it doesn't recognise the variable.
Sorry, its my first time structuring a website like this and I am finding it a bit confusing.
Cheers in advance
First of all, you don't close an an "img" tag with another "img" tag ...
function content(){
echo'
<div class="main">
<img class="main" src="'.$imgname.'" alt="" title="">
</div>
';
}
is the proper way of doing things. Now as to your question, I'm having trouble understanding your goal, but do you perhaps mean something a.la ...
function content(){
$imgname = include "file.php";
echo'
<div class="main">
<img class="main" src="'.$imgname.'" alt="" title="">
</div>
';
}
and the if_statement.php would be something like ...
if(isset($_SESSION['basket_total'])){
return $_SESSION['basket_total'];
}else{
return "img/basket.php";
}
This will get around the current issue you are having, but I would do like Ionut Flavius Pogacian suggested above and look into an MVC
<?php
require_once( 'page_elements.php' );
$image_name = "batman.jpg";
?>
<body>
<?php echo content($image_name);?>
</body>
page_elements.php:
<?php
function content($image_name) {
?>
<div class='main'>
<img class='main' src="<?=$image_name?>" />
</div>
<?php
} ?>