这里是 Php 初学者。我在页面上有 php 循环来显示图像(基本上是一个画廊),每个图像都有一个标题显示在下面。这些标题是指向仅显示一张图像的第二页的链接。 问题是每个链接都有不同的名称(名称的值是图像的特定/ID)。
<form action="image_link.php" method="post">
<?php>
$sql_select = "SELECT * FROM images ORDER BY data DESC";
$sql_table_selected = mysql_query($sql_select);
while ($table_row = mysql_fetch_array($sql_table_selected)){
echo "<div class=\"galery-1-3\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption><a href=image_link.php name=\"" . $table_row['ID'] . "\">" . $table_row['tytul'] . "</a></figcaption></figure></div>";
}
?>
我正在显示一张图片,但无论我点击什么标题,我总是从表格中获取最后一张图片。我如何才能在 image_link.php
上捕获特定 ID,或者我应该使用某种灵活的地址,如 /image_link.php?id=34
。我不知道从哪里开始。
image_link.php 包含基本相同的代码,但没有循环:
$sql_select = "SELECT * FROM images WHERE ID = $SomehowGetID";
$sql_table_selected = mysql_query($sql_select);
$table_row = mysql_fetch_array($sql_table_selected);
echo "<div class=\"galery-big\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption>" . $table_row['tytul'] . "</figcaption></figure></div>";
在此先感谢您的帮助。
最佳答案
不需要表格
第一个脚本:
<?php
$sql_select = "SELECT * FROM images ORDER BY data DESC";
$sql_table_selected = mysql_query($sql_select);
while ($table_row = mysql_fetch_array($sql_table_selected)){
echo "<div class=\"galery-1-3\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption><a href=\"image_link.php?id=" . $table_row['ID']
. "\" name=\"" . $table_row['ID']
. "\">" . $table_row['tytul'] . "</a></figcaption></figure></div>";
}
?>
第二个脚本
$SomehowGetID=$_GET['id'];
$sql_select = "SELECT * FROM images WHERE ID = $SomehowGetID";
$sql_table_selected = mysql_query($sql_select);
$table_row = mysql_fetch_array($sql_table_selected);
echo "<div class=\"galery-big\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption>" . $table_row['tytul']
. "</figcaption></figure> </div>";
尽管我建议您使用准备好的语句而不是简单的查询结构。
关于php:使用变量名在不同页面上显示特定图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37569993/