themeparkfocus
themeparkfocus

Reputation: 187

Echo separate array results as part of a MySql query PHP foreach loop

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.

Upvotes: 0

Views: 147

Answers (1)

Dwight
Dwight

Reputation: 72

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

echo '<div class="'. $flex[$key] .'"> 
...rest of your code
}

Upvotes: 1

Related Questions