Screenshot of the code:
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
require ('../../mysqli_oop_connect.php'); //Connect to the database.
$errors = array(); // Initialize an error array.
// Check for an isbn:
if (empty($_GET['isbn'])) {
$errors[] = 'You forgot to enter the ISBN-13.';
} else {
$isbn = $mysqli->real_escape_string(trim($_GET['isbn']));
}
if (empty($errors)) {
//Make the query:
$q = "SELECT * FROM books WHERE isbn=$isbn";
$r = $mysqli->query($q);
$num = $r->num_rows;
if ($num == 0) { // If it fails to run:
echo "<br /><br /><p>This book is currently not listed in our database. Would you like to add it?</p>\n";
//HERE IS WHERE IT NEEDS TO REDIRECT TO A PAGE THAT ALLOWS THE CONTENT TO BE ADDED TO THE DB LIKE REGISTER.PHP DOES.
} else { //If it suceeds then run the query:
echo '<h1>Is this your book?</h1>';
while ($row = $r->fetch_object()) { // POST DATA FROM DB.
echo '<img src="$row->image" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;
The rest of the code is just closing the DB connection.
I don't know what that icon means or where the issue is in the code. The DB is setup with three columns in a table: image, isbn, and name. The goal is to get the book to display the data being retrieved from the database. It is displaying all three items from the database based on the query, except the image is something else. The HTML is recognizing that there is an image being retrieved because when I just do $row->image it freaks out and posts random nonsense, as it should when there is an image being displayed like that. Any help is greatly appreciated.
echo '<img src="'.$row->image.'" height="100" width="100"><br />' . $row->name . '<br />' . $row->isbn ;
Try this way, and better you print your image path and check if it does exist or does not exist, and yes you can also inspect image element and check path of the image.
Its not clear in the question whether there is image link saved in the db or image data in base64. Answer is given assuming db has image link.
I'd guess from your comments about 'freaks out and posts random nonsense' that your image is stored in your database, rather than the path to your image. If so, you can't simply echo the image data in your <img> tag. The src attribute requires a URL, which you can create in one of two ways: create a script that serves the image with appropriate headers and use the URL of that script as your src attribute; or, create a data URI and include the data in your page. The latter is probably the simplest, but could increase the load time of your pages. Here's how:
// base64 encode image data and prepend the header
$imgURL = "data:image/png;base64,".base64_encode($row->image);
echo '<img src="'.$imgURL.'" height="100" width="100">' .
'<br />' . $row->name . '<br />' . $row->isbn ;
Note - you'll need to change the dataURL header to send the correct image type: image/jpg, image/png, etc.
You cannot have string substitution with single quotes. It will simply place the text $row->image in the echo:
echo '<img src="$row->image" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;
Try this instead:
echo "<img src='$row->image' height='100' width='100'>" . '<br />' . $row->name . '<br />' . $row->isbn ;
Or this:
echo '<img src="' . $row->image . '" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;
Personally, I prefer using sprintf like this:
echo sprintf('<img src="%s" height="100" width="100">', $row->image) . '<br />' . $row->name . '<br />' . $row->isbn ;
But on top of that, to make your debugging life even easier, I recommend formatting your PHP code to be easier to read like this:
echo sprintf('<img src="%s" height="100" width="100">', $row->image)
. '<br />'
. $row->name
. '<br />'
. $row->isbn
;
The cleaner your code is from the beginning, the easier it is to spot flaws & bugs when they arise.
Related
What I want to do is have images display on each blogposts, the images that will be displayed will be what the post was tagged with, for example I will tag 1 post with 2 tags, design and print, I would like 2 small images to appear on that posts page.
This is the code that I have managed to scrape together, the else statement is displaying so a post will have the same amount of default-author.jpg showing as there are tags.
This makes me think that something in the foreach is not working but I'm still trying to learn and this is puzzling me, any help would be much appreciated.
<div class="image_wrap">
<?php
$posttags = get_the_tags(); // Get articles tags
$home = get_bloginfo('url'); // Get homepage URL
// Check if tag-slug.png exists and display it.
if ($posttags) {
foreach($posttags as $tag) {
$upload_dir = wp_upload_dir();
$image_relative_path = "/wp-content/uploads/2017/06/" . $tag->slug .".png";
$image_path = $upload_dir['basedir'] . $image_relative_path;
$image_url = $upload_dir['baseurl'] . $image_relative_path;
if (file_exists($image_path)) {
echo '<a href="' . $home . '/' . $tag->slug . '" /><img title="' . $tag->name . '" alt="' . $tag->name . '" src="' . $image_url . '" /></a>';
// If no image found, output something else.
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/img/default-author.jpg" title="image missing"/>
<?php }
}
}
?>
Does anyone have any ideas on how I can achieve the effect I want, images to be displayed on a blogpost [in wordpress] depending on what tag it has?
I know this has already been put out on here but I've looked and mine still won't work and I don't know why someone please give me a hand.
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50); . '"/>';
Remove the semicolon you have after resize
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50) . '"/>';
Try this
echo "<img src= '".Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50)."'/>";
Please change
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50); . '"/>';
this line to
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50) . '"/>';
Because you had put one extra semi column ; after the resize(50,50)
Please change your code and try. Definitely work .
I have been trying to echo images from my database using PHP. I have tried various solutions on the forum with no success. I am just getting a blank image as an output.
while($rows=mysqli_fetch_assoc($getquery))
{
$id=$rows['id'];
$LastName=$rows['LastName'];
$FirstName=$rows['FirstName'];
$Year=$rows['Year'];
$Press=$rows['Press'];
$Description=$rows['Description'];
$Title=$rows['Title'];
$image=$rows['image'];
echo '<div class = "paragraph">' . $LastName . '<br/>'. $FirstName . '<br/>' . $Year . '<br/>' . $Press . '<br/>' . $Title . '<br/>'. $Description . "<img src='image/".$row['image']."' />" . '</div>' ;
}
You're echoing the image as $row['image'], but the only prior references to the image are $rows['image'] (note the s) and $image. Update the echo statement to use either of these and not $row['image'].
Edit: This will not fully fix your issue. As noted in a comment to your question, you will need to do some finagling to actually display the image as demonstrated here.
while($rows=mysqli_fetch_assoc($getquery)){
$id=$rows['id'];
/*
assuming the `image` directory is relative to the root
and not the directory from which the script runs then
a leading slash might be the issue.
*/
echo "
<div id='{$id}' class='paragraph'>
{$rows['LastName']}<br/>
{$rows['FirstName']}<br/>
{$rows['Year']}<br/>
{$rows['Press']}<br/>
{$rows['Title']}<br/>
{$rows['Description']}
<!--<img src='/image/{$row['image']}' />-->
<img src='data:image/jpeg;base64, {$rows['image']}' alt='' title='' />
</div>";
}
If you store the mimetype of the image when you save it to the db then you would substitute the proper mime/content type in above ~ ie: image/png, image/gif etc so that might then become something like:
<img src='data:{$rows['mimetype']};base64, {$rows['image']}' alt='' title='' />
I'm using Wordpress and the PODS plugin and created the pod project. In project i've created a set of fields. One of those is project_slide. If this field has a value, I want to show this in a RoyalSlider on my frontpage.
I've started with this piece of code (= one slide).
<?php
$project = pods('project', array('limit' => -1));
while ($project->fetch()) {
echo '<div class="rsContent">';
echo '<img class="rsImg" src="' . $project->display('project_slide') . '" alt="' . $project->display('project_title') . '">';
echo '</div>';
}
?>
The problem is: each project I add, displays in the slider. Even if project_slide has no value. Any idea how to resolve this?
Just add an if-statement to the loop.
Hide Image example
In my example, I only add the slide when the value isn't [empty string]
while ($project->fetch()) {
if( $project->display('project_slide') !== ""){
echo '<div class="rsContent">';
echo '<img class="rsImg" src="' . $project->display('project_slide') . '" alt="' .$project->display('project_title') . '">';
echo '</div>';
}
}
Expand with a detection if the image actually exists
You could expand it with a file_exists to also check if it exists (for the items with value, but the image itself doesnt exists). Just make sure the file_exists is 2nd, that way it only checks if the file is present if the string is not empty (if-statements go from left to right)
$project->display('project_slide') !== "" && file_exists($_SERVER['DOCUMENT_ROOT'].$project->display('project_slide')`
Default image method:
Instead of showing it only when there is an image, you could always show it, but build a default-image logic:
$defaultImage = '/some/image.jpg'; // set a default image outside of the loop
while ($project->fetch()) {
$src= $project->display('project_slide') !== "" ? $project->display('project_slide') : $defaultImage; // set a default umage
echo '<div class="rsContent">';
echo '<img class="rsImg" src="' . $src . '" alt="' .$project->display('project_title') . '">';
echo '</div>';
}
I have selected a row from the database, within the row is a field called image. This is a blob datatype which contains an image within the database but when i try to display the image via php it displays the text version of the image not the actual image.
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
$player = $this->player;
$name = $player->forename . " " . $player->surname;
$handed = $player->handed;
$image = $player->image;
?>
<div class="thumbnail"><?php echo '<img src="' . $image . '" alt="' . $name . '" />'?></div>
It is probably an easy fix and i did look at trying:
$mime = "image/jpeg";
$b64Src = "data:".$mime.";base64," . base64_encode($image["img"]);
echo '<img src="'.$b64Src.'" alt="" />';
?>
but that only outputed:
<img src="data:image/jpeg;base64,/w==" alt="">
Any help is much appreciated, thankyou in advance