Main Question:
Does an echo output differently inside a switch than on its own?
--
I don't think this is anything too involved, but it's certainly causing a little bit of headache for me!
To start with something simple, I'm able to show the Joomla full article image like this:
<img src="<?php echo htmlspecialchars($images->image_fulltext); ?>" />
I then wanted to expand this, and add a lightbox to the image, which turned into this:
<a href="<?php echo htmlspecialchars($images->image_fulltext); ?>" data-lightbox="image">
<img src="<?php echo htmlspecialchars($images->image_fulltext); ?>" />
</a>
The lightbox works fine.
However, then I only wanted this lightbox to apply to certain menu IDs (I have a News section in multiple languages, and so want to only apply the hyperlinks when certain pageIDs were being viewed).
I decided to make a switch (simple enough!)
<?php
$app = JFactory::getApplication();
$menuID = $app->getMenu()->getActive()->id;
switch ($menuID) {
case '168':
case '231':
echo
"<a href='".htmlspecialchars($images->image_fulltext)."' data-lightbox='image'>
<img src='".htmlspecialchars($images->image_fulltext)."' />
</a>";
break;
default:
echo
"<img src='".htmlspecialchars($images->image_fulltext)."' />";
break;
}
?>
The problem is that the switch is changing the output of src.
Outside of switch: /armouredshielding/images/news-events/website-screenshot.jpg
Inside switch: images/news-events/website-screenshot.jpg
surely this isn't the way switches work is it? It's still using an echo command, so the output should be exactly the same?
Any help is greatly appreciated!
I prefer something more like this:
$pagesToLight = array (168,231);
if(in_array($menuID, $pagesToLight)){
echo "<a href='".htmlspecialchars($images->image_fulltext)."' data-lightbox='image'>
<img src='".htmlspecialchars($images->image_fulltext)."' />
</a>";
} else {
echo "<img src='".htmlspecialchars($images->image_fulltext)."' />";
}
Related
trying to insert an image into some php. This code pulls from the database thumbnail page to show an item's details. It works fine when it's text "see it in 3d view" but when I try to insert a premade image in that location instead (a button jpg, aka "img src="#"), I'm getting an error. How can I do this correctly? Still learning the ins and outs of php and html, they don't always play the way I expect them to. Thanks for any help.
echo ("<br><img src= \"");
echo ($thumbnail);
echo (" \"><br><br><a href = \"");
echo ($photo);
echo ("\"><b>See it in 360 view</b></a></div>");
echo ("<div id=\"info\"; style=\"width:45%\"><br><br><div class = \"date\">");
echo ($date);
echo ("</div><br>");
echo ("<div class = \"blurbs\">");
echo ($sub);
echo ("<br><br><br>");
echo ($desc);
echo ("<br><br>");
echo ($hist);
echo ("<br><br><br><b>Provenance:</b><br>");
echo ($prov);
echo ("<br><br><b>Construction Label:</b><br>");
echo ($labl);
echo ("<br><br><br><br><b>");
echo ($cNum);
echo ("</b>");
<img src="#"> would never work. src="#" is a shortcut for "current page". e.g. browsers will try to use the current page's URL as the source for the image, which means it'll be trying to load a bunch of HTML as if it was a jpg/gif/png image. Since html isn't any of those, it'll just be a flat-out "this image contains errors" error.
Whatever you're putting in $thumbnail needs to be a proper url, e.g.
<img src="kittens.jpg">
<img src="http://example.com/kittens.jpg">
<img src="data:image/jpeg;base64,<?php echp base64_encode(file_get_contents('kittens.jpg')); ?>">
I would start out with cleaning up your file and remove some of the unneeded overhead (I personally love to have my controllers (Which is generating the output for my view files)
What is the output of this PHP file and what did you expect it to be?
<br><img src="<?= $thumbnail ?>">
<br><br><b>See it in 360 view</b>
</div>
<div id="info" style="width:45%"><br><br><div class = "date">
<?= $date ?>
</div><br>
<div class="blurbs">
<?= $sub ?>
<br><br><br>
<?= $desc ?>
<br><br>
<?= $hist ?>
<br><br><br><b>Provenance:</b><br>
<?= $prov ?>
<br><br><b>Construction Label:</b><br>
<?= $labl ?>
<br><br><br><br><b>
<?= $cNum ?>
</b>
a note to this is that Short Open tag which is enabled by default from PHP 5.4)
You should also look into using div or p tags instead of all the line breaks (it makes it easier for you to make changes to later on)
I'm working on a new WordPress theme (haven't touched WP themes since like 2.8...) and I'm attempting to make an if/else statement that changes the behavior of my top navigation bar based on whether the page is a front_page or not.
Essentially what I'm after is if the page is a front_page, then display the logo image. If the page is any other kind of page, then display a simple H1 element with the company name:
<li class="name">
<?php
if (is_front_page()) {
echo '<a href="<?php bloginfo("url"); ?>';
echo '<img src="' .bloginfo( "template_directory" ). '/images/logo-dsi.png" alt="DSI" />';
echo '</a>';
} else {
echo '<a href="' .home_url(). '">';
echo '<h1 class="logo">DSI</h1>';
echo '</a>';
}
?>
</li>
The else statement is working fine. But my if statement is not producing the img tag correctly. It echoes the literal bloginfo('template_directory') url and not inserting that into the img src, which is what I want it to do. It's trying to find an image at this url: localhost/images/logo-dsi.png
Obviously my syntax on line 5 is wrong somewhere, but I'm also not sure if I'm going about this the right way. Is there a better solution to what I'm trying to accomplish here?
Thanks!
The later is correct however
echo '<a href="<?php bloginfo("url"); ?>';
Should be
echo '<a href="' . bloginfo("url") . '">';
I tried this out and it worked:
<li class="name">
<a href="<?php bloginfo('url'); ?>">
<?php
if (is_front_page()) { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/logo-dsi.png" alt="DSI" />
<?php
} else { ?>
<h1 class="logo">DSI</h1>
<?php } ?>
</a>
</li>
Apologies, I should have hacked away at it a little longer. But maybe this will become helpful to someone else.
I knew echos in WordPress looked bad for a reason... ;-)
Also I'm sure there's a prettier way to write this out, but for now, that code does the trick.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
need php help
I need to add the if statement inside the html tag
if( get_field($image) ):
?><img src="<?php the_field($image); ?>" alt="" /><?php
endif;
I want to add the if statement inside the html tag below in place of img and a, is that possible?
echo
"<$html class=\"" .(!empty($this->options['class']) ? trim($thesis->api->esc($this->options['class'])) : ''). "\">
// I want this if statement to work inside here...how do i escape the html to make it work?
if( get_field($image) ):
?><img src="<?php the_field($image); ?>" alt="" /><?php
endif;
<img src=\"" .get_field($image)."\" alt=\"\" />
Download File
</$html>\n";
Putting PHP operators in HTML
First off, don't use the echo statement to spit out huge chunks of HTML, it makes code very hard to maintain and re-use. Isn't this easier?
<a href='<?php echo $some_variable; ?>'>
Using PHP logic in HTML blocks (general)
You're looking for something like this:
<?php if(!empty($image)): ?>
<img src='<?php echo $image; ?>' alt='Some Stuff'>
<?php endif; ?>
This is a short-hand equivelant called a ternary operator which may be easier to read in code:
<?php echo empty($image) ? null : "<img src='$image' alt='Some Stuff'>"; ?>
This will echo an image tag if $image has a value, and nothing if it doesn't.
Let's clean up & fix the code in the original post...
Your code looks like it has been deliberately obfuscated to confuse people. Learn to indent, don't embed logic within logic. Prioritize readability and your code will be a lot easier to maintain.
if(!empty($text))
echo
"<$html class=\"" .(!empty($this->options['class']) ? trim($thesis->api->esc($this->options['class'])) : ''). "\">
<img src=\"" .get_field($image)."\" alt=\"\" /> " .get_field($text)."
Download File
</$html>\n";
There is a lot that can be improved here. First of all, separate business logic out from display logic as much as possible:
Business logic
<?php
// This should be in another FILE ideally...
$this->divClass = empty($this->options['class']) ? null : trim($thesis->api->esc($this->options['class']));
$this->image = the_field($image);
$this->download = the_field($download);
$this->text = // I dont know how you're setting this.
?>
Display logic
Next, lose the get_field functions, add a null return to the_field if it's not found, that way you have cleaner code. Then, just use something like this:
<?php if(!isset($this->text)): ?>
<div class='<?php echo $divClass; ?>'>
<?php if(!isset($this->image) && !isset($this->download)): ?>
<img src='<?php echo $this->image; ?>'>
<a href='<?php echo $this->download; ?>'>Download File</a>
<?php endif; ?>
</div>
<?php endif; ?>
The <?php> tags are there to help you, they allow you to cleanly interpolate PHP code with HTML code in a way that most languages have to resort to ugly external tempating for. Use them, keep your code readable and understandable, don't take shortcuts because they will come back to bite you.
I am using the jQuery Hovercard plugin (here) and trying to impliment it with user images pulled from Facebook with their php sdk, here is the code I am using to display the Hovercard:
<?php
$friends = $this->facebook->api('/me/friends');
foreach ($friends["data"] as $value) { ?>
<label id="demo-facebook" data-hovercard="<?php echo $value['id'] ?>">
<img src="https://graph.facebook.com/<?php echo $value["id"] ?>/picture" alt="<?php echo $value["name"] ?>" />
</label>
<?php }
?>
The problem I am having is that the Hovercard will only show for the first users image displayed, after that no Hovercard will show when the image is moused over ?
You should change the CSS selector "demo-facebook" from an id to a class. I haven't used HoverCard before, but that seems like it would be the problem.
An ID specifies one specific element of a page, whereas a class can be re-used on multiple elements.
Also, I think it would be easier to just echo the HTML instead of having a <?php echo $value; ?> everytime you have to insert a PHP value. You could just do echo "<img src='$value'>"; instead of <img src="<?php echo $value; ?>" />. Just seems like it would be easier that way.
I have a PHP echo function inside of a HTML link, but it isn't working. I want to have an image location, defined in img src, be in part of the clickable link of the image. The page will have multiple images doing the same thing, so I am trying to use PHP to automate this.
<a href="http://statuspics.likeoverload.com/<?php echo $image; ?>">
<img src="<?php $image=troll/GrannyTroll.jpg?>" width="100" height="94" />
</a>
Turn
<?php $image=troll/GrannyTroll.jpg?>
into
<?php echo "troll/GrannyTroll.jpg"; ?>
?
Or provide more details on what you are trying to achieve.
Also, you might consider urlencode-ing some of those URL parameters.
Edit:
So you might try setting the variable beforehand:
<?php $image = "troll/GrannyTroll.jpg"; ?>
<img src="<?php echo $picture; ?>" width="100" height="94" />
So now i understand what you are trying to do.
One error is that you didn't enclose $image=troll/GrannyTroll.jpg with quotes like this:
$image = 'troll/GrannyTroll.jpg';
The second error is that you do it in the wrong order, you have to define $image first, before you use it.
That's what I believe you want to do:
<?php
$image = "troll/GrannyTroll.jpg";
?>
<img src="<?php echo $image; ?>" width="100" height="94"/>