Re: Reports generator

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jan 27, 2010 at 08:20:21PM -0800, Allen McCabe wrote:

> I actually started on a report class yesterday, and I have a few questions,
> but first some details:
> 
> - Reports will be on user orders (ticket reservations).
> - Need to be able to build a large variety of reports based on existing data
>  + Orders by a specific user
>  + Orders for a specific product (event)
>  + Orders by a user sub-group (organization)
>  + Orders by a user super-group (school district)
> - Reports need data from a primary table (orders) and several secondary
> tables (users, order_lineitems, etc.)
> 
> Now, I started to approach this with a class that builds an HTML table with
> the data as the end product, based upon the parameters I supply.
> 
> There shall be a table_header property which will be an array of column
> names, a rows property which will be a bi-dimensional array which will
> contain the data for each row.
> 
> I want to have methods like the following:
> 
> <?php
> 
> $Report->createNew('orders', 35); // STARTS AN ORDER USING `orders` TABLE -
> SHOW ID 35
> $Report->addColumn('contact'); // USERNAME - `users` TABLE
> $Report->addColumn('phone'); // USER'S PHONE NUMBER - `users` TABLE
> $Report->addColumn('quantity'); // TICKETS REQUESTED - `order_lineitems`
> TABLE
> 
> // SAVE OBJECT TO `reports` TABLE
> $report = serialize($Report);
> 
> $success = mysql_query('INSERT INTO `reports` (`data`) VALUES (\'' . $report
> . '\') ;');
> 
> if ($success) { $Notify->addtoQ('Report succesfully saved.', 'confirm'); }
> else { $Notify->addtoQ('There was en error saving the report.', 'error'); }
> 
> ?>
> 
> I was having a tough time wrapping my head around how to make the report
> class less specific and more flexible. For example, I have the user's
> user_id already stored in the `orders` table (of course, foreign key), but I
> want to display their username (or firstname, lastname pair), which would
> require another call to the `users` table, so I had a $queries property,
> which would be an array of queries to execute, but then I couldn't figure
> out how to handle each one, because each is handled uniquely.

You don't have to make a separate query; you can do a join. The problem
is how to handle it in a class/method way.

Honestly, what you're trying to do is very tricky. My suggestion is that
you do iterative development on this (I'm sure there's an erudite
programming student name for this which I don't recall). For example,
rather than doing addColumn() methods, use a rowQuery() method, and feed
that a query which will work for each row. If you like, you can allow
for "place markers" for variables, like '%ordernum%'. When you invoke
the generate() method (or whatever) have it parse the query for those
place markers, and substitute the values you supply. Then feed the query
to MySQL and capture the results in a 2D array.

Now write code which will iterate through your rows and columns and
insert them into HTML tables.

If you like, you can write a child class for each report, which inherits
from a common parent reports class. At this point, I'm not sure how much
use that would be, but you could do it.

Once you've done this and gotten it working for all your reports, you
then will have a better idea of what exact kinds of problems you're
going to run into. Then start on version 2.0, where you use that
information to build a better class with addColumn() methods, etc.

You might also consider snagging a report generation class (or somesuch)
from a place like phpclasses.org and seeing how *they* handle this.
Adapt the ideas to your needs.

I'm sorry if this doesn't help. This is just how I'd do it. The kind of
problems you're trying to solve are too abstract for my comfort, and I'd
want to get something working first which highlights the difficulties
before I tackle a full-on "pretty" OO solution.

(The other reason I'd do it this way is that I used to work for a
company that sold a report-writer coded in C. I never realized the
complexities of reports until I'd worked with that product for a while.
Subtotal levels, grand total levels, headers, footers, column labels,
gathered from a variety of COBOL, C, BASIC and other databases. Ugh.)

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux