img src won't work with php variables - php

<a href="'.$productLink.'" alt="'.$productName.'">
<img src="'.$productImg1URL.'" alt="'.$productName.' '.$productType.' ">
</a>
Hello the img src is actually in a different directory /images
I know this is probably super easy but Ive spent an hour on it and nothing. The page works but doesn't show the directory. Please help the rookie. I took out the < in front of the a href and img src couldn't make the page display

There are two ways of doing this:
Firstly, outside of a code block:
<?php
// code here
?>
<a href="<?= $productLink ?>" alt="<?= $productName ?>">
<!-- or -->
<img src="<?echo $productImgURL; ?> alt="<?php echo $productName . ' ' . $productType ?>">
</a>
The first form is called the short open tag and has to be enabled in your php.ini which it almost always is.
Or if you're doing this inside a code block:
<?php
echo <<<END
<a href="$productLink" alt="$productName">
<img src="$productImgLURL" alt="$productName $productType">
</a>
END
The <<<END is a heredoc and I usually prefer it to using a big string within double quotes, which requires escaping contained double quotes.

Job #1 when you have a problem with code that is generating HTML, is look at the output source and compare it with what you expect. I've had problems like this before and they usually vanished when I stopped thinking about the PHP code, and looked at the actual output.
What is the content of $productImg1URL - and if the images that it's referencing are in the URL starting /images/ - does that start $productImg1URL. If it's just the name of an image, but without the path - you have to put that into place.

Related

Trying to echo an image in Php, cant find src

i just started learning Php, right now im trying to convert a HTML page to a Wordpress Theme. I have trouble trying to display images...
In my HTML page i have imgs like:
<img class="mainlogoclean" src="images/akbarlogoclean.png"/>
When i tried to change it with php my page just stop working:
<img class="mainlogoclean" src="<?php echo /images/akbarlogoclean.png ?>"/>
You will have to echo a string for it to work properly.
i.e. have /images/akbarlogoclean.png inside single/double quotes.
You can learn more about how echo works Here
The code could be written in following ways
<img class="mainlogoclean" src="<?php echo "/images/akbarlogoclean.png" ?>"/>
Or you could also try the shorthand version of using echo
<img class="mainlogoclean" src="<?="/images/akbarlogoclean.png" ?>"/>
Both of these give the following output
<img class="mainlogoclean" src="/images/akbarlogoclean.png"/>

How to post figures on a new line in php?

I want to control when to put figures on a new line in php. I can keep posting figures like so:
<A HREF="./images_local/figname.gif">
<img height=196 width=128 src="./images_local/figname.gif"/></A>
<A HREF="./images_local/figname2.gif">
<img height=196 width=128 src="./images_local/figname2.gif"/></A>
<A HREF="./images_local/figname3.gif">
<img height=196 width=128 src="./images_local/figname3.gif"/></A>
But they all post left to right, until they run out of space, at which point they begin a new line. How do I control when to start posting those figures on a new line?
Thanks.
It is a simple HTML issue and has nothing to do with php, but even if you code HTML in php you could just use the <br> tag for line break.
By the way, it is considered a good practice to keep all HTML tags (<a>) and attributes (href="") in lower case. It just makes your code more readable and consistent with proper HTML markup recommendations. :)
as mentioned above, the <br> works just fine, alternatively you can list your images with :
<ul><li><A HREF="./images_local/figname.gif"></li>
<li><img height=196 width=128 src="./images_local/figname.gif"/></A>
<A HREF="./images_local/figname2.gif">
<img height=196 width=128 src="./images_local/figname2.gif"/></A>
<A HREF="./images_local/figname3.gif">
<img height=196 width=128 src="./images_local/figname3.gif"/></A></li>
</ul>
etc

Echo a txt file in data-caption

I am trying to echo in php the image description in the data-caption of the clearing image gallery of Foundation3 but it is not working. Here is my code:
<li class="clearing-feature">
<a href="administration/galleri/kcfinder/upload/images/Galleri1/1.jpg">
<img data-caption="<?php echo $myDataBild1 ?>;" src="kcfinder/upload/images/Galleri1/1.jpg"></a>
</li>
This is not working. The image gallery is still working correctly but the caption in this picture is not showing. How can I correct this?
NOTE: The php $myDataBild1 is a txt file with text inserted via CKEditor and it is working just fine. Let me know if you need the code and the variable is shown if I echo it anywhere else.
Misplaced comma - also try by using addslashes.
<img data-caption="<?php echo addslashes($myDataBild1); ?>" src="kcfinder/upload/images/Galleri1/1.jpg"></a>

How can I add a image click function to gallery.php temp code?

Ok so here is the link to the template code for gallery.
https://github.com/JoomShaper/Helix-Joomla/blob/master/helixPlugin/shortcodes/gallery.php
However when I insert images on my database (not in Php), the image shows up but I am unable to click that image and be directed to the images page, which I have hyperlinked on my database (Joomla). I will have multiple images where I will need to add this click function to.
I have been trying to figure this out forever. I am new to Php and coding, so forgive my ignorant use of word choices from the above paragraphs.
(I've noticed in the past when speaking to coders it is frustrating for them to code the jargon from a someone who doesn't know the language ha!) Thanks!
Sorry, I know this is not an answer probably, but I cannot comment yet. I have been looking at your code for a little while, and I noticed some things I wanted to ask you about. First:
<?php foreach ($galleryArray as $key=>$item) { ?>
<li style="width:<?php echo round(100/$columns); ?>%" class="<?php echo str_replace(',', ' ', $item['tag']) ?>">
<a class="img-polaroid" data-toggle="modal" href="<?php echo ($modal=='yes')? '#modal-' . $key . '':'#' ?>">
<?php
echo '<img alt=" " src="' . $item['src'] . '" />';
?>
<?php if($item['content'] !='') { ?>
<div>
<div>
<?php echo do_shortcode($item['content']); ?>
</div>
</div>
<?php } ?>
</a>
</li>
You begin your a and li inside the foreach loop, but you finish them outside of it. Considering that the a is your link, and your link is not working, I thought this could be part of your issue.
Also, I noticed that when you link to the modal, you use the same variables from the loop, but the variables were defined inside the loop and would not carry over on clicking the link that was generated. It seems to me that you would need to define a property value with the variable you need from each one, so that you can use JS to do something along the lines of "onClick" grab "this.property.value" so that you could have that information in the modal.
I only thought this was of note because the modal you are opening with the link is named by one of these variables. Unless you are creating a separate modal for each one inside the foreach loop, the name you are linking to does not exist. Sorry if this is confusing.

Anchor tag wont render correctly (WordPress PHP)

I am trying to add data atributes to my anchor tag for a WordPress custom theme.
The code below is what I've so far, the problem is with plain HTML this works fine but once I add the PHP lines then something breaks.
When the actual HTML is rendered it excludes the end of the open anchor tag, and leaves "> out to display on the page.
Not sure what went wrong but maybe someone can take a look at this and might be able to point out what I did wrong, a fix, a better way, or maybe if this is even possible.
<a
class="caption" href="<?php the_permalink()?>"
title="<?php the_title_attribute(); ?>"
data-title="<?php the_title(); ?>"
data-description="<?php the_excerpt(); ?>"
>
<?php the_post_thumbnail(array(301,301)); ?>
</a>
<?php endif; endif; ?>
This is not an answer, just some thoughts/things to try:
Are those PHP functions defined somewhere on the same page, or on a page that is included or required ?
Have you tried replacing those function calls with simple PHP commands, such as <?php echo "the_permalink_goes_here"; ?> etc. -- just to make sure, for example, that the anchor tag's href value changes to
<a href="the_permalink_goes_here" etc>
The excerpt function that you're using for the description is returning not just the excerpt, but also an additional "Read more" link, effectively putting an anchor inside your anchor tag, which is what is breaking it. To the best of my knowledge, there isn't a default WP function for returning an excerpt without this link, so you will need a function to do this. Try searching for 'excerpt without link'

Categories