Forum Moderators: coopster

Message Too Old, No Replies

Extracting data into checkboxes or radio buttons

         

toltec75

7:13 pm on Sep 27, 2004 (gmt 0)

10+ Year Member



I wonder what is the standard way to extraxt data into checkboxes from database?
Let`s say you have a table (users) in which for each user there are different values stored in a way that username is repeated in each row for differnet value of type of music!

In another table (music_type) you have types of music with their names and values!

I had it done this way and it works great!


...<td>Music type:</td>
<td><table width=\"100%\" border=\"0\">";

$sql2= "select name, music_value from music_type";
$resul2t=mysql_query($sql2) or die(mysql_error());
while ($row2=mysql_fetch_array($resul2t))
{
$music_value=$row2["music_value"];
$name=$row2["name"];

echo"<tr><td><input name=\"music_type[]\" type=\"checkbox\" value=\"$music_value\"";

$sql3="select music_value from users where user = '$_POST[user]'";

$result3 = mysql_query($sql3) or die(mysql_error());

while ($row3 = mysql_fetch_array($result3)){

$music = $row3["music_type"];

if ($music == $music_value)
{
echo"checked";
}
}
echo"
>$name</td></tr>";
}

echo "</table></td>
</tr>
...

It works for radio buttons too and I guess it could be modified for select menus!

I just wanna know if this is the standard way of doing this sort of thing, are there other ways, because I kinda came out with this myself!

I found some tutorials on the net that explain similar things and they were way too complicated!

jatar_k

6:48 pm on Sep 28, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



looks fine to me

I often grab all the information for the page right at the top and try to do it in a single query. Then I load it into an array and use that array to build the page.

The theoryis similar though, if it works for you then go with it.