点击链接时没有显示正确的图像

时间:2014-03-16 20:34:09

标签: php mysql 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 &raquo</a>";
}

当有人点击拇指在新页面中打开该图片时如何制作?此echo "<a href=pics.php?id=".$res['id'].">Open in new window &raquo</a>";无法打开正确的图像。实际上只是刷新页面但图像是一样的。

更新: 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

1 个答案:

答案 0 :(得分:1)

首先,没有必要放LIMIT 0,1只有

$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 &raquo</a>";

在你的pics.php中,你必须在sql查询中指定图像的id

$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