[英]php: use variable name to display specific image on diffrent page
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>";
在此先感谢您的帮助。
将查询更改为:
$sql_select = "SELECT * FROM images WHERE ID =" $_GET['id'];
不需要表格
第一个脚本:
<?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>";
我建议你使用预准备语句而不是简单的查询结构。
echo
"<figcaption>
<a href='image_link.php?id=$table_row[ID]' name='$table_row[ID]'>"
.$table_row['tytul'] ."
</a>
</figcaption>
</figure></div>";
您需要将id作为参数传递给href,并将$ table_row [ID]作为值。
在image_link.php中
采用:
$sql_select = "SELECT * FROM images WHERE ID = '$_GET[ID]'";
通过说你想要传递图像的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?id=" . intval($table_row['ID']) . "name=\"" . $table_row['ID'] . "\">" . $table_row['tytul'] . "</a></figcaption></figure></div>";
}
?>
image_link.php:
$sql_select = "SELECT * FROM images WHERE ID =" . mysql_real_escape_string($_GET['id']);
$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>";
请注意,mysql_ *函数的使用被高度弃用,你应该使用mysqli扩展
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.