Jump to content

Retrieving checkbox values from database


trassalg

Recommended Posts

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?

 

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

Link to comment
Share on other sites

<input type="checkbox" name="list[]" value="$i">

 

The [] in list will tell the script [when passed] that, that variable is an array.

 

$list = $_POST['list'];

foreach($list AS $id){
$sql = "DO THIS TO THIS TABLE WHERE `id`=$id";
$res = mysql_query($sql) or die(mysql_error());
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.