簡體   English   中英

php:使用變量名在不同頁面上顯示特定圖像

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM