Welcome Guest, Not a member yet? Register   Sign In
Model logic help get preselected colors from two tables??
#1

[eluser]123wesweat[/eluser]
Hi,

In a model i have a function which returns a table with colors and checkboxes. The table is dynamically parsed by reading out colors in tableB (fields are colorID and colorName).

Selected checkboxes are stored as a serialized array in tableA.fieldColors.

I am also using the preset_checkbox helper.
code so far
Code:
function get_color_checkboxes()
    {
        //first check if colors are already set in db
        $query = $this->db->query("SELECT color FROM tableA");
        $res = $query->result();
        $colorsDB=unserialize($res[0]->color);
        //testing
        foreach($colorsDB as $colorSelected ) {
         echo $colorSelected;
        }
        $preselect = 'red';//works
        die();
        //.end testing
        $this->db->select('id ,colorName');
        $colors = $this->db->get('tableB')->result();        
        $msg = '<table>';
        foreach ($colors as $color) {
            $msg .= '<tr><td> &lt;input type="checkbox" value="'.$color-&gt;colorName.'" name="colors[]" '. preset_checkbox('segments[]', $color->colorName, $preselect).' />
<span class="">'.$color->colorName.'</span></td></tr>';
        }
        $msg .= '</table>';
        return $msg;
    }

How do i set the $preselect in the preset_checkbox from the unserialized array??
#2

[eluser]123wesweat[/eluser]
ok i found a solution by

Code:
foreach ($colors as $color) {
            $msg .= '<tr><td> &lt;input type="checkbox" value="'.$color-&gt;colorName.'" name="colors[]" '. preset_checkbox('segments[]', $color->colorName, in_array($color->colorName,$colorsDB)).' />
<span class="">'.$color->colorName.'</span></td></tr>';
        }




Theme © iAndrew 2016 - Forum software by © MyBB