I wonder whether someone may be able to help me please.
I’m using the code below to construct a list with a radio button as a way of selecting each row of data.
<?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>';
?>
I can manage to construct the list but the value next to the radio button is the ‘userid’ whereas I’m looking to put the ‘dateoftrip’ against each radio button.
I’ve been working on this for a while now and I can’t see where my error is.
I just wondered whether someone could perhaps take a look at this please and let me know where I’m going wrong.
Many thanks
mysql_fetch_row()returns a numeric-indexed array from the result resource having fields in the same order as you selected them. Since yourSELECTlist’s first 2 fields areuserdetails.userid, finds.userid, those are the two returned back to yourlist()call.Instead, it’s easier to use
mysql_fetch_assoc(). You could usermysql_fetch_row()and figure out the numeric indices of the two columns you need, but that’s difficult. In the best situation, you would only include the two columns you actually need in yourSELECTin the first place, which would cause your code to work correctly.Note, that since you’re fetching two columns called
useridfrom different tables, you should create aliases for them to differentiate them after fetching: