0

I have a page with several links such as grade.php?item=kindergarten, where the item values are different. My goal is to have detail pages for each class.

On the target page, a detail page, I'm using a foreach loop, but I can't get it to work dynamically.

<?php foreach ($classProjects as $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 as $classProjects is used, the details are listed correctly, but there are different details depending on the item name. I have separate arrays for each class. My attempts at making the array name dynamic have not worked.... even when I was able to echo the correct string.... and change it's type to an array.

Perhaps I'm approaching this the wrong way? Would I be better off modifying my array to include another layer of depth, rather than trying to capture the item value and have that dynamically name my array in the foreach loop?

I could use an example. Thanks!

@DarylGill Additional Information:

  1. The contents of your array which you are working with (This is one of 6 arrays set up the same way)

$kinderProjects = array(

                    array(

                            title       => "Fall Leaves",
                            blurb       => "Our Kinders have enjoyed learning the difference between the warm colors (yellow, orange, red) and the cool colors (green, blue, violet) and are putting this knowledge to use by painting fall leaves in watercolor and oil pastel. They are also reviewing the use of the visual art elements of line and shape.",
                            image       => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"

                         ),

                    array(

                            title       => "Francis",
                            blurb       => "Francis knows her stuff. The big sister of Frankie himself, she runs the show. Don't miss her Margherita Mondays!",
                            image       => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"

                         ),

                    array(

                            title       => "Carlos",
                            blurb       => "Carlos is the epitome of the phrase &ldquo;Don't judge a book by it's cover&rdquo; &mdash; You simply cannot find a better chef.",
                            image       => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"

                         ),

               );
  1. your expected results

That selecting each link on the prior page will direct users to a detail page for that specific grade level (i.e., pull from the correct array for that grade).

  1. what information is required for the expected results

The foreach loop on the details page can't have a hardwired pointer to a specific array, or all the 6 links will go to that data. It needs to capture the item value that's passed in the URL. I was thinking that a way to do that would be to make another variable which would dynamically update the array name in the foreach loop.

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

if $classProjects could dynamically update to '$kinderProjects' when the kindergarden link is clicked, it would pull the correct data from that array.

8
  • 1
    Can you provide an example where your method is not working? Commented Oct 15, 2014 at 14:03
  • 1
    You should quote your array indexes $item['image'] instead of $item[image] Commented Oct 15, 2014 at 14:05
  • I can provide an example, but I'm afraid I'm going down the wrong path. Can someone speak to whether it's best to have separate arrays and somehow reflect that in the target page's foreach loop? Commented Oct 15, 2014 at 14:34
  • 1
    Not sure how to assist you with the data provided. Give more details on what item types we are dealing with and what should happen differently for each one of them. Commented Oct 15, 2014 at 14:50
  • The item types (text strings and an image) are not different between arrays. I just have arrays for each grade level. Each link on the first page is a grade level. Commented Oct 15, 2014 at 15:32

2 Answers 2

1
$count = count($classProjects);
for($i = 0 ;$i < $count ; $i++){
 echo $classProjects[$i];
 echo $anotherArray[$i];//or what ever you want to print here.
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure how to use this. What I would like to do is be able to take the passed value from the 'item=kindergarten', for example, and have the 'kindergarten' value inserted into where 'class' is in my detail page's foreach loop: <?php foreach ($classProjects as $grade => $item) { ?> Does that make sense?
it sounds like you are wanting to use variable variables: php.net/manual/en/language.variables.variable.php
0

You can request the data based on a parameter and then assign that to the array you're looping through.

For example:

$data_array_one = array(
    array(
        title       => "Fall Leaves",
        blurb       => "Our Kinders have enjoyed learning the difference between the warm colors (yellow, orange, red) and the cool colors (green, blue, violet) and are putting this knowledge to use by painting fall leaves in watercolor and oil pastel. They are also reviewing the use of the visual art elements of line and shape.",
        image       => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"
    ),
    array(
        title       => "Francis",
        blurb       => "Francis knows her stuff. The big sister of Frankie himself, she runs the show. Don't miss her 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 the difference between the warm colors (yellow, orange, red) and the cool colors (green, blue, violet) and are putting this knowledge to use by painting fall leaves in watercolor and oil pastel. They are also reviewing the use of the visual art elements of line and shape.",
        image       => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"
    ),
    array(
        title       => "Francis",
        blurb       => "Francis knows her stuff. The big sister of Frankie himself, she runs the show. Don't miss her 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 the default data here
        $classProject = array();
        break;
}

foreach ($classProjects as $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>';
}

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.