2013-02-21 188 views
1

内我有下面工作代码来从数据库mysqli_fetch_array()图像和所述图像加载到layout grid结构Jquery的移动1.2.0。循环while循环mysqli_fetch_array()

Jquery Mobile的layout grid结构使用built-in styled grid/blocks限制为五列,ui-block-a高达ui-block-e。因此,我应该坚持JQM风格。

注:我使用四层的结构,ui-block-aui-block-d

为了八(8)的图像加载到块,并让它们正确的风格,我必须使用这些类ui-block-a, -b, -c, -d用于第一一排四个图像和四个图像的第二排相同的类。

我用foreach (array(a, b, c, d, a, b, c, d) as $i)里面的while loop但我得到了重复的结果。

该代码工作正常,但我想知道是否有其他的可能性,通过删除所有IF-statements实现所需的结构。

代码:

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.='<div class="ui-block-'.$i.'"><img src="'.$thumb.'"></div>'; 

    $count = $count + 1; 
}; 

结果:

<div class="ui-grid-c"> 
    <!-- First row --> 
    <div class="ui-block-a"><img src="img1.jpg"></div> 
    <div class="ui-block-b"><img src="img2.jpg"></div> 
    <div class="ui-block-c"><img src="img3.jpg"></div> 
    <div class="ui-block-d"><img src="img4.jpg"></div> 
    <!-- Second row --> 
    <div class="ui-block-a"><img src="img5.jpg"></div> 
    <div class="ui-block-b"><img src="img6.jpg"></div> 
    <div class="ui-block-c"><img src="img7.jpg"></div> 
    <div class="ui-block-d"><img src="img8.jpg"></div> 
</div> 

预先感谢您!

+0

嗨可能会迟到我的答案,但想说感谢您的JQM帮助! – KevInSol 2013-02-28 15:11:44

+0

我喜欢它,我会用它来生成带有滑动导航的动态照片库;) – Omar 2013-02-28 15:14:09

回答

3

让我们给它一个试试吧!

$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.='<div class="ui-block-'.$i.'"><img src="'.$thumb.'"></div>'; 

    $count++; 
}; 
+0

对不起,不工作.. – Omar 2013-02-21 15:15:48

+0

忘记了'8' case/break,并引用数组元素。否则看起来不错。 – 2013-02-21 15:16:01

+0

它不经过'while循环'。 '$ count'回显结果'0'。 – Omar 2013-02-21 15:27:27

1

尝试

$count = 0; 
while ($row = mysqli_fetch_array($query) && $count < 8) { 
    $img = $row["fn"]; 
    if (empty($img)) { break; } 
    $thumb = 'images/th_'.$img; 

    $i = chr(97 + ($count%4)); // char a + 0, 1, 2 or 3 

    $ths.='<div class="ui-block-'.$i.'"><img src="'.$thumb.'"></div>'; 

    $count++; 
}; 

PHP函数CHR:http://php.net/manual/en/function.chr.php

ASCII码为 “一”:97

+0

不能正常工作。它不通过'while循环'。 '$ count'回显结果'0'。 – Omar 2013-02-21 15:27:05

+0

如果它不通过循环,那么mysqli_fetch_array($ query)返回false,因为当$ count为0时,显然是<8.尝试在SQL客户端上执行查询以确保其工作并返回结果。 – cernunnos 2013-02-21 17:18:02

+0

我感谢您的宝贵意见。我得到它修复:) – Omar 2013-02-21 17:40:27

1

这样用MOD来确定我们是在什么样的列其他的答案,但对于任何数量的图片的工作,也填补了空白单元格应NUM PIC的不能使用用户的4倍。输入的,而不是针对该演示,广告/ b的结果看http://solomon.ie/so/gallery.php?pics=6 - 改变图片= 6到任何数量

<? 
$num_pics = 0; 
$num_pics = $_GET['pics']; 


// store HTML/JQM fragments 
$blank_picture_row = "<div class='ui-block-a'>%pic_1%</div>\n"; 
$blank_picture_row .= "<div class='ui-block-b'>%pic_2%</div>\n"; 
$blank_picture_row .= "<div class='ui-block-c'>%pic_3%</div>\n"; 
$blank_picture_row .= "<div class='ui-block-d'>%pic_4%</div>\n\n"; 

//start HTML 
$out ="<div class='ui-grid-c'>\n\n"; 



for ($pic_index=0; $pic_index < $num_pics; $pic_index++) 
    { 
    $thumb_pic = "PiC# $pic_index";// create image tag - text for demo 

    if ($pic_index % 4 == 0) // mod result will be zero for start of each new row of 4 
     $picture_row = $blank_picture_row; 

    $position_in_row = ($pic_index % 4) + 1; 
    $pic_place_holder = "%pic_" . $position_in_row . "%"; 

    $picture_row = str_replace($pic_place_holder, $thumb_pic, $picture_row); 

    if ($pic_index == ($num_pics - 1)) // last pic, blank any positions that are not needed 
     { 
     $position_in_row = $pic_index % 4; 

     switch ($position_in_row) 
      { 
      case 0: // last pic was in column 1 - using a space, but could also put in a dummy pic 
        $picture_row = str_replace("%pic_2%", "&nbsp;", $picture_row); 
      case 1: // last pic was in column 2 
        $picture_row = str_replace("%pic_3%", "&nbsp;", $picture_row); 
      case 2: // last pic was in column 3 
        $picture_row = str_replace("%pic_4%", "&nbsp;", $picture_row); 
      case 3: // last pic is in column 4, no action required 
       break; 
      } 
     } // END if if ($pic_index == ($num_pics - 1)) 




    if ($pic_index % 4 == 3 || $pic_index == ($num_pics - 1)) 
     {// last pic in this row or last pic in total - add this row to output 
     $out .= $picture_row; 
     } 

    } // END for ($pic_index=$start_pic; $pic_index < $finish_pic; $pic_index++) 


$out .= "</div>\n\n\n"; 
$out=htmlspecialchars($out); 
$out=nl2br($out); 

print "$out\n"; 
?> 
+0

Intersting,感谢分享。 – Omar 2013-02-28 15:12:09

+0

不客气! – KevInSol 2013-02-28 16:58:17