0

I'm sure this is simple but I can't figure it out. I'm running a php foreach loop to echo out results from a MySQL query into individual DIV containers. The Divs are various sizes determined by CSS classes. I'm trying to put the order of classes into the foreach loop to format the DIVs as they are generated from the loop.

I control how many results come from the MySql query so can ensure the separate array has one item per result. The array looks like this

$flex = array("large", "large", "small", "small", 
"small-third", "small-third", "small", "small", "large", "large", ); 

And this is an example of how I'm doing my foreach loop:

        foreach ($result AS $row) {
    
        $title = $row['title'];
        $under_image = $row['under_image'];
        $over_image = $row['over_image'];
        
        echo '<div class="'. $flex .'"> 
    <p>'. $title .'</p>
    </div>'}

Where $flex would actually pull the next result from the array. How would I achieve this?

Hope that's clear, thanks for the help.

1 Answer 1

1
foreach($result as $key=>$row){
...your code, 

echo '<div class="'. $flex[$key] .'"> 
...rest of your code
}
Sign up to request clarification or add additional context in comments.

2 Comments

That appears to have worked, thank you. I would have never figured it out. Now to study it so I understand how it works. Thanks again!
for reference $key is index of array, in this case $result array.

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.