Help using arrays needed.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spudmclard
Forum Newbie
Posts: 16
Joined: Thu Oct 27, 2005 3:06 am

Help using arrays needed.

Post 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";
?>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post 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?
Post Reply