0

I am a little blank about the next thing. I am afraid I am not approaching the problem like it should. I have a multidimensional array to show editors in a custom cms. It looks like this:

$options = array(
array("template","Design skelet","Selecteer het gewenste design","text","false",array("front","content","subcontent","action","special","system"),"true"),
array("is_menu","Menu item","Selecteer de gewenste plaats in het menu","select","false",$catch_menu_sec['alias'],"true"),
array("category","Pagina categorie","Voeg hier de pagina naam in","text","false","","true"),
);

The $options are stored like this:

$option[0] = database value.

$option[1] = readable value for frontend in the cms.

$option[2] = description for the cms user.

$option[3] = type of editor (textarea, text or select)

$option[4] = if the editor has special options.

$option[5] = **where the problem is. Here are the values stored for the options for the select input.

$option[6] = if the field is shown in the cms.

Right now i have a problem with $option[5]. I want to retrieve database values, so that I can get a dropdown list for every menu item in the database. $option[5] is used to create a secundair menu or even tertaire, where you can select the menu item where it should be a child of.

the html output I want it to be is like this:

<select name="is_menu">
    <option name="true">True</option>  <- basic option to set it in primair menu, coming from the array
    <option name="false">False</option>  <- basic option, coming from the array
    <option name="home">Home</option>  <- database value, coming from the array
</select>

I know i cannot place while or foreach loops in an array, but how can i approach this problem otherwise?

Thanks in advance!

PS: I have basic knowledge of PHP within this area, so don't be harsh on my bad explantion please :)

1
  • I'm not sure what you want. Your Array do not contain seven elements. It only have 3 Elements. But I think you want the 5th part of each array inside the $options array (e.g. $option[1][5]) Commented Nov 2, 2016 at 12:46

1 Answer 1

1

Edit: To get sub-arrays in your DB records, either use a left join and GROUP_CONCAT(), or do two queries, one to the main table for your rows, then one to your related table (using the ids from the first result in your WHERE) to add to the first set. This is much easier if you construct your result arrays using the unique/primary key as the index; you can then use array_keys() for the second query, and be able to address rows in the first result by id when adding the related records.


Don't use ‘magic numbers’, make each array in $options an associative array like this:

$options = [
    [
        'db_val' => 'template',
        'label' => 'Design skelet',
        ...
    ],
    ...
];

Then you can iterate your arrays:

foreach ($options as $option) {
    ...
    echo '<select name="', $option['db_val'], '">';
    foreach ($option['select_opts'] as $select_opt => $label) {
        echo '<option value="', $select_opt, '">', $label, '</option>';
    }
    echo '</select>';
    ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

Much cleaner than i did yes. Thanks! Problem of the database values in the array still stands unfortunally, since this does not work.
As written, the question appeared to be about issues iterating loops.
The question is about how to get database values in the array loop :)
Don't. Get your values before you iterate otherwise you're using n + 1 selects. There are many resources explaining why one shouldn't do this, here's one.

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.