2014-02-08 102 views
0

我試圖使這需要6次,然後轉到另一行。我確信有更好的方法來解決這個問題,這可能是我的原因。它不會在foreach或while循環

但是,當我使用foreach()時,它什麼也不顯示,而當我使用while()時,頁面完全斷開。標題不發送,並且php_error不能捕捉它。

所有查詢都可以正常工作,這是導致問題的顯示。 也許你們可以幫忙?

public static function DisplayInv($user) { 
    $user = users::lookup($user); 
    $sql = mysql_query("SELECT * from `inventory` where `userid`='{$user['id']}'"); 
    $limit = 6; 
    $count = 0; 
    $fetch = mysql_fetch_array($sql) or die('Error: '.mysql_error()); 
    while($count <= 7) { 
     print "<div class='row-fluid show-grid'>"; 

     foreach($fetch as $items) { 
      $getItem = self::ItemInfo($items['itemid']); 
      print "<span class='span2'><b>{$getItem['name']}</b><br />{$getItem['description']}<br /><b>Power: {$getItem['power']}</b></span>"; 
      $count++; 
     } 
     /* while($items = mysql_fetch_array($sql)) { 
      $getItem = self::ItemInfo($items['itemid']); 
      print "<span class='span2'><b>{$getItem['name']}</b><br />{$getItem['description']}<br /><b>Power: {$getItem['power']}</b></span>"; 
      $count++; 
     }*/ 
     print "</div>"; 
     if($count == 6) { 
      $count = 0; 
     } 
    } 
} 
+0

** $計數++; **使用此的foreach –

+0

之外@ suresh.g - 這將創建一個無限循環,如果它是在while($計數<7),因爲它復位爲0,在6 – Rsmiley

回答

1

我想你正在尋找類似的東西?

public static function DisplayInv($user) { 
    $user = users::lookup($user); 
    $sql = mysql_query("SELECT * from `inventory` where `userid`='{$user['id']}'"); 
    $limit = 6; 
    $count = 0; 

    print "<div class='row-fluid show-grid'>"; 
    while($items = mysql_fetch_array($sql)) { 
     $getItem = self::ItemInfo($items['itemid']); 
     print "<span class='span2'><b>{$getItem['name']}</b><br />{$getItem['description']}<br /><b>Power: {$getItem['power']}</b></span>"; 

     if($count == $limit){ 
      print "</div><div class='row-fluid show-grid'>"; 
      $count = 0; 
     }else{ 
      $count++; 
     } 
    } 
    print "</div>"; 
} 
+0

這工作完美,謝謝! – Rsmiley