Jump to content
Sarah

Php & Mysql Printing question

Recommended Posts

Hello everyone.

 

I am stuck on a logic problem. I am trying to print out data from a database in a horizontal table format, not vertical, and I'm just not getting a way to do it!

 

For example, say theres a used car database. I have no problems using a while loop and printing the cars like this :

 

>Car #1
1999 Honda CR-V  
Stock# 123
Price 4999

Car #2
2000 Honda Civic
Stock #464
Price 5000

 

But how do I go about printing the cars in a horizontal format like this, with different record information in the same row?

>Car #1						Car #2
1999 Honda CR-V		 2000 Honda Civic
Stock 123					 Stock 464
Price 4999					Price 5000

 

I've tried using for loops, while loops, and arrays. But I can't figure out how to access the different fields in order to print side by side in a table! Am I missing something? Thanks in advance for your help.

Share this post


Link to post
Share on other sites

I think one trouble you are going to have is that if you get too many records from the DB, your table will get really wide, really fast.

 

I think that what you may need to do is to have one parent table row with a different subtable in each column. Then you'd let your <for each> command set up your master table columns with sub tables inside of them.

 

So your master table would look like:

><table>
<tr>

[here is where you put your FOR EACH code]
<td><table><tr><td>Car $i</td></tr><tr><td>$model</td></tr><tr><td>Stock $stock</td></tr><tr><td>Price: $price</td></tr></table></td>

[end your FOR EACH code here]
</tr>
</table>

But remember, you're going to get really wide pages unless you limit the results fetced from the database.

Share this post


Link to post
Share on other sites

Hi, thanks for the reply.

 

Hmm, so what you are saying is open the first table row,

then for each..

><td>
 <table>
  <tr>
 <td>Car $i</td>
  </tr>
  <tr>
 <td>$model</td>
</tr>
 <tr>
  <td>Stock $stock</td>
 </tr>
<tr>
 <td>Price: $price</td>
</tr>
  </table>
</td>

 

I see what you are saying, just keep adding <td>'s..

I know that it would get really wide, I would like to keep it at 2 cars per line.

 

I guess to fix that I would assume I would have to just write some code that increases a counter after every subtable is printed and then have a snippet that asks 'if the counter is less than the total number of cars in the select statement and if there has been two subtables already printed, then create a new row and add the subtable there.'

 

Do you think that would work?

Share this post


Link to post
Share on other sites

Please look at www.jo-danmotors.com for an example of how I would like this to print.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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...