Page 1 of 1

Help using arrays needed.

Posted: Mon May 22, 2006 7:39 am
by spudmclard
Hi

Ive managed to populate a table using an array called from a MYSQL database. The problem im having is as follows..

What im after is when the user selects the required radio button, the form submits this value along with the ID called from the MYSQL database.
Im getting the radio button value but the ID is always the last in the array. Any help would be appreciated....Code follows...

Code: Select all

<?php

$connection = mysql_connect("localhost","****","");
mysql_select_db("dbase", $connection);
$result = mysql_query ("SELECT * FROM access", $connection);
$nrows = mysql_num_rows($result);
if($nrows != 0)
{
print "<form action='sub.php'><TABLE border=1>";
for ($j=0;$j<$nrows;$j++ )
{
$row = mysql_fetch_array($result);
if ($row["access"] == "1") {
 $colour = "red";
} else {
 $colour = "black";
}
$hid[] = $row["id"];
# print "<TD>" . $row["id"] . "</TD>";
print "<TD><font color = '$colour'>" . $row["username"] . "</font></TD><TD><input type='radio' name='block' value='0'><input type='radio' name='block' value='1'><input type='hidden' name='usr' value='$row[username]'><input type='submit' value='GO'></TD></TR>";
}
print "</table></form>\n";
}
?>

Code: Select all

sub.php

Code: Select all

<?php
$data = $_GET['block'];
$hid = $_GET['usr'];
echo "$data";
echo "$hid";
?>

Posted: Mon May 22, 2006 7:59 am
by shiznatix
I don't see where you are using any database ID. if you are talking about the hidden input named 'usr' then of course it will be the last one because you have the same variable re-assigned until its the last one there. you should do something like

Code: Select all

echo '<input type="radio" name="something" value="'.$info['username'].'">';
then the value of $_POST['something'] will be the usrename from the database.

Posted: Mon May 22, 2006 9:17 am
by GM

Code: Select all

//<input type='hidden' name='usr' value='$row[username]'>
<input type='hidden' name='usr' value=\"{$row['username']}\">
Does this help matters?