Fredrik Burmester
Fredrik Burmester

Reputation: 239

SQL Printing the table doesn't work

I'm having trouble printing a table. I know that my SQL query works but nothing appears on my website. I'm a bit new to PHP and SQL and now I'm stuck. ANY tips about my code would be much appreciated!

$setidquery = "SELECT
                inventory.quantity,
                inventory.itemid,
                inventory.colorid,

                parts.partname,
                sets.setid,
                inventory.itemtypeid
                FROM
                inventory
                join
                parts
                on inventory.itemid = parts.partid
                join
                colors
                on inventory.colorid = colors.colorid
                join
                sets
                on inventory.SetID = sets.SetID
                where
                  sets.setid = '$_COOKIE[setid]'
                order by
                partname asc
            limit 1000";

Here I'm trying to get the correct images.

echo "<table class=\"table\">";
  echo "<tr><th>Quantity:</th><th>Partname:</th><th>ItemID:</th><th>SetID</th><th>Image:</th></tr>";

  while($row = mysqli_fetch_array($result)){

      $prefix = "http://www.itn.liu.se/~stegu76/img.bricklink.com/";

      $Quantity = $row['Quantity'];
      $ItemID = $row['ItemID'];
      $ColorID = $row['ColorID'];


      $ItemtypeID = $row['ItemtypeID'];

      $imagesearch = mysqli_query($conn, "SELECT * FROM `images` WHERE ItemTypeID = '$ItemtypeID' AND ItemID = '$ItemID' AND ColorID = '$ColorID' ");

      $imageinfo = mysqli_fetch_array($imagesearch);

      if($imageinfo['has_jpg']) {
        $filename = "$ItemtypeID/$ColorID/$ItemID.jpg";
      } else if($imageinfo['has_gif']) {
        $filename = "$ItemtypeID/$ColorID/$ItemID.gif";
      } else {
        $filename = "noimage_small.png";
      }

      $SetID = $row['SetID'];
      $Partname = $row['Partname'];
      echo "<tr>
              <td>$Quantity</td>
              <td>$Partname</td>
              <td>$ItemID</td>
              <td>$SetID</td>
              <td><img src=\"$prefix$filename\" alt=\"Part $ItemID\"/></td>
            </tr>";
  }
      echo "</table>";

And then print everything.

On the website it looks like this:

Part of the table

Upvotes: 1

Views: 227

Answers (1)

Pupil
Pupil

Reputation: 23978

In PHP, variables and array keys are case sensitive.

You are getting itemid from SQL and accessing it with ItemID.

So, $row['ItemID'] is different than $item['itemid']

That is why you are not getting images displayed.

So, the correct the following:

$Quantity = $row['Quantity'];
$ItemID = $row['ItemID'];
$ColorID = $row['ColorID'];

To:

$Quantity = $row['quantity'];
$ItemID = $row['itemid'];
$ColorID = $row['colorid'];

Upvotes: 5

Related Questions