Jump to content


Photo

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


  • Please log in to reply
5 replies to this topic

#1 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 26 August 2006 - 10:54 PM

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:


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


Image 
name               Now the 4th item is on the next row.
stock


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:



Image 
name
stock

Image 
name
stock

Image 
name
stock

And here is my code so far:

<?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>';


?>



#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 26 August 2006 - 11:38 PM

here's a method using a table

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 27 August 2006 - 12:01 AM

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

}

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 =)

#4 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 27 August 2006 - 12:06 AM

Oh, I figured it out by manipulating your script around barand XD Thanks very much.

#5 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 28 August 2006 - 08:09 PM

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

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

I know this is probably way off but am i on the right lines?
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#6 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 28 August 2006 - 10:32 PM

That sounds like it would work...but I am not sure.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users