Rsmiley
Rsmiley

Reputation: 29

It won't loop in a foreach or while

I'm trying to make this take 6 times, and then roll over into another row. I'm sure there's a better way of going about this, and that's probably why mine doesn't work.

However, when I use the foreach() it displays nothing, and when I use the while() the page breaks completely. The headers don't send, and php_error doesn't catch it.

All the queries work just fine, it's the display that's causing issues. Perhaps you guys can help?

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;
        } 
    } 
}

Upvotes: 0

Views: 59

Answers (1)

Jeroen de Lau
Jeroen de Lau

Reputation: 640

I guess you are looking for something like this?

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>";
}

Upvotes: 1

Related Questions