简体   繁体   中英

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

Php beginner here. I have php loop on page to display images (basically a gallery) and each image has a title displayed below. 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).

<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 . I don't know where to start.

image_link.php contains basically the same code without the loop:

$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.

In 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. 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:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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