mysql 循环中 i,在while循环中循环mysqli_fetch_array()

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.='

'.%24thumb.'
';

$count = $count + 1;

};

The result:

img1.jpg
img2.jpg
img3.jpg
img4.jpg
img5.jpg
img6.jpg
img7.jpg
img8.jpg

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.='

'.%24thumb.'
';

$count++;

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值