0

I want to loop thru ALL results and then check the ones that are selected from the database (2 tables). Right now it displays all results and checks the right ones but my results are duplicated. What can I do to fix this?

What Im getting now

x item1
item1
item1
item2
x item2
item2
item3
item3
x item3
etc...

Result I want

x item1
x item2
x item3
item4
item5
x item6
etc

Code Im using

<? foreach($modules as $key => $module): ?>
    <? foreach($selectedmodules as $key => $selected):?>
        <input type="checkbox" name="" value="<?=$module->module_id?>"
        <?=($selected->module_sel_id == $module->module_id ? 'checked="checked"' : '') ?>/><?=$module->module_name?><br />
    <? endforeach; ?>
<? endforeach; ?>

Model (Im using Codeigniter)

public function getModules()
{   
    $this->db->select('*');
    $this->db->from('module_type a');
    $this->db->join('module b', 'a.type_id = b.type_id');

    return $this->db->get()->result();
}

public function getSelectedModules($id)
{
    $this->db->select('b.module_sel_id');
    $this->db->from('module a');
    $this->db->join('module_select b', 'a.module_id = b.module_sel_id');
    $this->db->where('b.product_id', $id);

    return $this->db->get()->result();
}
2
  • 2
    why you putting two loops? The code you wrote is not enough to diagnose the problem Commented Aug 27, 2012 at 4:29
  • 1
    Please explain the part of "duplicated results". Then show the data example from the database and last - the query itself. Commented Aug 27, 2012 at 4:33

1 Answer 1

1

Here's a version that will work, but it's not the best way, and you should definitely consider finding a way to avoid the inner loop.test to see if it should be checked.

<? foreach($modules as $key => $module): ?>
    <? $checked = false; ?>
    <? foreach($selectedmodules as $key => $selected):?>
        <? if($selected->module_sel_id == $module->module_id) $checked = true; ?>
    <? endforeach; ?>
    <input type="checkbox" name="" value="<?=$module->module_id?>"
    <?=($checked ? 'checked="checked"' : '') ?>/><?=$module->module_name?><br />
<? endforeach; ?>

The current way is looping through all checkboxes, then all selected checkboxes. The best way would be to loop through all checkboxes, and then test to see if it should be checked. Here's some pseudocode:

<? foreach($modules as $key => $module): ?>
    <input type="checkbox" name="" value="<?=$module->module_id?>"
    <?=(in_array($module>module_id, $selectedmodules) ? 'checked="checked"' : '') ?>/><?=$module->module_name?><br />
<? endforeach; ?>
Sign up to request clarification or add additional context in comments.

4 Comments

You were spot on about your first version, worked. Im trying the second one, does the in_array going to work if Im using objects?
@dynamo, after taking a look at your newly posted code, I think it'd be better to combine your queries (if possible, I'm not familiar with CodeIgniter), so you have the information available in the first loop. Also, for performance reasons, it's typically better to not use the wildcard * to select columns in a table when you only need a few. This causes the server to have to return all of the columns, rather than just the ones you need.
Yea Im aware of the wildcard I usually just do that until I figure out what I need. I also know people hate the php shorthand. So basically I should try to do a subquery to get the selected options?
@dynamo, yes, that'd be ideal. And I agree with the PHP shorthand argument. Not all servers have it turned on by default (IIRC, most have it turned off), although this doesn't apply to <?=$var?>, at least not in PHP 5.4+.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.