bootstrap grid system w/MySQL

I am using the bootstrap grid system to make rows and columns
I want to dynamically change the rows each time 3 rows are outputted

Meaning I loop through the MySQL result set and display the first 3 results in a row then the next 3 results in the next row and so on….

So the output would be like such
<div class="row">
	<div class="col-md-4">1</div>
	<div class="col-md-4">2</div>
	<div class="col-md-4">3</div>
</div>

<div class="row">
	<div class="col-md-4">4</div>
	<div class="col-md-4">5</div>
	<div class="col-md-4">6</div>
</div

Open in new window

LVL 3
Timothy GoldenWeb DevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
You can use a counter:
<?php
$counter = 0;
while ($row = $result->fetch_assoc()) {
        $counter++;
        if ($counter == 1)
        {
               echo '<div class="row">';
               echo  '<div class="col-md-4">' . $row['name'] . '</div>';
        }
        else if ($counter == 2)
	{
              echo  '<div class="col-md-4">' . $row['secondvalue'] . '</div>';
        }
        else if ($counter == 3)
        {
              echo  '<div class="col-md-4">' . $row['thirdvalue'] . '</div>';
              echo '</div>'; // closing row
              $counter = 0; //reset counter
        }
}

Open in new window

0
Timothy GoldenWeb DevAuthor Commented:
I had thought about using a counter/loop just wasn't sure there is a better way it if I was missing something
0
Julian HansenCommented:
The standard way of doing this is to use a counter with a mod function

echo <<< ROW
    <div class="row">
ROW;
$counter = 0;
foreach($rows as $row) {
   // IF WE HAVE A MULTIPLE OF 3 AND COUNTER IS NOT 0
   // CLOSE OFF ROW AND OPEN NEW ROW
   if ($counter > 0 && $counter % 3 == 0) {
      echo <<< ROW
    </div>
    <div class="row">
ROW;
   }
   // SHOW THE COLUMN
   echo <<< COL
        <div class="col-md-4">
		{$row}
		</div>

COL;
    // BUMP THE COUNTER
    $counter++;
}
echo <<< ROW
    </div>
ROW;

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Timothy GoldenWeb DevAuthor Commented:
not quite what Marco Gasi suggested....
i was able to get a modified version to work

	$counter = 0;
while ($row = $resultAllInfo->fetch_assoc()) {
        $counter++;
        if ($counter == 1)
        {
               echo '<div class="row">';
               echo  '<div class="col-md-4"><p>' . $row['Description'] . '</p></div>';
        }
        else if ($counter == 2)
	{
		    echo  '<div class="col-md-4"><p>' . $row['Description'] . '</p></div>';
        }
        else if ($counter == 3)
        {
		   echo  '<div class="col-md-4"><p>' . $row['Description'] . '</p></div>';
              echo '</div>'; // closing row
              $counter = 0; //reset counter
        }
}

Open in new window

0
Marco GasiFreelancerCommented:
Modified: at a first look, it seems youìve just used actual names for array and database fields. Anyway, I find surprising the Julianìs version doesn't work: it's just more concise and I prefer it to mine :-)
0
Julian HansenCommented:
Thanks Marco,

Here is a working sample http://www.marcorpsa.com/ee/t861.php

Source attached.

The source uses a cut and paste of the code above with a slight modification to use a description key in the $row.
t861.php
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.