Loop through multi dimension array to show grid in PHP -


I have 3 tables, apples, hulls, and applesholds.

APL contains IDs, names, dates, hall has IDs, production enabled application is an included table and contains ID, AppleID, Hall_ID, status.

Not every solution is associated with every Apple. Those who are in a table to join with a situation (production, etc.) have been in the table

I need to display a report in a table / grid that has the following column headers: Apple Name, APLDAT, then the degenerated production name, in the form of remaining column titles. (If the Hull 7 is not in the result set, then it also does not get a column.

For the data I need to type the name of Apple name, APL, then the remaining column loop and fill in In the situation for the record in the table. If the Apple and the hull are not connected in the table, simply fill the cell with "NA".

I have tried a bunch of different ways Column headers dynamic for hiers in I can get the list, I think the data does not seem capable of filling the loop correctly.

  Sample data: APLS table ID: 1, name: x1-0000, Date: 1/1/2009 ID: 2, Name: BG-5480, Date: 2/22/2009 ID: 3, Name: HG-0000, Date: 2/27/2009 Hulls Table ID: 1, AP_ID: 1 , HLID: 1, Status: Deliver ID: 2, Apple_ID: 1, Production_name: Product ID: 2, Production_name: Produce ID: 3, Production_name: Product ID: 4, Prod Quote_name: Produced ApplauseHouse Table ID: 1, Hull_id: 3, Status: Ordered ID: 3, Apple_ID: 2, Hull_ID: 4, Status: Distributed  

I need a table to show it It is:

  APL | Date | Prada | Manufacturer | ProdD X1-0000 | 01/01/2009 | Distributed | | Order was given. NA BG-58080 | 02/22/2009 | NA | NA | Distributed  

Note that the column headers ignore ProdB because that record was not all on the joining table. Apart from this, it fills in the NA for the column which is on the joining table but there can be no cooperation on the table to join.

It's very confusing, I know.

You can get a list of hulls in your mind about a query like this:

  select h id (), from hulls h to pname to applying applying (h.id = hull_id)  

You (and possibly should) reply to this Now you can stop reading now and just use it that takes care of the column and find out how to put data in the table.

But once you have a list of kites that you care about, your program may have to write some evil SQL to create results for you. The code below assumes that your DB library gives an array of rows for your SQL query.

  $ hulls = query (select "h.id, hulls to pname h; aplshulls (h.id = hull_id)"); / * Should give a result like this: * $ hulls = array (array ('id' = & gt; 1, 'pname' => gt; proda '), * array (' id '=> 3, 'Pname' = & gt; prodc '), * array (' id '=> 4,' pname '=>' prodd ')); * / $ Sql ​​= "select the name, mdate"; Forex Currency ($ Hall $ as Line) {$ sql. = ", Ifnull ({$ row ['pname']}, 'NA') {$ row ['pname']}"; } $ Sql = "From APL"; Foreach ($ hulls $ as line) {$ sql. = "Join left (select apple_id, join hulls h as {$ row ['pname']}, join aplshulls at (h.id = hull_id) where pname = '{$ row [' pname '] } 'At {$ row [' pn \ ame ']} ({$ row [' pname ']}. Apl_id = apls.id) ";} $ Sql. =" Where in apls.id (Applied to Apple_ID Select "Forex" ($ $ sql); Foreign Currency ($ result as $ line) {Print "& lt; tr & gt;"; Foreign Currency (as the $ line $ value) {Print " & Lt; td & gt; $ value & lt; / td & gt; ";} Print" & lt; / tr & gt; \ n ";}  

Your database query methods Change the place to call with them.

The resulting sql is:

  select the date, ifnull (proda, 'NA') proda, ifnull ( Prodc, 'NA') are associated with prodc, ifnull (prodd, 'NA') apls (choose apl_id), from hulls to position as proda on aplshulls ah (h id = hull_id where pname = 'proda') (proda .apl_id = apls.id) on the left as proda (select apl_id), as prodc from hulls on h (h.id = hull_id) add aplshulls to ah where pname = 'prodc') on prodc (Prodc.apl_id = apls.id) join in the left (choose apl_id, position huls as prodd A. Add h aplshulls Ah (h.id = hull_id) where pname = 'prodd prodd .apl_id = on as') apls.id) where select different apl_id of apls.id (aplshulls);  

There is probably a better way of creating a query but this should work. This probably breaks if the number of halus is too large or if any included table is too big, if your product name is not legal in SQL, then you will need to do something else.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -