PHP syntax breaking the image tag - php

I am having an issue with my live site that works fine on a dev site (on a different host).
Essentially, it's brining in the image incorrectly and adding characters at the end... example:
https://www.idealhomeloans.com/wp-content/uploads/2016/08/refinance-1.jpg/%3E%20%3Cimg%20src=
it should just be: https://www.idealhomeloans.com/wp-content/uploads/2016/08/refinance-1.jpg
This is the php code I'm using to bring the image in...
<?php if( $image ): ?>
<?php
echo '<img class="'. $imagealigned.'" src="';the_sub_field('image');
echo '/>';
?>
<?php endif; ?>
<?php $image = get_sub_field('image');
//Checking if anything exists for the image field
if ($image) { ?>
<?php echo '<img src="';// display a sub field value
the_sub_field('image');
echo '/>';
?>
<?php } //if there is nothing for image then display
else { ?>
<?php }
?>
Can someone who knows PHP well take a look and see if there's something that would render the image tag to come in correctly?
Any guidance would be greatly appreciated!
Thanks

Try to put values into variables.
<?php if ($image): ?>
<img src="<?= $src; ?>" class="<?= $class; ?>" />
<?php endif; ?>
It is easier to keep the formats and tags of the html, and you could become less confused. Plus I think it is much cleaner.

Perhaps if you actually LOOKED at the html you're generating, you'd see that you're generating BAD html:
echo '<img class="'. $imagealigned.'" src="';the_sub_field('image');
^---start HTML attribute
echo '/>';
^----never end the attribute
So you're building
<img .... src="kittens.jpg>
and around and around the error merrygoround you go...

You are using a semicolon (;) instead of a point (.) to concatenate the_sub_field

Related

Basic Image display from Database in PHP

I am learning the most basic of PHP MYSQL and am trying to display and image on my page along with Name, etc.
In the database I made a path: images/Paul.jpg alongside his first name and last name
Everything display as expected except for image. I have tried many iterations of the code but it either errors or displays a placeholder with no image or even on inspection doesnt show a path to the image directory.
Any thoughts?
<?php echo $data['first_name']; ?>
<?php echo $data['last_name']; ?>
<?php echo $data['age']; ?>
<?php echo '<img src="['image']" />'; ?>
<?php echo $data['bio']; ?>
<?php
}
?>
<!-- </table> -->
<?php mysqli_close($db); // Close connection ?>
If you have the path in your database, then you should use the path in the src attribute of your img
<?php echo '<img src="' . $data['image'] . '" />'; ?>
You also need to make sure the directory with your image is at the same level as this file to display correctly or you'll get an incorrect path.

Which is the better way to insert HTML tags into PHP code?

I see two methods but I don't know which is better, should I insert a div tag like this :
echo "<div>";
or like this ?
?> <div> <?php
There's no right answer to this.
When displaying a lot of HTML, I normally use ?> <div> <?php. This is especially true for when looping over data from a database, I would use:
<?php while ($row == $query->fetchObject() ) : ?>
<div class="row-<?php echo $row->id ?>">
//Some more of the data
</div>
<? endwhile; ?>
However, displaying only a little, I normally just echo it.
<?php echo "Hello, my name is <strong>Joe Doe<strong>"; ?>
I would encourage you to play with it and find what you like best.
The text editor i am using has the color formatting, when you are echoing things, it only all comes out the same color, so if you are outside of PHP you get the benefit of the color co-ordination.

Why can't I insert an image into php?

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)

WP is_front_page and displaying images

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.

setting the img src via php?

Here is my problem.
If I do:
$imagePath = "images/spalt_images/body_images/525A.JPG";
?>
<img src="<?php $imagePath ?>" alt="front image" class="productImage"/>
<?php
Then my image does not show up.
However, if I do:
<img src="images/spalt_images/body_images/525A.JPG" alt="front image" class="productImage"/>
Then my image shows up just fine. Why would it not work with php?
Thanks
You need to echo it:
<?php echo $imagePath ?>
or use a short tag (not recommended, but that's another discussion):
<?=$imagePath ?>
You need to echo $imagepath otherwise it won't 'print' it out.
Change <?php $imagePath ?> in your code to <?=$imagePath?>, or add an echo if your server doesn't allow shorttags.

Categories