2012-02-22 100 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