2012-02-22 102 views
1

我想知道是否有人能夠幫助我。單選按鈕列表

我正在使用下面的代碼構造一個單選按鈕作爲選擇每一行數據的方式。

<?php 

mysql_connect("hostname", "username", "password")or 
die(mysql_error()); 
mysql_select_db("database"); 



$result = mysql_query("SELECT userdetails.userid, finds.userid, finds.locationid, detectinglocations.locationid, detectinglocations.locationname, finds.dateoftrip, finds.findname, finds.finddescription FROM userdetails, finds, detectinglocations WHERE finds.userid=userdetails.userid AND finds.locationid=detectinglocations.locationid AND finds.userid = 1"); 
if (mysql_num_rows($result) == 0) 
// table is empty 
    echo 'There are currently no finds recorded for this location.'; 
echo"<table>\n"; 
    while (list($userid, $dateoftrip) = 
    mysql_fetch_row($result)) 
    { 

    echo"<tr>\n" 
    . 
    "<td><input type='radio' name='radio' dateoftrip, value='{$userid}' /></td>\n" 
    ."<td><small>{$dateoftrip}</small><td>\n" 
    ."</tr>\n"; 
    } 
    echo"<tr><td colspan=\"3\">"; 
    echo"<br>"; 
    echo"</td></tr>"; 
    echo'</table>'; 

?> 

我可以管理來構建列表中,但旁邊的單選按鈕的值是「用戶ID」,而我期待把「dateoftrip」對每個單選按鈕。

我一直在這個工作了一段時間,我看不到我的錯誤在哪裏。

我只是想知道是否有人可以看看這個請讓我知道我要去哪裏錯了。

非常感謝

回答

1

mysql_fetch_row()返回從具有相同的順序領域您選擇它們​​的結果資源的數字索引數組。由於您的SELECT列表的前2個字段爲userdetails.userid, finds.userid,因此這兩個字段將返回給您的list()呼叫。

取而代之的是,它更易於使用mysql_fetch_assoc()。你可以使用mysql_fetch_row()並找出你需要的兩列的數字索引,但這很困難。在最好的情況下,首先你只需要在你的SELECT中包含實際需要的兩列,這會導致你的代碼正常工作。

while ($row = mysql_fetch_assoc($result)) { 
    $userid = $result['userid']; 
    $dateoftrip = $result['dateoftrip']; 
    // etc... 
} 

。注意,因爲你取兩列來自不同的表稱爲userid,你應該爲他們取的別名後進行區分:

// Aliases for userdetails.userid, finds.userid: 
$result = mysql_query("SELECT userdetails.userid AS userid, finds.userid AS findsid, finds.locationid, detectinglocations.locationid, detectinglocations.locationname, finds.dateoftrip, finds.findname, finds.finddescription FROM userdetails, finds, detectinglocations WHERE finds.userid=userdetails.userid AND finds.locationid=detectinglocations.locationid AND finds.userid = 1"); 
+0

你好,非常感謝你幫我完成這個。不勝感激。 – IRHM 2012-02-22 14:17:34

1

您的選擇會比更多的數據您正在使用。嘗試減少您的SELECT聲明,以便所選的第一個字段是用戶標識,第二個字段是旅行日期。那麼也許你可以希望你的list聲明能夠按照你的意願工作。

+0

非常感謝您花時間回覆我的帖子和指導。真的很感激。親切的問候 – IRHM 2012-02-22 14:18:13

2

您的問題是在這條線:

while (list($userid, $dateoftrip) = 

給出的查詢是:

SELECT userdetails.userid, finds.userid, finds.locationid, detectinglocations.locationid, detectinglocations.locationname, finds.dateoftrip, finds.findname, ..... 

列表()分配,以便變量由SELECT查詢返回的,前兩個結果列實際上是userid。

我建議使用:

while ($row = mysql_fetch_assoc($result)) { 
[...] 
"<td><small>".$row["dateoftrip"]."</small></td>\n" // you also have a typo here 

或更改您的查詢只返回用戶名和dateoftrip的順序

+0

非常感謝您花時間回覆我的博文和寶貴的知識。親切的問候 – IRHM 2012-02-22 14:16:56