Jump to content

Displaying results horizontally


scronner

Recommended Posts

I'm realitivly new to PHP and was hoping somebody could help.

 

I have a mysql database that stores information about books. I am currently using the code below to query the database and extract the 3 most recent entries and showing them in a dynamic list:

 

$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 3");
$productCount = mysql_num_rows($sql);
if ($productCount > 0) { // ensure a book exists
while($row = mysql_fetch_array($sql)){
             $id = $row["id"];
		 $title = $row["title"];
		 $author = $row["author"];
		 $price = $row["price"];
		 $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
		 $dynamiclist .= //My table showing the products 			
}
} else {
$dynamicList = "There are currently no Books listed in this store";
}

 

This works well when showing the most recent books 1 below the other. However, I would like to show these products side by side, horizontally across the page.

 

Can somebody please point me in the right direction?

 

Many thanks

Link to comment
Share on other sites

The code you posted desn't actualy deal with the formating of the information on the page for display.  what you are looking for is your html table code(it will start with <table>) remove the </tr><tr> from the table code leaving a single <tr> at the start, and single </tr> at the end, this will present everything on a single line.

 

if you want, post your full code and we'll point out where to apply the changes.

Link to comment
Share on other sites

try this...

echo "<table><tr>";
while($row = mysql_fetch_array($sql)){
$id = $row['id'];
$title = $row['title'];
$author = $row['author'];
$price = $row['price'];
$date_added = strftime("%b %d, %Y", strtotime($row['date_added']));
echo  "<td>" . $title . "<br/>" . $author . "<br/>" . number_format($price, 2, '.', ',') . "<br />" . $date_added . "</td>";
}
echo "</tr></table>";

Link to comment
Share on other sites

Thanks Guys,

 

After your help and a little code bashing I discovered this works:

 

$dynamiclist = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 2");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
$dynamiclist .= '<table width="90%" align="center"><tr>';
while($row = mysql_fetch_array($sql)){
             $id = $row["id"];
		 $title = $row["title"];
		 $author = $row["author"];
		 $price = $row["price"];
		 $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
		 $dynamiclist  .= '<td> <img src="product_images/'. $id .'.jpg" width="300" height="300" alt="'. $title .'" /><br />
      			'. $title .'<br />
     			by '. $author .'<br />
      			£ '. $price .'
			</td>';
}
$dynamiclist .= '</tr></table>';
} else {

$dynamiclist = "There are currently no products listed in the store";
}

Link to comment
Share on other sites

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.