scronner Posted March 1, 2011 Share Posted March 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/229241-displaying-results-horizontally/ Share on other sites More sharing options...
Muddy_Funster Posted March 1, 2011 Share Posted March 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229241-displaying-results-horizontally/#findComment-1181205 Share on other sites More sharing options...
litebearer Posted March 1, 2011 Share Posted March 1, 2011 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/229241-displaying-results-horizontally/#findComment-1181222 Share on other sites More sharing options...
scronner Posted March 1, 2011 Author Share Posted March 1, 2011 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/229241-displaying-results-horizontally/#findComment-1181245 Share on other sites More sharing options...
Muddy_Funster Posted March 1, 2011 Share Posted March 1, 2011 You are welcome, glad you got it sorted out. (mark as solved now?) Quote Link to comment https://forums.phpfreaks.com/topic/229241-displaying-results-horizontally/#findComment-1181280 Share on other sites More sharing options...
crmamx Posted March 1, 2011 Share Posted March 1, 2011 You are welcome, glad you got it sorted out. (mark as solved now?) The solved button has disappeared. Quote Link to comment https://forums.phpfreaks.com/topic/229241-displaying-results-horizontally/#findComment-1181309 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.