Welcome Guest, Not a member yet? Register   Sign In
Big problem with arrays and checkboxes
#1

[eluser]Référencement Google[/eluser]
Hi,

I am facing a big problem to repopulate checkboxes group from a DB.
Here are the situations:

- The checkbox group can be empty the first time, because we have to add the user selection to the DB
- The checkboxes can be selected according to the DB, for exemple in a editing case
- Finally, we have to keep the choice of the user while posting the form for validation.

So here is my actual code:
Code:
function DB_checkBoxes($data, $fieldName, $values = array())
{
    $CI =& get_instance();
    
    $html = '';
    $selected = '';
                  
    foreach($data as $item)
    {
        if (!empty($_POST))
        {
           $selected = $CI->validation->set_checkbox($fieldName, $item->word_ID);
        }
            
        foreach ($values as $value)
        {
            if ($value->word_ID === $item->word_ID)
            {
               $selected = ' checked="checked"';                  
            }
        }
                
        $html .= "    <label class=\"checkbox\">";
        $html .= "&lt;input type=\"checkbox\" name=\"".$fieldName."[]\" value=\"".$item-&gt;word_ID."\"".$selected." /> ".$item->word;
        $html .= "</label>\n";            
    }
        
    return $html;
}

Code explaination:
$data is an array containing all the checkboxes
$fieldName is the name of the input, in my casse it will be for exemple choices[]
$values is the array coming from the DB with the checked checkboxes that was saved by the user

I don't know if somebody can help me, really I tryed almost all day about that and can't find the solution!




Theme © iAndrew 2016 - Forum software by © MyBB