Jump to content

Displaying images from result set


oha055

Recommended Posts

Hi!

 

I want the image of my site to be clickable so that it shows the next image contained in the result set of the sql query.

 

<?php
require_once('includes/connection.inc.php');
$conn = dbConnect();
$sql = 'SELECT filename FROM images ORDER BY image_id DESC LIMIT 1';
$msg = '';
if(!$sql) { echo "Something went wrong with the query. Please try again."; }
$result = $conn->query($sql) or die(mysqli_error());
if(mysqli_num_rows($result) == 0) {
header('Location: empty_blog.php');
$conn->close();
} else {
$row = $result->fetch_row();
$image = $row[0];
}
?>
<?php include('header.php'); ?>
<div class="content main">
<img id="image" src="images/<?php echo $image; ?>"/>
</div>
<?php include('includes/footer.inc.php'); ?>

 

All the filenames of the images are contained correctly in the $result variable, all I need to do now is to fetch the next image in the set. How would I go about this? Any help is greatly appreciated! :)

Link to comment
https://forums.phpfreaks.com/topic/248790-displaying-images-from-result-set/
Share on other sites

Try the following:

 

<?php

$imageid = 1;

if(isset($_GET['id'])
{
    $imageid = $_GET['id'];
}

require_once('includes/connection.inc.php');
$conn = dbConnect();
$sql = 'SELECT filename FROM images WHERE image_id = $imageid';
$msg = '';
if(!$sql) { echo "Something went wrong with the query. Please try again."; }
$result = $conn->query($sql) or die(mysqli_error());
if(mysqli_num_rows($result) == 0) {
header('Location: empty_blog.php');
$conn->close();
} else {
$row = $result->fetch_row();
$image = $row[0];
}
?>
<?php include('header.php'); ?>
<div class="content main">
<a href="thispage.php?id=<?php echo $imageid+1; ?>">
<img id="image" src="images/<?php echo $image; ?>"/>
</a>
</div>
<?php include('includes/footer.inc.php'); ?>

 

I haven't actually tested this so forgive me if I've missed something out. Hopefully you'll get the idea.

Thank you so much for your reply! :)

 

I tried out your suggestion, but now I am getting this error:

 

"Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\photoblog\index.php on line 13"

 

On this line:

 

$result = $conn->query($sql) or die(mysqli_error());

 

Don't know what to make of this, there was no bitching about this before  :P

Well, that worked! :D ....The page displays fine now. Still doesn't jump to the next image in the result set though.

 

When I click on the image, the address bar displays "http://localhost/photoblog/index.php?id=2", but the image doesn't actually change to image no. 2

 

Thank ou so much anyways :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.