Reputation: 3375
So I'm started recently to learn PHP and PDO. While I read some tutorials I've create one simple site with 2 pages. index.php
and single.php
. On the index page I load images from database(this is done). Then when I click on single image is go to single.php
where this image is displayed(done this also). Now I'm trying to make buttons for next and previous image from database. I'm not so sure how is this working but after reading some tutorials I think I've got the idea.
This is the link on the index.php
which redirect to single.php
<a href="single.php?image_id='.$row['image_id'].'">
<img src="'.$row['image_name'].'"/>
</a>
Here is single.php
( part of it )
if(isset($_GET['image_id']) && is_numeric($_GET['image_id']))
{
$image_id = $_GET['image_id'];
$result = $pdo->prepare("SELECT * FROM images WHERE image_id= ? LIMIT 1");
if ($result->execute(array($_GET['image_id'])))
{
if($row = $result->fetch())
{
$update = $pdo->prepare("UPDATE images SET image_hits = image_hits + 1 WHERE image_id = ?");
$update->bindParam(1, $_GET['image_id'], PDO::PARAM_INT);
$update->execute();
echo ' // image data ';
}
}
}
If I'm understanding correctly how is work I need current id which should be image_id
, next id and prev id. It's sound kind of easy.. but not for me. I've thing that I need something like this but the problem is where and how to implement it into current code that I have?
$result = $pdo->prepare("SELECT image_id FROM images WHERE image_id > $image_id ORDER BY image_id ASC LIMIT 1");
if($result){
$next_id = $result->fetch();
}
//get previous picture id
$result = $pdo->prepare("SELECT image_id FROM images WHERE image_id < $image_id ORDER BY image_id DESC LIMIT 1");
if($result){
$prev_id = $result->fetch();
}
if($result){
//construct next/previous button
$prev_button = (isset($prev_id) && $prev_id>0)?'<a href="#" data-id="'.$prev_id.'"><img src="prev.png" /></a>':'';
$next_button = (isset($next_id) && $next_id>0)?'<a href="#" data-id="'.$next_id.'"><img src="next.png" /></a>':'';
Upvotes: 0
Views: 278
Reputation: 7853
There are many problems in your code:
prepare
method doesn't return the result directly. It returns an executable statement. They needed to be executed explicitly.$image_id
through the prepared statement instead of attach it before prepare.fetch()
doesn't return the $prev_id
or $next_id
. Instead, it returns a result row. By default, it should be an associated array with keys equal to fields in the row.To fix your code should look something like this:
// get next picture id
$stmt = $pdo->prepare('SELECT image_id FROM images WHERE image_id > :image_id ORDER BY image_id ASC LIMIT 1');
if($stmt){
$stmt->execute(array(':image_id' => $image_id));
if (($row = $result->fetch()) !== FALSE) {
$next_id = $row['image_id'];
}
}
// get previous picture id
$stmt = $pdo->prepare('SELECT image_id FROM images WHERE image_id < :image_id ORDER BY image_id DESC LIMIT 1');
if($stmt){
$stmt->execute(array(':image_id' => $image_id));
if (($row = $result->fetch()) !== FALSE) {
$prev_id = $row['image_id'];
}
}
// construct next/previous button
$prev_button = (isset($prev_id) && $prev_id>0)?'<a href="#" data-id="'.$prev_id.'"><img src="prev.png" /></a>':'';
$next_button = (isset($next_id) && $next_id>0)?'<a href="#" data-id="'.$next_id.'"><img src="next.png" /></a>':'';
Upvotes: 1