简体   繁体   English

php:使用变量名在不同页面上显示特定图像

[英]php: use variable name to display specific image on diffrent page

Php beginner here. Php初学者在这里。 I have php loop on page to display images (basically a gallery) and each image has a title displayed below. 我在页面上有php循环来显示图像(基本上是一个图库),每个图像都有一个显示在下面的标题。 Those titles are links to second page where only one image is shown. 这些标题是指向第二页的链接,其中只显示一个图像。 The problem is that every link has a different name (value of name is specific /ID of image). 问题是每个链接都有不同的名称(名称的值是特定的/图像的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>";
    }
?>

I'm getting a single image displayed but no matter what title I click, I always get last image from the table. 我正在显示一个图像,但无论我点击什么标题,我总是从表中获得最后一张图像。 How can I catch a specific ID on image_link.php , or should I use some flexible kind of address like /image_link.php?id=34 . 如何在image_link.php上捕获特定的ID,或者我应该使用一些灵活的地址,如/image_link.php?id=34 I don't know where to start. 我不知道从哪里开始。

image_link.php contains basically the same code without the loop: 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>";

Thanks in advance for help. 在此先感谢您的帮助。

将查询更改为:

$sql_select = "SELECT * FROM images WHERE ID =" $_GET['id'];

No need for a form 不需要表格

first script : 第一个脚本:

<?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>";
}
?>

second script 第二个脚本

$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>";

I'd advice you though to use prepared statement instead of simple query constructions. 我建议你使用预准备语句而不是简单的查询结构。

echo 
"<figcaption>
<a href='image_link.php?id=$table_row[ID]' name='$table_row[ID]'>"
 .$table_row['tytul'] ."
</a>
</figcaption>
</figure></div>";

You need to pass the id as a parameter in your href with $table_row[ID] as value. 您需要将id作为参数传递给href,并将$ table_row [ID]作为值。

In image_link.php 在image_link.php中

Use: 采用:

$sql_select = "SELECT * FROM images WHERE ID = '$_GET[ID]'"; 

You had the right idea by saying that you want to pass the id of the image. 通过说你想要传递图像的id,你有正确的想法。 your code should looks like this : 您的代码应如下所示:

<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: 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>";

Please note that the use of mysql_* function is highly deprecated, you should use mysqli extention instead 请注意,mysql_ *函数的使用被高度弃用,你应该使用mysqli扩展

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM