Generating HTML tables with PHP, with special requirements
MatthewHSE
Status: Contributor
Joined: 20 Jul 2004
Posts: 122
Location: Central Illinois, typically glued to a computer screen
Reply Quote
I've got kind of a peculiar situation. There's a MySQL database of customer information - names and addresses. I need to be able to pull the records out of the database and format them into HTML tables, three customer records across and five down. In other words, I need to create a 15-cell HTML table with one customer record in each cell.

Sometimes there will be fewer than 15 records to be retrieved, sometimes many more, but almost never will the number actually be evenly-divisible by 15. This means I'm going to have to come up with a way to fill up a table with the right number of empty cells and/or rows.

For every 15 (or portion thereof) customer records that are retrieved from the database, I need a new HTML table (but it can be the same HTML page - no pagination is required).

So after counting the records that are to be retrieved, how can I put them into nicely-formatted HTML tables that meet that criteria? I think I'll be able to work up a decent system for getting the records into table rows three cells across by using nested while() loops, although my thoughts are a little hazy so far in that direction. Breaking up the tables after five rows, though, is getting a little more complex.

I'm not looking for a ready-made script that will do this - I want to write one myself just for the fun and learning of it. But I sure could use some ideas on where to start.
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4127
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
Just use loop counters, it's pretty easy.

First loop counter counts to 15, then starts new table, second loop counter, contained in the first, count 1 2 3. If loop counter 1 > 15, start new table. If loop counter 2< 3, add filler cells, 1 for counter == 2, 2 for counter == 1. I'd probably make it slightly more elegant, and just use a single spacer cell, with colspan derived from the second loop counter.

I just did a job something like this for a client, but far more complicated, that one required storing 4 mysql rows in variables, then listing them under a single category, all of this taken from the output as relevant.

But your case would be fairly simple, just count the output loops, and trigger behaviors based on that count.

For example, pseudocode:

start loop for count of total customer rows.

For count == 1, start table. For count number % 15 = 0, end table, start new table if count != total count. If count = total count, end table.

Since your total counts are not consistently divisible by 15, you have to tighten up the logic a bit, but it's not that hard, it's all going to be counters doing something, you just have to decide what that something is.

If you need, for layout purposes, the rows to always = 15, then it's counters again, when that data set ends, use counters, and add the required number of rows.

:: Code ::
SELECT DISTINCT item FROM client_table


May also be of use to you, to get the count for each category of client, or whatever you may need.

This stuff gets so specific though that you really can't speculate much on it, it's all about getting the exact format for your exact data, I'd post an examplified version of a very complicated type thing like that but it's too different from your requirement.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours