I would like to echo a div - php

In $ruta I have the value "imagenes/1.jpg"
If I do that, I display the image
<img src="imagenperfil/<?php echo $ruta; ?>" width="250" height="300">
but now I have this:
echo "<div class='tweet_user'><img class='user_img' src=''></div>";
So in src I would like to put
"imagenperfil/<?php echo $ruta; ?>"
I try lots of ways and none works.
thanks a lot! you always solve my all my problems

Try,
echo "<div class='tweet_user'><img class='user_img' src='imagenperfil/$ruta'/></div>" ;
Demo

$ruta = htmlspecialchars($ruta);
echo "<div class='tweet_user'><img class='user_img' src='imagenperfil/{$ruta}'></div>";
Added htmlspecialchars()

If you tried to do this:
echo "<div class='tweet_user'><img class='user_img' src='"imagenperfil/<?php echo
$ruta; ?>"'></div>";
then it won't work. You are already in php and using echo
You can try this:
echo "<div class='tweet_user'><img class='user_img' src='imagenperfil/".$ruta."'></div>";

Related

Trying to use in line styling in the middle of a PHP echo

Is there anyway I can get in-line styling within a PHP echo statement. I want to set the height and the width of an image, as when I try to apply the height and `width externally it makes no difference as the image loads before the style sets in.
I have tried this but doesn't seem to be making any difference what so ever...
<p><b>Profile Picture: </b>
<?php
$picture = $row['imagePath'];
if (empty($picture)){
echo "<img src='profiles/no-image.png' 'width=500' 'height=600' >";
} else {
echo "<img src='".$row['imagePath']."' 'width=500' 'height=600' >";
};
?></p>
That doesn't work because you are not setting the quotes right.
This should do the trick:
echo "<img src='profiles/no-image.png' width='500' height='600' >";
You can apply styles the same way:
echo "<img src='profiles/no-image.png' style='width:500px;height:600px;'>";
Here is an alternative. Use PHP like a template engine. This approach does not use echo statements to output HTML. Instead, dynamic elements are introduced as needed.
<p><b>Profile Picture: </b>
<?php
$picture = $row['imagePath'];
if (empty($picture)) :
?>
<img src="profiles/no-image.png" width="500" height="600" >
<?php else : ?>
<img src="<?= $row['imagePath'] ?>" width="500" height="600" >
<?php endif ?>
</p>

Link is not working inside of Div but works outside

Thanks for taking the time. I trying to make some image links inside of a DIV in html. The links are stored in a SQL DB. The links work just fine when outside of this specific div, but I really want to put them inside for styling purposes. The social images div is the one
echo "<div class=column-3>";
echo "<h3 id=\"artistNameLabel\"><strong>$artist_name</strong></h3>";
echo "<img id=\"artistImage\"src=\"imageuploads/$imageFilePath\">";
echo "<p><img alt=\"Artist Instagram\" src=\"images/instagramLogo.png\" width=\"25%\"></p>";//link doesnt work
echo "<p><img alt=\"Artist Streaming Account\" src=\"images/spotifyLogo.png\" width=\"25%\"></p>";//link doesnt work
echo "</div>";
The images appear just fine, but the link doesn't work. Also the text value that is the link is loaded just fine. Thanks for any help
echo "<div class=column-3>";
echo "<h3 id='artistNameLabel'><strong>".$artist_name."</strong></h3>";
echo "<img id='artistImage' src='imageuploads/".$imageFilePath."'>";
echo "<p><a href='".$artistInstagram."' target='_blank'><img alt='Artist Instagram' src='images/instagramLogo.png' width='25%'></a></p>";//link doesnt work
echo "<p><a href='".$artistStreaming."' target='_blank'><img alt='Artist Streaming Account' src='images/spotifyLogo.png' width='25%'></a></p>";//link doesnt work
echo "</div>";
You can try like this way :
$artistInstagram = "https://www.instagram.com/";
$artistStreaming = "https://www.example.com/";
$artist_name = "Test";
$imageFilePath = "https://via.placeholder.com/150";
$html = <<< EOT
<div class=column-3>
<h3 id="artistNameLabel"><strong>$artist_name</strong></h3>
<img id="artistImage"src=$imageFilePath>
<p>
<a href=$artistInstagram target="_blank">
<img alt="Artist Instagram" src="https://via.placeholder.com/150" width="5%">
</a>
</p>
<p>
<a href=$artistStreaming target="_blank">
<img alt="Artist Streaming Account" src="https://via.placeholder.com/150" width="5%">
</a>
</p>
</div>
EOT;
echo $html;
I think this will solve your problem
You need to style you anchor tag with "display:inline-block" and it will work fine. no matter if image is there or not.

Show image, if it's set in database

I'm trying to show an image (or rather a link to an image) stored in a database and I'd like to get the image to show only if the link is set in the database.
Currently, if the link is not set (value is null), it shows a broken link.
Is there a way to for example use an if-statement and echo a HTML-code?
Something like this:
(The value have been fecthed to array $current in this example:)
<?php
if(isset($current['image']) {
echo "<img src='<?php echo $current['image'];
?>' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'})">
You used <?php twice, you have problem with quotes, brackets, etc.
<?php
if (!empty($current['image'])) {
echo "<img src='" . $current['image'] . "' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'>";
} else {
// here you can write for example default no-image image or whatever yo want, if you want
}
Nevermind, got it.
-Solution:
<?php if(isset($current['image'])): ?><img src="<?php echo $current['image']; ?>" class="left" style="max-height:20em; max-width:15em; margin-right:1em; margin-top:0;})">
<?php endif; ?>
<?php
if(isset($current['image'])) {
?>
<img src='<?php echo $current['image'];?>' class='left' style='max-height:20em; max-width:15em;
margin-right:1em; margin-top:0;'>
<?php
}
?>

Php's echo does not close properly

Hello I have this src path in codeigniter
src="<?php echo base_url();?>photos/<?php echo $data1; echo '"/>';?>
The problem is that when i'm going to continue my code it seems that all the below code is inside an echo ""; it looks like the echo is still open.. For example i want to continue my code with a html divand thediv` is still on an echo area.. Thanks
I think you are inside of quotes of src-property.
src="<?php echo base_url();?>photos/<?php echo $data1;?>" />
try this
src="<?php echo base_url();?>photos/<?php echo $data1;?>" />
try this
<?php echo 'src="'.base_url().'photos/'.$data.'" />'; ?>
or
<?php echo '<img src="'.base_url().'photos/'.$data.'" />'; ?>
it seems like you haven't closed the last echo . tyr this
src="<?php echo base_url();?>photos/<?php echo $data1;?>" />
there is more better way of doing this in codeigniter. pass the url as the arguement to the base_url function
src="<?php echo base_url('photos/'.$data1);?>" />
try it. for more details: click

Displaying an image in PHP using name stored in database

I am trying to display an image that is in a folder "upload" by getting the image name from the database.
while($row = mysqli_fetch_array($result))
{
$pic = $row['image'];
echo $row['item'] ;
echo $row['location'];
echo $row['description'];
echo $row['forum'];
echo $row['datetime'];
echo $row['username'];
?>
</br>
<img src="upload/<?php echo $pic ?>"/>
<?php echo $row['image']; } ?>
"upload/<?php echo $pic ?>"
</body>
</html>
As you can see it display everything except the img src.
This is my database (ignore BLOB that was a test). I can't seem to figure out where I'm going wrong.
Thanks
My recommendation is to store the full relative path in the database like this:
uploads/folder/file.jpg
My preferred MySQL field type is 'varchar(255)' the your echo in te PHP code will look like:
echo '<img src="'. $row['image'].'" />';
You are trying to print the image source outside the while-loop. The while-loop will only exit when $row is empty, so $row['image'] is also empty.
1) view source (or use firebug) to see the image tag and see what is the src given there.
2) try the url: "http://{LOCAL}/projects/projectviewposted.php/upload/happyball(1).jpg" and see if you can open the image
Try this
<img src="upload/<?php echo $pic; ?>"/>
<?php echo $row['image']; } ?>
"upload/<?php echo $pic ?>"
Else note down the link of the folder path. It might be that the folder path is not correct.
Hope this helps
You can try :
while($row = mysqli_fetch_array($result))
{
$pic = $row['image'];
echo $row['item'] ;
echo $row['location'];
echo $row['description'];
echo $row['forum'];
echo $row['datetime'];
echo $row['username'];
echo '<br />
<img src="upload/'.$pic.'"/>
'.$row['image'];
}
Well after 3 days I figured it out... my header location was incorrect!
../project/projectviewposted.php/ A stupid extra slash at the end!!
Thanks for your help and suggestions!

Categories