0

I wish to convert this arrayto an optgroup inthe following way while using the Codeigniter form_dropdown function.

<select>
  <optgroup label="Thomas More Kempen">
    <option value="22">D001</option>
    <option value="23">D002</option>
  </optgroup>
  <optgroup label="Thomas More Geel">
    <option value="2">G001</option>
    <option value="8">A102</option>
  </optgroup>
</select>
value is lokaalId

So far I could show the "Thomas More ..." in optgroup with objects ad chlildren, but so far no dice.

Solved: I changed the way I get my Data from the DB:

function getAllMetCampus() {
        $this->load->model('campus_model');
        $alleCampussen = $this->campus_model->getAll();

        foreach ($alleCampussen as $campus) {
            $lokalen = $this->getAllOnCampusId($campus->id);
            foreach($lokalen as $key=>$value){
                unset($lokalen[$key]);
                $lokalen[$value->lokaalId] = $value->lokaalNr;
            }
            $result[$campus->naam] = $lokalen;
        }
        return $result;
    }

Then I just echo form_dropdown($result);

1
  • 1
    whats your code you are trying ... please add that to your question. Commented Mar 15, 2013 at 14:11

2 Answers 2

2

Optgroups arent that hard in codeigniter:

if you want this:

<select>
  <optgroup label="Thomas More Kempen">
    <option value="22">D001</option>
    <option value="23">D002</option>
  </optgroup>
  <optgroup label="Thomas More Geel">
    <option value="2">G001</option>
    <option value="8">A102</option>
  </optgroup>
</select>

You can simply loop your array like this:

function getAllMetCampus() {
        $this->load->model('campus_model');
        $alleCampussen = $this->campus_model->getAll();
        $options = array();

        foreach ($alleCampussen as $campus) {
            $options[$campus->naam] = array();

            $lokalen = $this->getAllOnCampusId($campus->id);
            foreach($lokalen as $lokaal){
                $options[$campus->naam][$lokaal->lokaalId] = $lokaal->lokaalNr
            }
        }
        return $options;
    }

Makes me wonder, why do you have an ID for campus and lokaal, Are there more rows of campus with the same name, and lokalen with the same number in each campus? It looks like an bad pratice for primary keys to me, can you comment on this?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, the question is four years old already and in the meantime I've graduated. The delivered data model was part of a class assignment.
1

The helper function is looking for arrays inside the main array not stdObjects as your linked input shows, see the source: form_helper:336

You will have to convert your input, maybe something like this:

$options = array_map(function($item){
    return (array)$item;
}, $options);

Or since your input seem to be coming from a db query result, use the result_array() or row_array() methods instead of the result() or row() when you fetch them.

You should be getting an error like this, without converting:

PHP Catchable fatal error:  Object of class stdClass could not be converted to string

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.