I have the below working code to fetch images from database mysqli_fetch_array() and load the images into layout grid structure in Jquery Mobile 1.2.0.
The layout grid structure of Jquery Mobile uses built-in styled grid/blocks limited to five columns, ui-block-a up to ui-block-e. Hence, I am ought to stick to JQM styles.
NOTE: I am using four columns structure, ui-block-a to ui-block-d
In order to load eight (8) images into the blocks and have them styled correctly, I have to use these classes ui-block-a, -b, -c, -d for the first row of four images and the same classes for the second row of four images.
I used foreach (array(a, b, c, d, a, b, c, d) as $i) inside the while loop but I got duplicated results.
The code is working fine, but I was wondering if there are other possibilities to achieve the desired structure by removing all IF-statements.
The code:
while ($row = mysqli_fetch_array($query)) {
$img = $row["fn"];
if (empty($img)) { break; }
$thumb = 'images/th_'.$img;
if ($count == 8) { break; } // limited to 8 images/results
if ($count == 0) $i = 'a'; //first 4 imgs row classes
if ($count == 1) $i = 'b';
if ($count == 2) $i = 'c';
if ($count == 3) $i = 'd';
if ($count == 4) $i = 'a'; //second 4 imgs row classes
if ($count == 5) $i = 'b';
if ($count == 6) $i = 'c';
if ($count == 7) $i = 'd';
$ths.='
$count = $count + 1;
};
The result:
Thank you in advance!
解决方案
let's give it a try!
$count=0;
$arr=array('a', 'b', 'c', 'd');
while ($row = mysqli_fetch_array($query)) {
$img = $row["fn"];
if (empty($img)) { break; }
$thumb = 'images/th_'.$img;
if ($count == 8) { break; }
$i=$arr[$count%4];
$ths.='
$count++;
};