0

I need to populate a dropdown list on the basis of the values in an array. This is my code.

Suppose a user has ids 1,2,3. Then the table_group corresponding to these ids must be selected and displayed in a dropdown. Also there should not be any repeating values like in this case in id 2 and 3 value 23 repeats, in this case only one value will be taken. Here $table_id is fetched from USERS TABLE.

     $mod = explode(',',$table_id);
     $mod = array('1','2','3')
     $res = array();

     function addItems($items, $arr) {
        foreach ($items as $value) {
            if (!in_array($value, $arr)) {
                $arr[] = $value;
            }
        }

        return $arr;
     }

     $res = array();
     for ($i = $mod[0]; $i <= end($mod); $i++) {
        $query = $this->db->query("SELECT table_group FROM group where table_id = '$i'");
        $row = $query->row();
        $grp = $row->table_group;
        $group = explode(',', $grp);
        $res = addItems($group, $res);
     }
     return $res;

Its actually working, but the error occuring here is that suppose user selects 1,2,4. In this only the table_group values of 1,2 and 4 must be selected. But with my code, values of 1,2,3,4 are selected. If its 3,4,5 or 2,3,4 etc. then code works just fine. How can i solve this error? Just need to rectify the code.

5
  • 4
    That's a bad database design. You should start fixing your "problem" by normalizing the design, after which (in general) these types of problems go away or at least become MUCH simpler Commented Nov 19, 2012 at 2:13
  • @Marc B: can you be more specific. Did you mean by making rows not to contain same values. Commented Nov 19, 2012 at 2:24
  • He means that you should not store comma separated values in a field and expect to be able to search through them efficiently. forums.phpfreaks.com/topic/… Commented Nov 19, 2012 at 2:29
  • @Zane Edward Dockery: if we store seperated by commas isnt it possible to convert it to array format using explode function Commented Nov 19, 2012 at 5:40
  • Theoretically, yes, you can do that... but in doing so you are creating redundant code. Imagine working for some company, and your job is to search through records. Instead ofyour boss giving you access to a DB, he hands you 99 pages of papers with lists of hundreds of people. You are then asked to arrange them all by their last payment date. It is easy enough to do but it will take a lot of work... in other words, you are making PHP do more work than necessary when you could already have these records at your disposal given the database was normalized properly. Commented Nov 19, 2012 at 6:15

2 Answers 2

1

Change,

for ($i = $mod[0]; $i <= end($mod); $i++) {
$query = $this->db->query("SELECT table_group FROM group where table_id = '$i'");

to

for ($i = 0; $i < count($mod); $i++) {
$query = $this->db->query("SELECT module_group FROM module_group where module_id = '$mod[$i]'");
Sign up to request clarification or add additional context in comments.

Comments

0

Not withstanding what the others have written;

You've got 2 queries which you can put into an associated array of ID vs GROUP:

idVsgroups array
[1] = array(value11, value12, value 13)
[2] = array(value21, value 22, value 23)
[3] = array(value31, value 23)
[4] = array(value 41, value 13)
[5] = array(value 51)

After that, if 1, 2 and 3 are selected do the following:

foreach ($selected as $key) { $result[] = $selected[$key] }

Which will put everything into $selected - really easy aye? After that try and use array_unique or do what this guys already written for you.

Comments

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.