Welcome Guest, Not a member yet? Register   Sign In
displaying distributed php foreach lists in multidimensional array
#1

[eluser]mx_code_monkey[/eluser]
Hello,

it's well documented that in order to display a list from $data passed from the controller is pretty simple, I have successfully been doing it like this:

Code:
<ul>&lt;?php foreach ($sample_array as $array_item) { ?&gt;<li>&lt;?php echo $array_item; ?&gt; </li>&lt;?php } ?&gt;</ul>


As long as $sample_array is a one-dimensional array, the references work reliably.

But let's say the array fed into the view from $data in the controller is has two dimensions, associative or indexed, and we want to display the data in the same list item as a combined array. Hers some example data:

Code:
$data[$things] = array("thing1"=>"link for thing1","thing2"=>"link for thing2"..."thingX"=>"link for thingX"); // etc.


I'd like to produce the result of listing the items and item links in the same <LI> tags, obviously to make a hyperlink,

Code:
pseudocode: for each thing, echo <li><a src="$things[$thingX]">ThingX key</a></li></ul>

but how do I call the thingX key name? do i have to use a for() loop as an umbrella over the for each lists?

any suggestions for best practice with the view file? I'd rather not deal with combining the array in combining the array using array_combine() in the controller, although that might be one option. If there has been a discussion about this already in this forum, my search did not produce any results. Any comments welcome. Thanks!
#2

[eluser]solid9[/eluser]
You can use two foreach()
or
one for() inside foreach()

Have you tried experimenting first?

Better try it first, If it doesn't work post the complete codes here.
Along with your example array().
#3

[eluser]mx_code_monkey[/eluser]
solid9,

yes and no, i have not tried exactly to nest a for loop within a foreach loop,
but i suppose that is how it could logically work.

i'm just wondering what the best practice is, since to me,
using more than one nested logical control loop within a MVC view
sort of defeats the purpose of separating control code with view code.

for now, I have combined my strings before the output with the full list items pre-formatted as an html string, keeping it to one-dimensional array, easy to display.
#4

[eluser]solid9[/eluser]
Using multiple foreach or for() with foreach is not against the law of MVC in your view.
And trying first before asking is a good character of a good programmer.





#5

[eluser]CroNiX[/eluser]
Code:
$data = array(
  'first' => array('first one', 'first two'),
  'second'=> array('second one', 'second two')
);

//Loop over outer array:
foreach($data as $outter_key => $outter_array)
{
  echo 'Outer Array: "' . $outter_key . '" Values:<br>';  //'first', 'second', etc

  //loop over the inner array for this array
  foreach($outter_array as $inner_array)
  {
    echo 'Inner Array: ' . $inner_array . '<br>';  //'first one', 'first two' etc
  }

  echo '<hr />';  //put a separator after each entry
}
#6

[eluser]mx_code_monkey[/eluser]
[quote author="solid9" date="1350662547"]Using multiple foreach or for() with foreach is not against the law of MVC in your view.
And trying first before asking is a good character of a good programmer.
[/quote]

Noted on the trial by error approach and I concur. I am researching ways to improve my code before I go ahead and improve it. One doesn't have to learn everything by first hand experience. In fact, when I take the approaches offered in response to intelligent questions, I find it can solidify knowledge building.

Having online discussions for research about best practices with more experienced programmers is also good character for a good programmer, I think. For instance, above is a generous helping of code to help me along my way. I very much do appreciate it. I'd rather not have the philosophy debate about putting too much programmer logic in view files, but if you really think about it, there is a grey area here.

Thank you both for now!




Theme © iAndrew 2016 - Forum software by © MyBB