Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        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.

>Solution :

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

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading