2013-05-14 88 views
0

我從我的數據庫中檢索產品,並在while循環中列出它們。像這樣點擊鏈接查詢數據庫

<?php 
    while($row = mysql_fetch_row($result)){ 
    echo '<div class="product_info">'; 
    echo '<div class="category_product_title"><a href="category-page.php">Product</a>'; 
    echo '</div>'; 
     echo '<div class="category_product_number">#'.$row[0].'('.$row[1].')'.'</div>';//prints id and name 
    echo '<div class="category_product_description">'.$row[2].'</div>';//prints description 
    echo '<div class="category_product_price">'.$row[4].'TL</div>';//prints price 
     echo '<div class="category_details"><a href="productpage2.php">DETAILS</a></div>';//The link to list the product details 
    echo '</div>'; 
    echo '</div>'; 
} 
?> 

我希望能夠在「詳細信息」鏈接是由獲得該paticular產品的編號或名稱,並使用查詢數據庫點擊打印上paticular prodcut更多細節。 我嘗試將ID保存在像這樣的會話變量$_SESSION['id'] = $row[0]中,但由於我正在循環這些產品,因此單擊時無法獲取特定產品的ID。我怎樣才能做到這一點?。由於循環,我感到困惑。謝謝

回答

2

您可以使用GET變量爲每個產品分配不同的鏈接。
而不是<a href="productpage2.php">DETAILS</a>,鏈接可能指向<a href="productpage2.php?id='.$row[0].'">DETAILS</a>

然後,在產品頁面上productpage2.php,你可以從$_GET全局數組所選擇的ID是這樣的:

$id = $_GET['id']; 
+1

這可能比我的答案對於這種情況更好。所以習慣用ajax做這種事情。 – sgroves

+0

謝謝。我想到了這一點,但不知道是否會因爲循環而保留特定ID。但它顯然運作良好。 – dhani

0

很多方法可以做到這一點。我可能會做這樣的事情:

echo '<div class="product_info" data-product-number="' . $row[0] . '">'; 

然後,只需抓住data-product-number屬性使用JavaScript,並通過其與請求一起。很顯然,一旦請求發生,您需要以某種方式在服務器端進行驗證,因爲用戶可以將請求中的產品編號設置爲任何他們想要的內容。

相關問題