Forum Moderators: coopster

Message Too Old, No Replies

Grouping data to display in table

Thought about using a multi-dimensional array

         

scots

7:04 pm on Sep 23, 2007 (gmt 0)

10+ Year Member



ok, I'm in need of some help as this is getting on my nerves.
It is probably the fault of my bad db table design.

I have included the output of the data below as I thought it would be best to put it in a multi-dimensional array.

Array
(

[0] => Array
(
[id] => 56
[date] => 2007-04-11
[customer] => 1
[invoice] => 1
[type] => P
[amount] => 423.64
[info] =>
)

[1] => Array
(
[id] => 55
[date] => 2007-04-14
[customer] => 1
[invoice] => 1
[type] => I
[amount] => 525.00
[info] =>
)

[2] => Array
(
[id] => 58
[date] => 2007-04-11
[customer] => 3
[invoice] => 5
[type] => I
[amount] => 225.45
[info] =>
)

[3] => Array
(
[id] => 57
[date] => 2007-05-11
[customer] => 1
[invoice] => 1
[type] => D
[amount] => 23.64
[info] =>
)

)

The problem is trying to display it in a table like:

<tr>
<td>Date</td>
<td>Customer</td>
<td>Invoice No</td>
<td>Amount</td>
<td>Paid</td>
<td>Monthly Total</td>
<td>Balance</td>
</tr>

Since I want to group the transactions of invoices (type of transactions are detailed in the 'type' variable, I=Invoice,P=Payment & D=Deduction) Deductions should just be deducted from the Invoice Amount for display purposes.

I also want to be able to group the invoices by date.

I've been at this for a while and would be most grateful if anyone can help find a way to do this.

cameraman

8:05 pm on Sep 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



foreach($outer_array as $rec_ary){
echo "<tr>\n";
foreach($rec_ary as $column)
echo "<td>$column</td>\n";
echo "</tr>\n";
}

The \n are for readability if you view the source.
The first thing you'll notice is that the columns are out of order from the way you've set up your headers. Now you could do this, which would replace that inner foreach (and of course the line immediately below it):
echo "<td>" . $rec_ary['date'] . "</td>\n";
echo "<td>" . $rec_ary['customer'] . "</td>\n";
.
.

But you could instead manipulate your query so that the first method will work. Instead of (I'm guessing):
SELECT * FROM sometable...

list the fields explicitly in the order you want to eventually display them:
SELECT date,customer,invoice, . . . FROM sometable

For the grouping, you can do something like this at the end of the sql statement:
GROUP BY type ORDER BY date

You'll need to play with that a little for the right syntax, and sometimes mysql is a little finicky about what you're doing in the select when you're trying to get it to group. Pay attention to the messages that mysql gives you, or you can even put the query statement into phpMyAdmin and fiddle with it there until both you and mysql are happy, then paste the statement back into your script.