So I have upload form wich uploads image to foldar and save name to database(mysql). So far I have this:
<?php
mysqli_query($connection, "SELECT * FROM images LIMIT 1");
$result = mysqli_query($connection, $sql) or die ("SQL Error: ".mysqli_error($connection));
while($row = mysqli_fetch_array($result)){
echo "<div class=\"picture\">";
echo "<p>";
echo "<img src=\"upload/" . $row['filename'] . "\" alt=\"\" /><br />";
echo $row['caption'] . "<br />";
echo "</p>";
echo "</div>";
}
?>
How can I change the code to show me one image at the time.. Like sort of gallery with buttons 'next' and 'prev' to change images? In DB I store only 'id' and 'filename'. Thanks
edit: I made it like this
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
} else {
$id = 1;
$res=mysqli_query($connection, "select * from images order by id ASC LIMIT $id, 1");
$prevSQL = mysqli_query($connection, "SELECT * FROM images WHERE id > '".$id."' LIMIT 1");
$nextSQL = mysqli_query($connection, "SELECT * FROM images WHERE id < '".$id."' LIMIT 1");
$row = mysqli_fetch_array($res);
if($prevSQL === FALSE) {
die("SQL Error: " . mysqli_errno($connection));
}
if($nextSQL === FALSE) {
die("SQL Error: " . mysqli_errno($connection));
}
if(mysqli_num_rows($prevSQL)>1){
$prevRow = mysqli_fetch_array($prevSQL);
$prev = '<a href="show.php?page='.$prevRow['id'].'">Prev</a>';
} else {
$prev = 'Prev';
}
if(mysqli_num_rows($nextSQL)<1){
$nextRow = mysqli_fetch_array($nextSQL);
$next = '<a href="show.php?page='.$nextRow['id'].'">Next</a>';
} else {
$next = 'Next';
}
echo "<img src=\"upload/" . $row['filename'] . "\" alt=\"\" /><br />";
echo $row['caption'] . "<br />";
echo "</p>";
echo "<a href='show.php?page=".$id."'>Next</a>";
echo "<br/>";
echo "<a href='show.php?page=".$id."'>Prev</a>";
echo "</div>";
}
?>
I make it this way
$query = "select * from images order by id asc";
$result = mysqli_query($connection, $query) or die("Query failed: " . mysqli_errno($connection));
$line = mysqli_fetch_array($result, MYSQL_BOTH);
if (!$line) die('Empty table');
$previd = -1;
$currid = $line[0];
if (isset($_GET['id'])) {
do {
$currid = $line[0];
if ($currid == $_GET['id']) break;
$previd = $currid;
$line = mysqli_fetch_array($result, MYSQL_BOTH);
} while ($line);
}
if ($line) {
echo "<div id=\"picture\">";
echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/" . $line['filename'] . "\" alt=\"\" /><br />";
echo $line['caption'] . "<br />";
echo "</p>";
} else echo "No records.
";
if ($previd > -1) echo '<a href="show.php?id='.$previd.'">Prev | </a>';
$line = mysqli_fetch_array($result, MYSQL_BOTH);
if ($line) echo '<a href="show.php?id='.$line[0].'">Next </a>';
echo '<br>';
echo "</div>";
Using MySQL Limit you can do this. Note that it takes two parameters. The first is the starting record so:
SELECT * FROM images
order by ID ASC
LIMIT $page, 1
By passing a new $page with every click, you get the next record by ID.
To make sure you have a valid $page at all times:
if isset($_GET['page']) {
$page = $_GET['page'];
} else {
$page = 1;
}