Basic Image display from Database in PHP - 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.

Related

I'm trying to echo an image in a PHP variable in CodeIgniter

Hello I'm trying to echo an image on a view page in CodeIgniter but nothing is displayed on the page.
Here I'm making the variable:
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
And here I'm trying to echo the image:
<img src="<?php echo $image;?>">
Intro
This is the most basic php, so what's the addition of this question? Please read the basics of echo here: http://php.net/manual/en/function.echo.php (Example 1).
Learn the basics first!
Solution
1. Assign full html tag to variable and echo full html:
<?php
$image = '<img src="../../images/stoel.jpg" alt="Foo">';
echo $image;
?>
2. Or assign image path to variable and echo concat string:
<?php
$path = '../../images/stoel.jpg';
echo '<img src="' . $path . '" alt="Foo">';
?>
3. Or assign image path to variable and echo only this with php:
<?php
$path = '../../images/stoel.jpg';
?>
<img src="<?= $path; ?>" alt="Foo">
You are inserting full Image tag into src attribute.
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
and
<?php echo $image; ?>
or
<?php $image = "../../images/stoel.jpg"; ?>
and
<img src="<?php echo $image;?>">
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
you try like this
<?php echo $image; ?>
You are already assign full image code in variable so you just need to print your variable:
<img src="<?php echo $image;?>">
To
<?php echo $image;?>
Make sure your images out side of the application folder then you can do something like
application
images
images > stoel.jpg
system
index.php
Use Base url from the url helper
<img src="<?php echo base_url('images/stoel.jpg');?>" />
Make sure you have set the base_url in config.php
$config['base_url'] = 'http://localhost/yourproject/';
You can also use HTML Helper img();
I think this is what you mean. Since you are echoing inside the src attribute, you do not need to store the whole <image>, just the path will do.
<?php
$image = "../../images/stoel.jpg";
?>
<img src="<?php echo $image;?>">

PHP syntax breaking the image tag

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

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)

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
}
?>

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