![](/img/trans.png)
[英]how to retrieve data from db and display it in two tables or more in php?
[英]how to display data from two tables in php
我有一個數據庫,兩個表sight_country
和sightseeing
。 我從插入鄉間田野的ID sight_country
表s_country
表的字段sightseeing
。 在php中,我在CSS下拉菜單欄中顯示了sight_country
國家/地區字段值。
該代碼是
<li class="menu-item-has-children"><a href="#">Sightseeing</a>
<ul class="sub-menu">
<?php
$qry_st = "select * from `sight_country` limit 5";
$rec_st = mysql_query($qry_st );
if( mysql_num_rows($rec_st) > 0)
{
while($res_st = mysql_fetch_array($rec_st))
{
echo "<li><a href='sightseeing.php?id=$res_st[id]'>".$res_st['country']."</a></li>";
}
}
?>
</ul>
</li>
當單擊縣值的鏈接時,我將在php頁面中顯示來自表格觀光的所有觀光數據。
該代碼是
$sql = "select * from `sightseeing` where `s_country` ='$id'";
$res = mysql_query($sql);
$rec = mysql_fetch_array($res);
該國家可能有兩個或多個相關的觀光數據,因此我在PHP頁面的側邊欄菜單中顯示了觀光表中的觀光標題。
該代碼是
<ul class="st_lnks">
<?php
$qry_st = "select * from `sightseeing` where s_country = '$id'";
$rec_st = mysql_query($qry_st );
if( mysql_num_rows($rec_st) > 0)
{
while($res_st = mysql_fetch_array($rec_st))
{
echo "<li><a href='sightseeing.php?id=$res_st[s_country]'>".$res_st['stitle']."</a></li>";
}
}
?>
</ul>
當我單擊標題的鏈接時,我想在同一頁面中顯示其相關的觀光數據。 怎么做?
您可以為第二個鏈接添加第二個參數。 給第一個參數一個唯一的名稱
<ul class="sub-menu">
<?php
$qry_st = "select * from `sight_country` limit 5";
$rec_st = mysql_query($qry_st );
if ( mysql_num_rows($rec_st) > 0) {
while ($res_st = mysql_fetch_array($rec_st)) {
echo "<li><a href='sightseeing.php?idCountry=$res_st[id]'>".$res_st['country']."</a></li>";
}
}
?>
</ul>
然后在生成第二個鏈接時添加第二個參數:
<ul class="st_lnks">
<?php
$qry_st = "select * from `sightseeing` where s_country = '$id'";
$rec_st = mysql_query($qry_st );
if ( mysql_num_rows($rec_st) > 0) {
while($res_st = mysql_fetch_array($rec_st)) {
echo "<li><a href='sightseeing.php?idCountry=$idCountry&idSightseeing=res_st['id']'>".$res_st['stitle']."</a></li>";
}
}
?>
</ul>
我假設是這樣;
現在,另外顯示所選觀光的詳細信息(用戶單擊); 修改第二個列表中的鏈接。 而不是:
echo "<li><a href='sightseeing.php?id=$res_st[s_country]'>".$res_st['stitle']."</a></li>";
寫:
echo "<li><a href='sightseeing.php?id=$res_st[s_country]&ss_id=$res_st[id]'>".$res_st['stitle']."</a></li>";
現在,當您單擊鏈接中的一個並返回到sightseeing.php時,您還將獲得另一個獲取變量GET ['ss_id'](其中包含您要查看的觀光活動的ID)。 您可以使用此變量提取觀光的相關細節。
$sightSeeingId = $_GET['ss_id'];
$sql3 = "select * from `sightseeing` where `id` ='$sightSeeingId' LIMIT 1";
$res3 = mysql_query($sql3);
$sightSeeingData = mysql_fetch_array($res3);
檢查它是否有數據並打印出來
if(!$res3) die(mysql_error());
if(mysql_num_rows($res3) > 0){
echo "Sight Seeing id:" . $sightSeeingData['id'];
}
附帶說明一下,您應該知道mysql_ *函數已經過時,並且您的代碼對於sql注入是脆弱的,請參見此處;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.