2016-04-25 66 views
0

因此,我正在開發一個基本網站,報告存儲在數據庫中的項目的位置。我想要做的是從存儲在phpmyadmin中的表創建一個結果表。PHP和mysql表

我想要的是讓每個表格行都鏈接到另一個頁面,其中的項目將被展開成更多的細節(單個頁面結果)。例如。人點擊與商店有關的行,然後他們將被引導到該商店的另一個頁面。那麼我該如何使用php。看到我的代碼如下

while($row = mysqli_fetch_assoc($results)){ 
    $ratingStars = ""; 
    $ratingNo = $row[rating]; 

    for($i=0; $i<$ratingNo; $i++){ 
     $ratingStars .= "&#9733; "; 
    } 

    echo "<form action='' method='post'>"; 
    echo "<tr id=". $row['id'] .">"; 
    echo "<td class='lrg-tbl-row'>" . $row["hotspot-name"] . "</td>"; 
    echo "<td class='lrgz-tbl-row'>" . $row["address"] . "</td>"; 
    echo "<td class='lrg-tbl-row'>" . $row["suburb"] . "</td>"; 
    echo "<td class='sml-tbl-row'>" . $ratingStars . "</td>"; 
    echo "<td>" . "<input type='submit' name = 'Submit' value='Text'/>" . "</td>"; 
    echo "</tr>"; 
    echo "</form>"; 
} 
+1

撥打林k提供記錄的id。在請求的頁面加載數據基於該ID .. – chris85

+0

對不起,你是什麼意思chris85 – GaminMatt

回答

0

無需形式提交。只需創建一個鏈接去其他頁面。如果你想要,你可以編碼你的ID值。也

while($row = mysqli_fetch_assoc($results)){ 
    $ratingStars = ""; 
    $ratingNo = $row[rating]; 

    for($i=0; $i<$ratingNo; $i++){ 
     $ratingStars .= "&#9733; "; 
    } 


    echo "<tr id=". $row['id'] .">"; 
    echo "<td class='lrg-tbl-row'>" . $row["hotspot-name"] . "</td>"; 
    echo "<td class='lrgz-tbl-row'>" . $row["address"] . "</td>"; 
    echo "<td class='lrg-tbl-row'>" . $row["suburb"] . "</td>"; 
    echo "<td class='sml-tbl-row'>" . $ratingStars . "</td>"; 
    echo "<td>" . "<a href='<Your domain>/?id=" .$row['id'] . "'>View Details</a></td>"; 
    echo "</tr>"; 

} 
0

作爲Dipanwita Kundu說是很好的方式來重定向到詳細的形式。

我曾嘗試jQuery的方式檢查了..

例如,我改變了一些初始條件。相應地更改提取查詢& URL。 我建議你把表格和表外循環

<!DOCTYPE html> 
<html> 
<?php 
echo "<form action='' method='post'>"; 
echo "<table>"; 
$results = array(
    array('hotspot-name'=>'hotspot1','address'=>'192.168.0.1','suburb'=>'asdf','rating'=>'5','id'=>'1'), 
    array('hotspot-name'=>'hotspot2','address'=>'192.168.0.1','suburb'=>'asdf','rating'=>'4','id'=>'2'), 
    array('hotspot-name'=>'hotspot3','address'=>'192.168.0.1','suburb'=>'asdf','rating'=>'3','id'=>'3'), 
    array('hotspot-name'=>'hotspot4','address'=>'192.168.0.1','suburb'=>'asdf','rating'=>'2','id'=>'4') 
     ); 

$ratingNo = 4; 
foreach($results as $row){ 
    $ratingStars = ""; 
    $ratingNo = $row['rating']; 

    for($i=0; $i<$ratingNo; $i++){ 
     $ratingStars .= "&#9733; "; 
    } 
    echo "<tr id=". $row['id'] .">"; 
    echo "<td class='lrg-tbl-row'>" . $row["hotspot-name"] . "</td>"; 
    echo "<td class='lrgz-tbl-row'>" . $row["address"] . "</td>"; 
    echo "<td class='lrg-tbl-row'>" . $row["suburb"] . "</td>"; 
    echo "<td class='sml-tbl-row'>" . $ratingStars . "</td>"; 
    echo "<td>" . "<input type='submit' name = 'Submit' value='Text'/>" . "</td>"; 
    echo "</tr>"; 

} 
echo "</table>"; 
echo "</form>"; 
?> 

<script src="https://code.jquery.com/jquery-1.12.3.js"></script> 
<script> 
$(document).ready(function(){ 
    $('tr').click(function(){ 
     var id = $(this).attr('id'); 
     var url = 'https://example.com'+'?id='+id; 
     window.open(url, '_blank'); 
    }); 
}); 
</script> 

0

使用錨標記從你的數據庫詳情頁面上重定向,並張貼有詳細信息頁面的URL的ID並獲得該ID的細節並打印出來......

<?php 
while($row = mysqli_fetch_assoc($results)){ 
    $ratingStars = ""; 
    $ratingNo = $row[rating]; 

    for($i=0; $i<$ratingNo; $i++){ 
     $ratingStars .= "&#9733; "; 
    } 

    echo "<form action='' method='post'>"; 
    echo "<tr id=".$row['id'].">"; 
    echo "<td class='lrg-tbl-row'>" . $row["hotspot-name"] . "</td>"; 
    echo "<td class='lrgz-tbl-row'>" . $row["address"] . "</td>"; 
    echo "<td class='lrg-tbl-row'>" . $row["suburb"] . "</td>"; 
    echo "<td class='sml-tbl-row'>" . $ratingStars . "</td>"; 
    echo '<td class="sml-tbl-row"><a href="detail.php?id='.$row['id'].'" >More Details</a></td>'; 
    echo "</tr>"; 
    echo "</form>"; 
} 
?> 

創建一個新的文件detail.php

<?php 
$id=''; 
if(isset($_GET['id'])) 
{ 
    $id =$_GET['id']; 
    if(!empty($id)) 
    { 
     $details = mysql_query('SELECT * FROM table WHERE id = $id'); 
     ////here your will get all the details 
    } 
} 
?>