所以我尝试学习php并决定创建一个我添加图像的网站,将它们保存在文件夹和id,名称,类型,mysql中的路径中。然后在页面上显示。到目前为止,我已上传表单,我可以上传和保存图像。我也在页面上成功展示了它们。
现在我正在尝试制作类别 - 自然,有趣......等等。所以我在主表中添加了一个字段 - > img_category。 我也疯了第二张桌子 - 猫咪cat_id和cat_name字段。使用它来显示页面上的类别:
<?php
$q = mysqli_query($con,"select * from cats");
while ($res = mysqli_fetch_assoc($q))
{
echo '<a href="pic.php?cat_id='. $res['cat_id'] .'">'.$res['cat_name'].'<br/>';
}
现在,当我点击某个类别链接以仅从此类别加载图片时,我该如何制作? 我已经设法制作了这样的东西,但它不像预期的那样工作
<?php
$q = mysqli_query($con,"select * from cats");
while ($res = mysqli_fetch_assoc($q))
{
echo '<a href="pic.php?cat_id='. $res['cat_id'] .'">'.$res['cat_name'].'<br/>';
}
?>
<hr>
<?php
$cat_id = $_GET['cat_id'];
$query = "SELECT * FROM images JOIN cats ON images.img_category = cats.cat_id WHERE cats.cat_id = '$cat_id'";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
$line = mysqli_fetch_array($result, MYSQL_BOTH);
if (!$line) echo '';
$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['name']."\" /></a><br />";
echo "<div id=\"caption\">".$line['caption']."</div><br />";
}
else echo "There is no images!\n";
if ($previd > -1) echo '<a href="pic.php?cat_id='.$previd.'" class="prev_pic"><span>Prev</span></a>';
echo str_repeat(' ', 5);
$line = mysqli_fetch_array($result, MYSQL_BOTH);
$query = "select * from images order by RAND() LIMIT 1";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
while ($row = mysqli_fetch_array($result, MYSQL_BOTH)){
echo '<a href="pic.php?cat_id='.$row['id'].'"class="random">Random</a>';
}
echo str_repeat(' ', 5);
if ($line) echo '<a href="pic.php?cat_id='.$line[0].'" class="next_pic"><span>Next</span> </a><br /><br />';
echo "</div>";
?>
结果是:
当显示类别中的图像时,如果我点击“下一步”按钮,我会得到相同的图像。 如果类别中没有图像,我会得到所有回声,例如链接白色,检查的最后一个类别的ID:没有像链接的图像,如果我点击它,我会加载最后一个类别ID。在我的情况下,我有8个类别,所以ID = 8。 任何帮助都很感激! 感谢的
编辑: 好的这一行:
echo '<a href="pic.php?cat_id='.$line[0].'" class="next_pic"><span>Следваща</span> </a>
pic.php在哪里?cat_id = ...我认为是错的。在这里,我必须采取下一个图像ID而不是下一个类别ID但是如何改变图像呢?如果我把它作为pic.php?id = ...我得到空页。 我不明白。我知道这是凌乱的代码,但我现在能做的最好。
编辑2: 我做了这样的事。现在,您可以帮助我如何为下一个图像进行查询,因为现在没有获得下一个图像并保持不变。
$cat_id = $_GET['cat_id'];
$cat_id = mysqli_real_escape_string($con, $cat_id);
$query = "SELECT * FROM images JOIN cats ON images.img_category = cats.cat_id WHERE cats.cat_id = '$cat_id'";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
$prevSQL = mysqli_query($con,"SELECT cat_id FROM cats WHERE cat_id < $cat_id ORDER BY cat_id DESC LIMIT 1") or die (mysqli_error($con));
$nextSQL = mysqli_query($con, "SELECT cat_id FROM cats WHERE cat_id > $cat_id ORDER BY cat_id ASC LIMIT 1") or die (mysqli_error($con));
$prevobj=mysqli_fetch_object($prevSQL);
$nextobj=mysqli_fetch_object($nextSQL);
$pc = mysqli_fetch_object(mysqli_query($con, "SELECT COUNT(cat_id) as pid FROM cats WHERE cat_id<$cat_id ORDER BY cat_id DESC")) or die (mysqli_error($con));
$nc = mysqli_fetch_object(mysqli_query($con, "SELECT COUNT(cat_id) as nid FROM cats WHERE cat_id>$cat_id ORDER BY cat_id ASC")) or die (mysqli_error($con));
$prev=$pc->pid>0 ? '<a href="pic.php?cat_id='.$cat_id.'&id='.$prevobj->cat_id.'">Prev</a> |' : '';
$next=$nc->nid>0 ? '<a href="pic.php?cat_id='.$cat_id.'&id='.$nextobj->cat_id.'">Next</a>' : '';
$row = mysqli_fetch_array($result);
echo "<div id=\"picture\">";
echo "<img src=\"upload/" . $row['name'] . "\" alt=\"\" /><br />";
echo $row['caption'] . "<br />";
echo "</p>";
echo $prev;
echo $next;
答案 0 :(得分:2)
正如你所说,我猜错误在于:
echo '<a href="pic.php?cat_id='.$line[0].'" class="next_pic"><span>Следваща</span> </a>
我认为应该是:
echo '<a href="pic.php?cat_id='.$line[0].'&id='.$next_picture_id.'" class="next_pic"><span>Следваща</span> </a>
修改强> 您的代码应如下所示:
<?php
$q = mysqli_query($con,"select * from cats");
while ($res = mysqli_fetch_assoc($q))
{
echo '<a href="pic.php?cat_id='. $res['cat_id'] .'">'.$res['cat_name'].'<br/>';
}
?>
<hr>
<?php
$cat_id = $_GET['cat_id'];
$query = "SELECT * FROM images JOIN cats ON images.img_category = cats.cat_id WHERE cats.cat_id = '$cat_id'";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
$line = mysqli_fetch_array($result, MYSQL_BOTH);
if (!$line) echo '';
$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['name']."\" /></a><br />";
echo "<div id=\"caption\">".$line['caption']."</div><br />";
}
else echo "There is no images!\n";
if ($previd > -1) echo '<a href="pic.php?cat_id='.$previd.'" class="prev_pic"><span>Prev</span></a>';
echo str_repeat(' ', 5);
$line = mysqli_fetch_array($result, MYSQL_BOTH);
$query = "select * from images order by RAND() LIMIT 1";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
while ($row = mysqli_fetch_array($result, MYSQL_BOTH)){
echo '<a href="pic.php?cat_id='.$row['id'].'"class="random">Random</a>';
}
echo str_repeat(' ', 5);
if ($line) echo '<a href="pic.php?cat_id='.$line[0].'&id='.$line[0].'" class="next_pic"><span>Next</span> </a><br /><br />';
echo "</div>";
?>
答案 1 :(得分:1)
试试这个
<?php
if(isset($_GET['cat_id'])){
$cat_id = $_GET['cat_id'];
$query = "SELECT * FROM images WHERE img_category = '$cat_id'";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
$line = mysqli_fetch_array($result, MYSQL_BOTH);
if (!$line) echo '';
$previd = -1;
$currid = $line[0];
if (isset($_GET['id'])) {
$previous_ids = array();
do {
$previous_ids[] = $line[0];
$currid = $line[0];
if ($currid == $_GET['id']) break;
$previd = end($previous_ids);
$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['name']."\" /><br />\r";
echo "<div id=\"caption\">".$line['caption']."</div><br />";
}
else echo "There is no images!\n";
if ($previd > -1)
echo '<a href="pic.php?cat_id='.$cat_id.'&id='.$previd.'" class="prev_pic"><span>Prev</span></a>';
echo str_repeat(' ', 5);
$line = mysqli_fetch_array($result, MYSQL_BOTH);
$query = "select * from images WHERE img_category = '$cat_id' order by RAND() LIMIT 1";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
while ($row = mysqli_fetch_array($result, MYSQL_BOTH)){
echo '<a href="pic.php?cat_id='.$cat_id.'&id='.$row['id'].'"class="random">Random</a>';
}
echo str_repeat(' ', 5);
if ($line) echo '<a href="pic.php?cat_id='.$cat_id.'&id='.$line[0].'" class="next_pic"><span>Next</span> </a><br /><br />';
echo "</div>\r";
}
?>