Reputation: 499
On my home page I have one image(big) on left side and small box in my right panel where I show random thumb of images on every refresh page. Here is how I show this image:
$rand = mysqli_query($con, "SELECT * from images order by RAND() LIMIT 0,1");
while ($res = mysqli_fetch_assoc($rand))
{
echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$res['name']."\" /><br />";
echo "<div id=\"caption\" style=\"text-align:center;\">".$res['caption']."</div><br />";
echo "<a href=pics.php?id=".$res['id'].">Open in new page »</a>";
}
How to make when someone click on thumb to open that image in new page? This echo "<a href=pics.php?id=".$res['id'].">Open in new window »</a>";
doesn't open the proper image. In fact just refreshing the page but the image is the same.
UPDATE: pics.php
$sql = "SELECT COUNT(*) FROM images";
$result = mysqli_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysqli_fetch_row($result);
$numrows = $r[0];
$rowsperpage = 1;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
} // end if
if ($currentpage < 1) {
$currentpage = 1;
} // end if
$offset = ($currentpage - 1) * $rowsperpage;
$sql = "SELECT name, caption FROM images LIMIT $offset, $rowsperpage";
$result = mysqli_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while ($list = mysqli_fetch_assoc($result)) {
echo "<div id=\"picture\">";
echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$list['name']."\" /></a><br />";
echo "<div id=\"caption\">".$list['caption']."</div><br />";
} // end while
Upvotes: 0
Views: 69
Reputation: 2187
First there is no need to put LIMIT 0,1
Just have
$rand = mysqli_query($con, "SELECT * from images order by RAND() LIMIT 1");
$res = mysqli_fetch_assoc($rand);
echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$res['name']."\" /><br />";
echo "<div id=\"caption\" style=\"text-align:center;\">".$res['caption']."</div><br />";
echo "<a href=pics.php?id=".$res['id'].">Open in new page »</a>";
And in you pics.php you must specify the id of the image in your sql query
$id = abs((int)$_GET['id']);
$sql = "SELECT name, caption FROM images WHERE id='{$id}' LIMIT $offset, $rowsperpage";
$result = mysqli_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while ($list = mysqli_fetch_assoc($result)) {
echo "<div id=\"picture\">";
echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$list['name']."\" /></a><br />";
echo "<div id=\"caption\">".$list['caption']."</div><br />";
} // end while
Upvotes: 1