By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

Format mySQL data to stack in table

P: 1
Hey guys. I'm still learning PHP and have run into a formatting problem that I thought would be simple but am finding myself running around in circles trying to solve.

I'm calling a mySQL database with PHP and displaying the information in a table. I would like it to display the information so that each of the database row information is grouped together and displayed in columns side-by-side. Instead, all the information is listing horizontally and stacked on top of each other just as it appears in the database. So, right now it's displaying like this:

a info info info info
b info info info info
c info info info info

Does anyone know how to format the table so that it displays like this:

a | b | c
info info info
info info info

d | e | f
info info info
info info info


Here is the code I'm using:


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <?php
  4.  
  5. $db_host = 'localhost';
  6. $db_user = 'user_name';
  7. $db_pwd = 'password';
  8. $database = 'database_name';
  9.  
  10. if (!mysql_connect($db_host, $db_user, $db_pwd))
  11.     die("Can't connect to database");
  12.  
  13. if (!mysql_select_db($database))
  14.     die("Can't select database");
  15.  
  16. $query="SELECT * FROM products WHERE advertisercategory='briefs'";
  17. $result=mysql_query($query);
  18.  
  19. $num=mysql_numrows($result);
  20.  
  21. mysql_close();
  22.  
  23. ?>
  24.  
  25. <?
  26. $i=0;
  27. while ($i < $num) {
  28.  
  29. $image=mysql_result($result,$i,"imageurl");
  30. $namet=mysql_result($result,$i,"name");
  31. $sku=mysql_result($result,$i,"sku");
  32. $price=mysql_result($result,$i,"price");
  33. $category=mysql_result($result,$i,"advertisercategory");
  34. $updated=mysql_result($result,$i,"lastupdated");
  35. $affiliate=mysql_result($result,$i,"buyurl");
  36. ?>
  37.  
  38. <table>
  39. <tr>
  40. <td><a href="<? echo $affiliate; ?>"><img width="110" src="<? echo $image; ?>"></a></font></td>
  41. <td><? echo $first." ".$last; ?></font></td>
  42. <td><? echo $fax; ?></font></td>
  43. <td><? echo $email; ?></font></td>
  44. <td><a href="<? echo $affiliate; ?>">Buy Now</a></font></td>
  45. <td><? echo $web; ?></font></td>
  46. </tr>
  47.  
  48. <?
  49. $i++;
  50. }
  51.  
  52. echo "</table>";
  53. ?>
  54.  
  55.  
Apr 28 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.