Page 1 of 1

Retrieving checkbox values from database

Posted: Fri Aug 03, 2007 7:35 pm
by trassalg
I have the following code and can't seem to get either the checkboxes to show their individual label nor retrieve their respective value from the database. Any ideas what I can do to fix this?

Code: Select all

<?php
function checkbox($name,$label,$value_yn,$input='',$category_array='')
{
    $chk = '';
    $attr = $category_array;
    if(is_array($category_array))
    {
        $attr = '';
        foreach($category_array as $key=>$aValue)
        {
            $attr .= " $key=\"$aValue\"";
        }
    }
    if($value_yn == trim($input))
    {
        $chk = ' checked="checked"';
    }
    $tag = "<label><input type=\"checkbox\" name=\"value[$i]\" value=\"$value_yn\"".$attr.$chk." />$label</label>\n";
    echo $tag;
}
?>

<?php
        $category_data = array(
                 1 => '<strong>1. ALIMENTOS</strong>',
                                ...
                1112 => '11.12 - TeorĂ­as<br>'
      );

?>
<?php
    include("includes/misc.inc");
    include("includes/connection.inc");

    $result = mysql_query("SELECT categories FROM articleTable WHERE articleIDNumber=$articleIDNumber");
    while ($row = mysql_fetch_array($result)) {
    $row = explode(",", $category_data);
            }
    $category_array = array($category_data);

echo "<table>\n
    <tr>\n
        <td>\n";

for ($i = 0; $i < count($category_data); $i++){
checkbox();
    }
?>

    </td>
  </tr>
</table>

Re: Retrieving checkbox values from database

Posted: Fri Aug 03, 2007 8:01 pm
by VladSun

Code: Select all

$result = mysql_query("SELECT categories FROM articleTable WHERE articleIDNumber=$articleIDNumber");
    while ($row = mysql_fetch_array($result)) {
    $row = explode(",", $category_data);
            }
You are overwriting the $row variable. Instead, you need to read it ...