...

View Full Version : Manipulating mysql output through PHP



djackson88
02-17-2009, 06:20 PM
Hello All,

I am trying to manipulate the way my mysql table data displays on my PHP page. Here is my setup:

Mysql table called orders

Fields:
* custnum
* fname
* lname
* item
* qty
* date

When a customer places the order an entry is made to the table for each item purchased so in my example, Customer A made 3 purchases so this would be in the table.

1001 First Name Last Name Shoes 1 2/17/2009
1001 First Name Last Name Socks 2 2/17/2009
1001 First Name Last Name Laces 2 2/17/2009

I am using PHP to display this order information for order history purposes. Right now I have a while loop that just prints out the table exactly as it is above into a html table in a browser that looks like this

<table>
<tr>
<td>1001</td><td>First Name</td><td>Last Name</td><td>Shoes</td><td>1</td><td>2/17/2009</td>
</tr>
<tr>
<td>1001</td><td>First Name</td><td>Last Name</td><td>Socks</td><td>2</td><td>2/17/2009</td>
</tr>
<tr>
<td>1001</td><td>First Name</td><td>Last Name</td><td>Laces</td><td>2</td><td>2/17/2009</td>
</tr>
</table>

Now for my question, is there a way that I can print out my table into a php/html table where instead of creating three seperate lines for the same customer it will only print one line that looks like this (this is assuming each customer will ALWAYS have three items purchased at the same time, no more, no less):

<table>
<tr>
<td>1001</td><td>First Name</td><td>Last Name</td><td>Shoes</td><td>1</td><td>Socks</td><td>2</td><td>Laces</td><td>2</td><td>2/17/2009</td>
</tr>
</table>

Fumigator
02-17-2009, 07:05 PM
Yes there is. But this is a PHP question, not a MySQL question.

Anyways...

First, isolate the portion of your HTML coding that repeats.

once
<table>
<tr>

once per customer
<td>1001</td><td>First Name</td><td>Last Name</td>

repeat up to three times per customer
<td>Shoes</td><td>1</td>
<td>Socks</td><td>2</td>
<td>Laces</td><td>2</td>

once per customer
<td>2/17/2009</td>
</tr>

once
</table>

Now build your PHP code looping structure around this.

HINT: As you fetch data from your tables, build an array instead of trying to create your HTML tags in your fetch loop.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum