Jump to content

Archived

This topic is now archived and is closed to further replies.

pocobueno1388

Displaying info from DB into table, then making rows. *SOLVED*

Recommended Posts

I am trying to creat a script for a store. On this page it will display all the items I have inserted in the database. The rows include the items name, the url for the image, how many are in stock, and the description. On the store page I want it to pull all the items information out and put it into a table. I want the table to make a <tr> for every 4 rows of the table.

This is an example of how I want the page setup:

[code]

Image        Image        Image        Image
name        name        name        name
stock        stock        stock          stock


Image
name              Now the 4th item is on the next row.
stock [/code]


Hopefully that helped a little. I won't ever have enough different items to make another page, so that won't be necesarry.

The code I have now makes a new line for every item, like so:


[code]

Image
name
stock

Image
name
stock

Image
name
stock[/code]

And here is my code so far:

[code]
<?php
include 'header.php';

$sql = mysql_query("SELECT * FROM items_store WHERE stock > 0");

print '<table border=1 width="80%" align="center" valign="center">';


while ($row = mysql_fetch_assoc($sql)){

echo "<td align='center'><img src='$row[image_url]'><br>";
echo "<b>$row[item_name]</b><br>";
echo "<i>$row[description]</i>";
echo "<h3><a href='store.php'>Buy</a></h3></td>";

}

print '</table>';


?>
[/code]

Share this post


Link to post
Share on other sites
here's a method using a table

[code]<?php
define ("NUMCOLS",4);

$res = mysql_query("SELECT col1, col2 FROM mytable");

$count = 0;
echo "<TABLE border=1>";
while (list($col1, $col2) = mysql_fetch_row($res)) {

    if ($count % NUMCOLS == 0) echo "<TR>\n";  # new row

    echo "<TD>$col1<br>$col2</TD>\n";
    $count++;

    if ($count % NUMCOLS == 0) echo "</TR>\n";  # end row
}

# end row if not already ended

if ($count % NUMCOLS != 0) {
  while ($count++ % NUMCOLS) echo "<td>&nbsp;</td>";
  echo "</TR>\n";
}
echo "</TABLE>";
?>[/code]

Share this post


Link to post
Share on other sites
That displays the name of the item and shows the picture...but I am not sure how to get it to display all the other information and in the order I want it. I would prefer if I could keep this part of the code:
[code]while ($row = mysql_fetch_assoc($sql)){

echo "<td align='center'><img src='$row[image_url]'><br>";
echo "<b>$row[item_name]</b><br>";
echo "<i>$row[description]</i>";
echo "<h3><a href='store.php'>Buy</a></h3></td>";

}[/code]

So if that is possible, that is how I would like it, unless you could get the information to display like that with your code. Otherwise your script would work great =)

Share this post


Link to post
Share on other sites
I was looking at the example given by Barand and was wandering if it it possible it use a folder full of images instead of pulling image names from a database

I thought of uding something like this but need to get it to work if possible

[code]$picid = 0;
....
....
echo "<TD><img src='images/image".$picid.".jpg'></TD>\n";
$picid = $picid + 1;[/code]

I know this is probably way off but am i on the right lines?

Share this post


Link to post
Share on other sites

×

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.