Martin Newman Martin Newman - 4 months ago 12
HTML Question

While cycle result to three columns

I have following HTML code and I need make results from db, but I don't know how. There are three static divs. I don't know how to end static div and how to recognize columns. Can you help me please? Please see structure of html code with numbers of results. Thank for help.

<div class="static-div">
<div class="first-div">1</div>
<div class="second-div">4</div>
<div class="third-div">7</div>

<div class="first-div">10</div>
<div class="second-div">13</div>
<div class="third-div">16</div>

<div class="first-div">19</div>
<div class="second-div">22</div>
<div class="third-div">25</div>
</div>

<div class="static-div">
<div class="first-div">2</div>
<div class="second-div">5</div>
<div class="third-div">8</div>

<div class="first-div">11</div>
<div class="second-div">14</div>
<div class="third-div">17</div>

<div class="first-div">20</div>
<div class="second-div">23</div>
<div class="third-div">26</div>
</div>

<div class="static-div">
<div class="first-div">3</div>
<div class="second-div">6</div>
<div class="third-div">9</div>

<div class="first-div">12</div>
<div class="second-div">15</div>
<div class="third-div">18</div>

<div class="first-div">21</div>
<div class="second-div">24</div>
<div class="third-div">27</div>
</div>

Answer

You can achieve this output by using Arrays.

we will create three arrays for given three static divs.

try the following code.

The variable $i is not for loop iteration so if you are using $i for iteration please replace the following $i with other name.

$i=1;
$first_arr=array();
$sec_arr=array();
$third_arr=array();
while(database loop condition)
{
     if($i==1)
     {
        $val=' <div class="first-div">'.$row['column'].'</div>';//$val is a variable from database query result
        array_push($first_arr,$val);
        $i++;
     }
    elseif($i==2)
     {
        $val=' <div class="second-div">'.$row['column'].'</div>';//$val is a variable from database query result
        array_push($sec_arr,$val);
        $i++;
     }
     elseif($i==3)
     {
        $val=' <div class="second-div">'.$row['column'].'</div>';//$val is a variable from database query result
        array_push($sec_arr,$val);
        $i=1;
     }
}
$first_div=implode('',$first_arr);
$sec_div=implode('',$sec_arr);
$third_div=implode('',$third_arr);
echo '<div class="static-div">'.$first_div.'</div>
<div class="static-div">'.$sec_div.'</div>
<di
Comments