Marcos Ortegui Marcos Ortegui - 1 month ago 11
PHP Question

Change value of div with while

I need put this divs in this order:

<div class="col-md-9"> 1 box </div>
<div class="col-md-3"> 2 box </div>
<div class="col-md-3"> 3 box </div>
<div class="col-md-9"> 4 box </div>
<div class="col-md-9"> 5 box </div>
<div class="col-md-3"> 6 box </div>
<div class="col-md-3"> 7 box </div>
<div class="col-md-9"> 8 box </div>


An continue in this order. I try make it with a while but I can't find an algoritm that put me the divs in this order.

This is the code that I try but don't work:

$count = 0;

while($items = mysql_fetch_array($consult))
{
if($count % 2 == 0)
{
echo '<div class"col-md-9"> '.$items['title'].' </div>';
}
else
{
echo '<div class="col-md-3"> '.$items['title'].' </div>';
}
}


This show me:

<div class="col-md-9"> value </div>
<div class="col-md-3"> value </div>
<div class="col-md-9"> value </div>
<div class="col-md-3"> value </div>


Any idea?

Regards

Answer

1st: don't use MYSQL_* commands, they are deprecated and unsafe!

2nd: This should do. Notice: I did not test it, just wrote an quick example:

$count = 1;
$md = 9;

while ($items = mysql_fetch_array($consult)) {
    $count++;
    echo '<div class="col-md-' . $md . '"> ' . $items['title'] . ' </div>';

    if ($count > 1) {
        $md = $md == 3 ? 9 : 3;
        $count = 0;
    }
}