2012-02-22 5 views
1

私は誰かが私を助けてくれるかどうか疑問に思います。ラジオボタンリスト

私は以下のコードを使用して、データの各行を選択する方法としてラジオボタン付きのリストを作成しています。私は、各ラジオボタンに対して「dateoftrip」を入れて探しています一方、私はリストが、ラジオボタンに次の値を構築するために管理することができ

<?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」です。

私はしばらくの間この作業を続けてきましたが、どこにエラーがあるのか​​わかりません。

私はちょうど誰かがこれを見て、私がどこに間違っているのかを教えてもらえるかどうか疑問に思った。

感謝

答えて

1

mysql_fetch_row()は、あなたがそれらを選択したのと同じ順序でフィールドを持つ結果リソースから数値索引配列を返します。 SELECTリストの最初の2つのフィールドはuserdetails.userid, finds.useridなので、返された2つのフィールドはlist()コールに返されます。

代わりにmysql_fetch_assoc()を使用する方が簡単です。ユーザーmysql_fetch_row()を使用して、必要な2つの列の数値インデックスを調べることはできますが、それは困難です。最良の状況では、最初にSELECTに実際に必要な2つの列のみを含めると、コードが正常に動作します。あなたが異なるテーブルからuseridと呼ばれる2つの列をフェッチしているので、

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

注意、それは、あなたがフェッチ後にそれらを区別するためにそれらのエイリアスを作成する必要があります。

// 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

こんにちはにこれで私を助けるための多くのおかげで、ユーザーIDとdateoftripを返すようにクエリを変更します。大変感謝しています。 – IRHM

1

はあなたがより多くのデータを選択していますあなたは使用しています。 SELECTステートメントを減らして、選択された最初のフィールドがユーザーIDで、2番目のフィールドがトリップの日付になるようにしてください。それでは、あなたが望むようにあなたのlist声明がうまくいくことを望むかもしれません。

+0

私のポストへの返信とガイダンスのために時間を割いてくれてありがとう。本当に感謝しています。親切には – IRHM

2

あなたの問題は、この行である:クエリ与え

while (list($userid, $dateoftrip) = 

は次のとおりです。

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

リスト()最初の2つの結果カラムは、SELECTクエリによって返される順序で変数を割り当てます実際にはuseridです。

私が使用することをお勧め:

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

あるいは単に、そのため

+0

私の記事と貴重な知識に返信する時間を取ってくれてありがとう。敬具 – IRHM

関連する問題