Thursday 15 May 2014

php - Dynamic Array Name in Foreach Loop? -



php - Dynamic Array Name in Foreach Loop? -

i have page several links such grade.php?item=kindergarten, item values different. goal have detail pages each class.

on target page, detail page, i'm using foreach loop, can't work dynamically.

<?php foreach ($classprojects $grade => $item) { ?> <li><?php echo $item[image]; ?><a href="grade.php?item=<?php echo $grade; ?>"><?php echo $item[title]; ?></a><br /><p><?php echo $item[sentence]; ?></p><br /><a href="grade.php?item=<?php echo $grade; ?>">more &rsaquo;&rsaquo;</a></li> < } ?>

as long $classprojects used, details listed correctly, there different details depending on item name. have separate arrays each class. attempts @ making array name dynamic have not worked.... when able echo right string.... , alter it's type array.

perhaps i'm approaching wrong way? improve off modifying array include layer of depth, rather trying capture item value , have dynamically name array in foreach loop?

i utilize example. thanks!

@darylgill additional information:

the contents of array working (this 1 of 6 arrays set same way)

$kinderprojects = array(

array( title => "fall leaves", blurb => "our kinders have enjoyed learning difference between warm colors (yellow, orange, red) , cool colors (green, blue, violet) , putting knowledge utilize painting fall leaves in watercolor , oil pastel. reviewing utilize of visual fine art elements of line , shape.", image => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>" ), array( title => "francis", blurb => "francis knows stuff. big sis of frankie himself, runs show. don't miss margherita mondays!", image => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>" ), array( title => "carlos", blurb => "carlos epitome of phrase &ldquo;don't justice book it's cover&rdquo; &mdash; cannot find improve chef.", image => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>" ), ); your expected results

that selecting each link on prior page direct users detail page specific grade level (i.e., pull right array grade).

what info required expected results

the foreach loop on details page can't have hardwired pointer specific array, or 6 links go data. needs capture item value that's passed in url. thinking way create variable dynamically update array name in foreach loop.

<?php foreach ($classprojects $grade => $item) { ?>

if $classprojects dynamically update '$kinderprojects' when kindergarden link clicked, pull right info array.

you can request info based on parameter , assign array you're looping through.

for example:

$data_array_one = array( array( title => "fall leaves", blurb => "our kinders have enjoyed learning difference between warm colors (yellow, orange, red) , cool colors (green, blue, violet) , putting knowledge utilize painting fall leaves in watercolor , oil pastel. reviewing utilize of visual fine art elements of line , shape.", image => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>" ), array( title => "francis", blurb => "francis knows stuff. big sis of frankie himself, runs show. don't miss margherita mondays!", image => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>" ) ); $data_array_two = array( array( title => "fall leaves", blurb => "our kinders have enjoyed learning difference between warm colors (yellow, orange, red) , cool colors (green, blue, violet) , putting knowledge utilize painting fall leaves in watercolor , oil pastel. reviewing utilize of visual fine art elements of line , shape.", image => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>" ), array( title => "francis", blurb => "francis knows stuff. big sis of frankie himself, runs show. don't miss margherita mondays!", image => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>" ) ); switch ($_get['item']) { case 'kindergarten': $classproject = $data_array_one; break; case 'tiergarten': $classproject = $data_array_two; break; default: // define default info here $classproject = array(); break; } foreach ($classprojects $grade => $item) { echo '<li>'. $item['image'] .'<a href="grade.php?item='. $grade .'">'. $item['title'] .'</a><br /><p>'. $item['sentence'] .'</p><br /><a href="grade.php?item='. $grade .'">more &rsaquo;&rsaquo;</a></li>'; }

php arrays foreach

No comments:

Post a Comment