2014-02-08 69 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