Jump to content

Recommended Posts

It's only displaying the first result and not the others. What am I doing wrong?

I'm selecting the items the user occupies.

Then I'm selecting the name and image of that item from the item table.

 

echo "<table cellspacing=\"0\" class=\"news\" align=\"center\">";
echo "<tr>";
$sql = "SELECT * FROM useritems WHERE userid='".$_SESSION['userid']."' LIMIT $offset, $rowsperpage"; //selects all the users items to whoever is logged in
$result = mysqli_query($cxn,$sql) or die(mysqli_erro($cxn));
$imagecount = 0;
while ($row = mysqli_fetch_assoc($result)) //while there are still results
{
	extract($row);

	$quantity = $row['quantity'];
	$itemid = $row['itemid'];

	$sql = "SELECT * FROM items WHERE itemid='".$itemid."'"; //selecting the name and image of the item that is displaying
	$result = mysqli_query($cxn, $sql) or die(mysqli_error($cxn));
	$row = mysqli_fetch_assoc($result);

	$name = $row['name'];
	$image = $row['image'];

	if ($imagecount%5 == 0) //after 5 items have been listed, start a new line
	{
		echo "</tr>";
		echo "<tr>";
	}
}

echo "<td width=\"120px\" align=\"center\">";
echo "<img src=\"http://www.elvonica.com/".$image."\"><br>";
echo $name." (".$quantity.")";
echo "</td>";

$imagecount++; //after one item has been displayed, redo the loop if there are still results
}
echo "</table>";

Link to comment
https://forums.phpfreaks.com/topic/236025-while-loop-not-working/
Share on other sites

Why are you using extract($row) and then defining variables based upon the variable $row?!!!

 

Assuming $row contains an index named "quantity", the extract function will automatically create a variable called $quantity with the value associated with that index. You then redefine $quantity using $row['quantity']. That makes no sense.

 

Second, why are you running a query in your loop? Just do a join in the first query to get all the records you need at one time.

 

As to your particular issue, I suspect there is a problem with one of the two values you are using on the LIMIT clause. But, you don't show how those are defined, so I can't help you there. But, try echoing the query to the page to ensure it has the values you expect.

You are overwriting the variables you defined for your first query ($sql, $result and $row) when you go to run your second query:

		$sql = "SELECT * FROM items WHERE itemid='".$itemid."'"; //selecting the name and image of the item that is displaying
	$result = mysqli_query($cxn, $sql) or die(mysqli_error($cxn));
	$row = mysqli_fetch_assoc($result);

 

However performing a second query from the results of your first query is not recommended. This is an inefficient way of getting data from your database as it takes up server resources. What you can do is use an alternative query (called a join) which will grab the necessary data from both your useritems table and items table using just one query.

 

Here is a rewrite of your code that should produce the results you are after - but I did not test it as I don't have your database. Note, I created it so that you can separate the logic (PHP) from the output (HTML).

 

<?php

    //selects all the users items to whoever is logged in
    $sql = "SELECT useritems.quantity, items.name, items.image
            FROM useritems
            JOIN items USING(itemid)
            WHERE userid='{$_SESSION['userid']}'
            LIMIT {$offset}, {$rowsperpage}";
    $result = mysqli_query($cxn, $sql) or die(mysqli_erro($cxn));
    $imagecount = 0;
    $tableOutput = '';
    while ($row = mysqli_fetch_assoc($result)) //while there are still results
    {
        $imagecount++;
        $quantity = $row['quantity'];
        $name     = $row['name'];
        $image    = $row['image'];

        //Open new row
        if($imagecount%5==1)
        {
            $tableOutput .= "<tr>\n";
        }

        //Create TD
        $tableOutput .= "<td width=\"120px\" align=\"center\">";
        $tableOutput .= "<img src=\"http://www.elvonica.com/{$image}\"><br>";
        $tableOutput .= "{$name} ({$quantity})";
        $tableOutput .= "</td>\n";

        //Close current row
        if ($imagecount%5==0) //after 5 items have been listed, start a new line
        {
            $tableOutput .= "</tr>\n";
	}
}
    //Close last row  if needed
    if($imagecount%5!=0)
    {
        $tableOutput .= "</tr>\n";
    }

?>

<table cellspacing="0" class="news" align="center">
<?php echo $tableOutput; ?>
</table>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.