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

Time:03-11

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 > 
    <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.

CodePudding user response:

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

echo '<div > 
...rest of your code
}
  • Related