473,467 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using an Object to fill in a template

Claus Mygind
571 Contributor
I am making a cross-tab report where essentially the data has to be turned sideways (rows to columns). My approach was to create a blank template object, cycle through the data and fill in the cells of the printed report. Making page breaks when the returned query data exceeded the size of the page template.

For the fixed header information I have no trouble but when I try to create an array in the object to hold the variable number of column data, I cannot figure out how to reference / fill in the template.

How do I reference the array I created in the pObj object? see code below.

The following code illustrates my feeble attempt to do this. Ok this example is actually trying to use an array to store all the stuff. But I did try to use an object also.

Expand|Select|Wrap|Line Numbers
  1. function makePageObject($result, $row){
  2.  
  3.     global $cPageCnt, $cRecCnt,$cBoring;
  4.     $pObj = array();
  5.  
  6.     $pObj = array(
  7.         /*
  8.         ------------------------------------------
  9.         page header info for specific boring
  10.         found in each row but only used on header
  11.         ------------------------------------------
  12.         */
  13.         $jobId    = $row["jobid"],
  14.         $boring   = $row["boring"],
  15.  
  16.         /*
  17.         ------------------------------------------
  18.         data section of page is a 11 X 22 table.
  19.         data can be more, less or equal to size of table
  20.  
  21.         so I create an array with blanks
  22.         initialize array with blanks
  23.         ------------------------------------------
  24.         */
  25.         $colArray = array_fill(0, 22, array_fill(0, 10, " "))
  26.     );
  27.         //detail sample info for boring
  28.         while ($row = $result->fetch_array() and $cBoring == $row["boring"] and $cRecCnt < 11 ) 
  29.         {
  30.             /*
  31.             --------------------------------
  32.             build one column of sample data 
  33.             for each boring up to 10 samples 
  34.             per page
  35.  
  36.             How do I reference the array I
  37.             created in the pObj object?
  38.             --------------------------------
  39.             */
  40.             $colArray[0][$cRecCnt]  = $row["boring"];
  41.             $colArray[1][$cRecCnt]  = $row["sample"];
  42.             $colArray[2][$cRecCnt]  = $row["depth"]; 
  43.             $colArray[3][$cRecCnt]  = $row["blows"];
  44.             $colArray[4][$cRecCnt]  = $row["sampleType"];
  45.             $colArray[5][$cRecCnt]  = $row["contNumb"];
  46.             $colArray[6][$cRecCnt]  = $row['WWofSC'];
  47.             $colArray[7][$cRecCnt]  = $row['DWofSC'];
  48.             $colArray[8][$cRecCnt]  = $row['contWeight'];
  49.             $colArray[9][$cRecCnt]  = $row['moisture'];
  50.             $colArray[10][$cRecCnt] = $row['sampleInAir'];
  51.             $colArray[11][$cRecCnt] = $row['sampleInWater'];
  52.             $colArray[12][$cRecCnt] = $row['water'];
  53.             $colArray[13][$cRecCnt] = $row['dryUnit'];
  54.             $colArray[14][$cRecCnt] = $row['dimDryUnit'];
  55.             $colArray[15][$cRecCnt] = $row['dimWeight'];
  56.             $colArray[16][$cRecCnt] = $row["dimDiameter"];
  57.             $colArray[17][$cRecCnt] = $row["qu"];
  58.             $colArray[18][$cRecCnt] = $row["percentFail"];
  59.             $colArray[19][$cRecCnt] = $row["qp"];
  60.             $colArray[20][$cRecCnt] = $row["tv"];
  61.             $colArray[21][$cRecCnt] = $row["soilDesc"];
  62.             //advance
  63.             $cRecCnt ++;
  64.         }
  65.  
  66.     return $pObj;
  67. }
  68.  
  69.  
Jul 2 '12 #1

✓ answered by Claus Mygind

The correct response in this case was to learn how to create an object from a class

By defining a class with properties and methods, I was able to create a page object to fill in data.

The object contained several properties one of which was an array of arrays.

I initialized the array with a nested array_fill( array_fill()), that gave me a two dimensional array to transpose my data from query rows to table columns.

Then I created 3 methods (functions) to build and print may page object.

The first method was a constructor which I used to store my header information as well as create the table array.

The second method was to load data into the array until I needed a page break or I reached the end of the data

The last method I used to stream out each page as it was complete.

Here is a sample cross-tab report that can easily be modified for any type of two dimensional data.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <?php
  3. /* soilsLabPrintable.php - 
  4.     Author:    Claus Mygind
  5.  
  6.     Date:    6/25/2012 - php
  7.  
  8.     Purpose: The purpose of this program is to create a multi-page cross tab report
  9.  
  10.     It uses one embeded class to create page objects, which can be completed
  11.     and then printed.
  12.  
  13. */
  14. try 
  15. {
  16.  
  17.     /*open connection to database*/
  18.     include(my includes);
  19.  
  20.     .... my query section .....
  21.  
  22.     if ( $result = $db->query($sql) )
  23.     {
  24.         if ( $result->num_rows > 0)
  25.         {
  26. ?>
  27.             <html>
  28.                 <head>
  29.                     <title>Printable cross tab report</title>
  30.                 </head>
  31.  
  32.                 <body>
  33.                     <form>
  34.  
  35. <?php
  36.                     $cPageCnt = 1;
  37.                     $cRecCnt = 0;
  38.                     $cBoring = '';
  39.  
  40.                     /*
  41.                     here I loop through the query result,
  42.                     a page object is create if none exists
  43.                     a page is streamed out when the data
  44.                     calls for a page break or the end of the data
  45.                     */
  46.  
  47.                     while ($row = $result->fetch_array()) 
  48.                     {
  49.  
  50.                         $cRecCnt ++;
  51.  
  52.                         if (!isset($pObj)){
  53.                             //set boring flag - used to flag a page break
  54.                             $cBoring = $row["boring"];
  55.  
  56.                             //create page object if not exists
  57.                             $pObj = new pageObject($row, $cPageCnt);
  58.                             $pObj->loadRow($row, $cRecCnt);
  59.  
  60.  
  61.                         }else if($cBoring != $row["boring"] or $cRecCnt > 10){
  62.  
  63.                             //page breaks are created when there are more than
  64.                             //10 samples or the next boring number is encountered.
  65.  
  66.                             //print page object if new boring or record count has exceeded page
  67.                             $pObj->printPage();
  68.  
  69.                             //insert page break
  70.                             echo '<span class="newPage"></span>';
  71.  
  72.                             //re-initilize control variables
  73.  
  74.                             //reset record counter
  75.                             $cRecCnt = 1;
  76.  
  77.                             //reset boring flag
  78.                             $cBoring = $row["boring"];
  79.  
  80.                             //destroy page object
  81.                             unset($pObj);
  82.  
  83.                             //advace page count
  84.                             $cPageCnt ++;
  85.  
  86.                             //create new page object
  87.                             $pObj = new pageObject($row, $cPageCnt);
  88.                             $pObj->loadRow($row, $cRecCnt);
  89.  
  90.                         }else{
  91.                             //add row data to page object
  92.                             $pObj->loadRow($row, $cRecCnt);
  93.                         }
  94.                     }
  95.  
  96.                     if (isset($pObj)){
  97.                         //some data is left to print
  98.                         $pObj->printPage();
  99.  
  100.                         //destroy page object
  101.                         unset($pObj);
  102.                     }
  103.  
  104. ?>
  105.                     </form>
  106.                 </body>
  107.             </html>
  108. <?php
  109.         }else{
  110.             throw new Exception("No records found!");
  111.         }
  112.  
  113.     }else{
  114.         throw new Exception("Query failed.");
  115.     }
  116. }
  117.  
  118. catch (exception $e) 
  119. {
  120.     echo 'An error occured : ' .$e->getMessage();
  121. }
  122.  
  123. class pageObject
  124. {
  125.     //properties
  126.     public $jobId;
  127.     public $boring;
  128.     public $date;
  129.     public $page;
  130.     public $project;
  131.     public $client;
  132.     public $engineer;
  133.     public $webUser;
  134.  
  135.     //detail sample infor for boring
  136.     public $colArray;
  137.  
  138.     //methods
  139.     public function __construct( $row, $cPageCnt ) {
  140.         /*
  141.         this method is called when a new page object is created
  142.         */
  143.  
  144.         //page header info for specific boring
  145.         $this->jobId    = $row["jobid"];
  146.         $this->boring    = $row["boring"];
  147.         $this->date        = $row["sampleDate"];
  148.         $this->page        = $cPageCnt;
  149.         $this->project    = $row['prjctname'];
  150.  
  151.         /*
  152.         detail information is stored in a cross-tab table 
  153.         where the query rowset is transposed into columns
  154.  
  155.         the table is inintialized with the required number
  156.         of cells. each cell is seeded with blank html tag
  157.         */
  158.         $this->colArray = array_fill(0, 22, array_fill(0, 11, "&nbsp;"));
  159.         /*
  160.         -------------------
  161.         fill in row headers 
  162.         -------------------
  163.         */
  164.         $this->colArray[0][0]  = "Boring";                                            
  165.         $this->colArray[1][0]  = "Sample #";                                          
  166.         $this->colArray[2][0]  = "Depth in Feet";                                     
  167.         $this->colArray[3][0]  = "No. of Blows";                                      
  168.         $this->colArray[4][0]  = "Type";                                              
  169.         $this->colArray[5][0]  = "Container Number";                                  
  170.         $this->colArray[6][0]  = "Wet weight of<br/>Sample + Container";              
  171.         $this->colArray[7][0]  = "Dry weight of<br/>Sample + Container";              
  172.         $this->colArray[8][0]  = "Weight of container";"Percent moisture";            
  173.         $this->colArray[9][0]  = "Percent moisture";                       
  174.         $this->colArray[10][0] = "Weight of sample in air";                           
  175.         $this->colArray[11][0] = "Weight of sample in water";                         
  176.         $this->colArray[12][0] = "Weight of water";                                   
  177.         $this->colArray[13][0] = "Disp. Dry unit weight in<br/>pounds per cubic foot";
  178.         $this->colArray[14][0] = "Dim. Dry unit weight in<br/>pounds per cubic foot"; 
  179.         $this->colArray[15][0] = "Dry unit scale weight";                             
  180.         $this->colArray[16][0] = "Dimension<br/>(Diameter * Length)";                  
  181.         $this->colArray[17][0] = "Qu (TSF)";                                          
  182.         $this->colArray[18][0] = "Qu % failure";                                      
  183.         $this->colArray[19][0] = "Qp (TSF)";                                          
  184.         $this->colArray[20][0] = "Tv";                                                
  185.         $this->colArray[21][0] = "Soil description"; 
  186.  
  187.     }                                
  188.  
  189.     public function loadRow($row, $cRecCnt){
  190.         /*
  191.         --------------------------------
  192.         fill in columns with sample data
  193.         --------------------------------
  194.         */
  195.  
  196.         $this->colArray[0][$cRecCnt]  = ( empty($row["boring"] ))        ?"&nbsp;" :$row["boring"];
  197.         $this->colArray[1][$cRecCnt]  = ( empty($row["sample"] ))        ?"&nbsp;" :$row["sample"];
  198.         $this->colArray[2][$cRecCnt]  = ( empty($row["depth"] ))        ?"&nbsp;" :$row["depth"]; 
  199.         $this->colArray[3][$cRecCnt]  = ( empty($row["blows"] ))        ?"&nbsp;" :$row["blows"];
  200.         $this->colArray[4][$cRecCnt]  = ( empty($row["sampleType"] ))    ?"&nbsp;" :$row["sampleType"];
  201.         $this->colArray[5][$cRecCnt]  = ( empty($row["contNumb"] ))        ?"&nbsp;" :$row["contNumb"];
  202.         $this->colArray[6][$cRecCnt]  = ( $row['WWofSC'] == 0)            ?"&nbsp;" :$row['WWofSC'];
  203.         $this->colArray[7][$cRecCnt]  = ( $row['DWofSC'] == 0)            ?"&nbsp;" :$row['DWofSC'];
  204.         $this->colArray[8][$cRecCnt]  = ( $row['contWeight'] == 0)        ?"&nbsp;" :$row['contWeight'];
  205.         $this->colArray[9][$cRecCnt]  = ( $row['moisture'] == 0)        ?"&nbsp;" :$row['moisture'];
  206.         $this->colArray[10][$cRecCnt] = ( $row['sampleInAir'] == 0)        ?"&nbsp;" :$row['sampleInAir'];
  207.         $this->colArray[11][$cRecCnt] = ( $row['sampleInWater'] == 0)    ?"&nbsp;" :$row['sampleInWater'];
  208.         $this->colArray[12][$cRecCnt] = ( $row['water'] == 0)            ?"&nbsp;" :$row['water'];
  209.         $this->colArray[13][$cRecCnt] = ( $row['dryUnit'] == 0)            ?"&nbsp;" :$row['dryUnit'];
  210.         $this->colArray[14][$cRecCnt] = ( $row['dimDryUnit'] == 0)        ?"&nbsp;" :number_format($row['dimDryUnit'], 1, '.', '');
  211.         $this->colArray[15][$cRecCnt] = ( $row['dimWeight'] == 0)        ?"&nbsp;" :number_format($row['dimWeight'], 1, '.', '');
  212.         $this->colArray[16][$cRecCnt] = ( $row["dimDiameter"] == 0)        ?"&nbsp;" :$row["dimDiameter"];
  213.         $this->colArray[17][$cRecCnt] = ( empty($row["qu"] ))            ?"&nbsp;" :$row["qu"];
  214.         $this->colArray[18][$cRecCnt] = ( empty($row["percentFail"] ))    ?"&nbsp;" :$row["percentFail"];
  215.         $this->colArray[19][$cRecCnt] = ( empty($row["qp"] ))            ?"&nbsp;" :$row["qp"];
  216.         $this->colArray[20][$cRecCnt] = ( empty($row["tv"] ))            ?"&nbsp;" :$row["tv"];
  217.         $this->colArray[21][$cRecCnt] = ( empty($row["soilDesc"] ))        ?"&nbsp;" :$row["soilDesc"];
  218.  
  219.     }                                                                                  
  220.  
  221.     public function printPage(){
  222. ?>
  223.         <table 
  224.             bgcolor="#FFFFFF"
  225.             width="100%"
  226.         >
  227.           <tr>
  228.             <td colspan="2">Job #: <span class="subHeading3"><?php echo $this->jobId ?></span>
  229.             &nbsp;Boring #: <span class="subHeading3"><?php echo $this->boring ?></span> Date: <span class="subHeading3"><?php echo $this->date ?></span></td>
  230.             <td align="right">Page: <span class="subHeading3"><?php echo $this->page ?></span></td>
  231.           </tr>
  232.           <tr>
  233.             <td><span class="subHeading3">Project: </span><?php echo $this->project ?></td>
  234.           </tr>
  235.         </table> 
  236.         <table 
  237.             bgcolor="#FFFFFF"
  238.             border="1"
  239.         >
  240.  
  241. <?php
  242.  
  243.         for ($i=0; $i <= 21; $i++) {
  244.             echo '<tr>';
  245.             //set row back ground color
  246.             if ($i == 9 or $i == 13 or $i == 14 or $i == 17 or $i == 19){
  247.                 $cCol="#E0E0E0";
  248.             }else{
  249.                 $cCol="#FFFFFF";
  250.             }
  251.  
  252.             echo '<td width="15%">'.$this->colArray[$i][0].'</td>';
  253.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][1].'</td>';
  254.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][2].'</td>';
  255.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][3].'</td>';
  256.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][4].'</td>';
  257.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][5].'</td>';
  258.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][6].'</td>';
  259.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][7].'</td>';
  260.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][8].'</td>';
  261.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][9].'</td>';
  262.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][10].'</td>';
  263.             echo '</tr>';
  264.         }
  265.  
  266.         echo '</table>';
  267.     }
  268. }
  269. ?>

9 1951
Dormilich
8,658 Recognized Expert Moderator Expert
line #25 looks suspicious for an array definition ... usually there are no assignments in there. (though that is different for an object)

hm, for the template, can’t you just reverse the nesting for the output?

meaning:
Expand|Select|Wrap|Line Numbers
  1. // if you have
  2. for ($i = 0; $ < $length_lvl1; $i++) // row level
  3. {
  4.   for ($j = 0; $ < $length_lvl2; $j++) // cell level
  5.   {
  6.     // do something with $array[$i][$j]
  7.   }
  8. }
  9.  
  10. // change it to
  11. for ($j = 0; $ < $length_lvl2; $j++) // row level
  12. {
  13.   for ($i = 0; $ < $length_lvl1; $i++) // cell level
  14.   {
  15.     // do something with $array[$i][$j]
  16.   }
  17. }
Jul 4 '12 #2
Claus Mygind
571 Contributor
Thanks for the response.

With regards to line 25, that one is actually right from the manual http://us.php.net/manual/en/function.array-fill.php

I see what you are doing with the code by reversing the calls, I will study that in more details. My problem is they have cut my work back to just one day a week so I am getting a little rusty.

I know what I want to do in concept, I just need to figure out how to write the code.

1) create an object that represents the page content. That is create a class

2) give the class some properties for the header information that does not change from page to page

3) add a table array (an array of an array) that will take the variable data or insert a blank html tag

4) add 4 methods that will process the data two for going in and two for taking it out. One of the two for the header and the other for the variable data (this is where I will use your coding example for handling the data)

5) define a pObj (page object) from the class

6) figure out how to call the methods to load and retrieve the data.

That is a lot to ask here so I had better get some books and start studying.
Jul 5 '12 #3
Claus Mygind
571 Contributor
Quick question do I still use the "include" statement if I define a class in the same code where I will call it.

This link is a pretty good example of what I want to do.
http://ballen.co.uk/2011/01/08/how-t...rst-php-class/

But I would like to just define the class and then write the access code just below it. I will not be needing this class in any other app. It just seems a sensible way to organize the data.
Jul 5 '12 #4
Dormilich
8,658 Recognized Expert Moderator Expert
With regards to line 25, that one is actually right from the manual
I didn’t mean the array_fill() function, I meant the assignment.

Quick question do I still use the "include" statement if I define a class in the same code where I will call it.
no. if the code is already there, it doesn’t need to be included (otherwise you get an already defined error). nevertheless, I would always use autoloading and never define a class in the executing script.

This link is a pretty good example of what I want to do.
be aware that the var keyword is from PHP4. cf. http://php.net/oop


I’ll get back later when I have more time to think about it.
Jul 5 '12 #5
Claus Mygind
571 Contributor
Your help has been fantastic as usual. Also I have had a chance to read up on classes, so now I have a better overview of the process.

Just a few notes on why I am doing it the way I am doing it.
1) Time is only permitting me to do a straight conversion from my old dBase code to php (no time for improving it, so I am sticking as closely to the old procedural coding as possible)

2) The returned result set from the query can create page breaks for 3 reasons. The data set is made up of:
A) a job
B) one or more borings
C) a set of samples (variable number from 1 to infinity)

To format the report with proper page breaks I use two loops that step through the $result set.

An external loop, which simply continues until the end of the returned query.

And a 2nd internal (within the class) loop that continues until a page break is needed.

Page breaks should occur when
1) the boring # changes
2) for each 11 samples of a boring
3) when the $result is at an end.

Here is what I have developed for the internal loop. The actual streaming out of the data will occur in the external loop after I return a pObj (page object)

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function makePageObject($result, $row, $cPageCnt,$cBoring){
  3.  
  4.     class pageObject
  5.     {
  6.         //properties
  7.         public jobId;
  8.         public boring;
  9.         public date;
  10.         public page;
  11.         public project;
  12.         public client;
  13.         public engineer;
  14.         public webUser;
  15.  
  16.         //detail sample infor for boring
  17.         public colArray;
  18.  
  19.         //methods
  20.         public __contruct( $result, $row, $cPageCnt,$cBoring ) {
  21.  
  22.             //page header info for specific boring
  23.             $this->jobId    = $row["jobid"];
  24.             $this->boring    = $row["boring"];
  25.             $this->date        = $row["sampleDate"];
  26.             $this->page        = $cPageCnt;
  27.             $this->project    = $row['prjctname'].'<br/>'.
  28.                               $row['prjctaddr1'].' '.$row['prjctaddr2'].'<br/>'.
  29.                               trim($row['prjctcity'].', '.
  30.                               $row['prjctst'];
  31.             $this->client    = $row['name'].'<br/>'.
  32.                               trim($row['addr1'].' '.
  33.                               $row['addr2'].'<br/>'.
  34.                               trim($row['city']).' '.
  35.                               $row['st'].' '.
  36.                               zipConvert(trim($row['zip']));
  37.             $this->engineer    = $row['engineer'];
  38.             $this->webUser    = trim($row['webuser']);
  39.  
  40.             //detail sample infor for boring
  41.             $this->colArray = array_fill(0, 22, array_fill(0, 11, "&nbsp;"));
  42.  
  43.             $cRecCnt = 0;
  44.  
  45.             //detail sample info for boring
  46.             while ($row = $result->fetch_array() and $cBoring == $row["boring"] and $cRecCnt < 11 ) 
  47.             {
  48.                 /*
  49.                 --------------------------------
  50.                 fill in columns with sample data
  51.                 --------------------------------
  52.                 */
  53.                 $this->colArray[0][$cRecCnt]  = $row["boring"];
  54.                 $this->colArray[1][$cRecCnt]  = $row["sample"];
  55.                 $this->colArray[2][$cRecCnt]  = $row["depth"]; 
  56.                 $this->colArray[3][$cRecCnt]  = $row["blows"];
  57.                 $this->colArray[4][$cRecCnt]  = $row["sampleType"];
  58.                 $this->colArray[5][$cRecCnt]  = $row["contNumb"];
  59.                 $this->colArray[6][$cRecCnt]  = $row['WWofSC'];
  60.                 $this->colArray[7][$cRecCnt]  = $row['DWofSC'];
  61.                 $this->colArray[8][$cRecCnt]  = $row['contWeight'];
  62.                 $this->colArray[9][$cRecCnt]  = $row['moisture'];
  63.                 $this->colArray[10][$cRecCnt] = $row['sampleInAir'];
  64.                 $this->colArray[11][$cRecCnt] = $row['sampleInWater'];
  65.                 $this->colArray[12][$cRecCnt] = $row['water'];
  66.                 $this->colArray[13][$cRecCnt] = $row['dryUnit'];
  67.                 $this->colArray[14][$cRecCnt] = $row['dimDryUnit'];
  68.                 $this->colArray[15][$cRecCnt] = $row['dimWeight'];
  69.                 $this->colArray[16][$cRecCnt] = $row["dimDiameter"];
  70.                 $this->colArray[17][$cRecCnt] = $row["qu"];
  71.                 $this->colArray[18][$cRecCnt] = $row["percentFail"];
  72.                 $this->colArray[19][$cRecCnt] = $row["qp"];
  73.                 $this->colArray[20][$cRecCnt] = $row["tv"];
  74.                 $this->colArray[21][$cRecCnt] = $row["soilDesc"];
  75.                 //advance
  76.                 $cRecCnt ++;
  77.             }
  78.         } 
  79.         $pObj = new pageObject( $result, $row, $cPageCnt,$cBoring );
  80.         return $pObj;
  81.     }
  82. ?>
Jul 6 '12 #6
Claus Mygind
571 Contributor
with regards to my array_fill() function and assignment, I see the following in the php manual

$a = array_fill(5, 6, 'banana');

that appears to use the same assignment method which I use to create my array

$this->colArray = array_fill(0, 22, array_fill(0, 11, "&nbsp;"));

The only difference I am crating a 2 dimensional array, or as properly stated in php an array of arrays.
Jul 6 '12 #7
Dormilich
8,658 Recognized Expert Moderator Expert
that appears to use the same assignment method which I use to create my array

$this->colArray = array_fill(0, 22, array_fill(0, 11, "&nbsp;"));
that makes sense now. previously you had
Expand|Select|Wrap|Line Numbers
  1. $pObj = array(
  2. $jobId    = $row["jobid"],
  3. $boring   = $row["boring"],
  4. $colArray = array_fill(0, 22, array_fill(0, 10, "&nbsp;"))
  5. );
which is somewhat strange for an array.

PS. I never meant the use of array_fill() to be strange.
Jul 6 '12 #8
Claus Mygind
571 Contributor
yep! I agree, my original post was goofy. Still one error in my last post where I declare the properties of the class, I forgot to use the $ as in "public $jobid;"
Jul 8 '12 #9
Claus Mygind
571 Contributor
The correct response in this case was to learn how to create an object from a class

By defining a class with properties and methods, I was able to create a page object to fill in data.

The object contained several properties one of which was an array of arrays.

I initialized the array with a nested array_fill( array_fill()), that gave me a two dimensional array to transpose my data from query rows to table columns.

Then I created 3 methods (functions) to build and print may page object.

The first method was a constructor which I used to store my header information as well as create the table array.

The second method was to load data into the array until I needed a page break or I reached the end of the data

The last method I used to stream out each page as it was complete.

Here is a sample cross-tab report that can easily be modified for any type of two dimensional data.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <?php
  3. /* soilsLabPrintable.php - 
  4.     Author:    Claus Mygind
  5.  
  6.     Date:    6/25/2012 - php
  7.  
  8.     Purpose: The purpose of this program is to create a multi-page cross tab report
  9.  
  10.     It uses one embeded class to create page objects, which can be completed
  11.     and then printed.
  12.  
  13. */
  14. try 
  15. {
  16.  
  17.     /*open connection to database*/
  18.     include(my includes);
  19.  
  20.     .... my query section .....
  21.  
  22.     if ( $result = $db->query($sql) )
  23.     {
  24.         if ( $result->num_rows > 0)
  25.         {
  26. ?>
  27.             <html>
  28.                 <head>
  29.                     <title>Printable cross tab report</title>
  30.                 </head>
  31.  
  32.                 <body>
  33.                     <form>
  34.  
  35. <?php
  36.                     $cPageCnt = 1;
  37.                     $cRecCnt = 0;
  38.                     $cBoring = '';
  39.  
  40.                     /*
  41.                     here I loop through the query result,
  42.                     a page object is create if none exists
  43.                     a page is streamed out when the data
  44.                     calls for a page break or the end of the data
  45.                     */
  46.  
  47.                     while ($row = $result->fetch_array()) 
  48.                     {
  49.  
  50.                         $cRecCnt ++;
  51.  
  52.                         if (!isset($pObj)){
  53.                             //set boring flag - used to flag a page break
  54.                             $cBoring = $row["boring"];
  55.  
  56.                             //create page object if not exists
  57.                             $pObj = new pageObject($row, $cPageCnt);
  58.                             $pObj->loadRow($row, $cRecCnt);
  59.  
  60.  
  61.                         }else if($cBoring != $row["boring"] or $cRecCnt > 10){
  62.  
  63.                             //page breaks are created when there are more than
  64.                             //10 samples or the next boring number is encountered.
  65.  
  66.                             //print page object if new boring or record count has exceeded page
  67.                             $pObj->printPage();
  68.  
  69.                             //insert page break
  70.                             echo '<span class="newPage"></span>';
  71.  
  72.                             //re-initilize control variables
  73.  
  74.                             //reset record counter
  75.                             $cRecCnt = 1;
  76.  
  77.                             //reset boring flag
  78.                             $cBoring = $row["boring"];
  79.  
  80.                             //destroy page object
  81.                             unset($pObj);
  82.  
  83.                             //advace page count
  84.                             $cPageCnt ++;
  85.  
  86.                             //create new page object
  87.                             $pObj = new pageObject($row, $cPageCnt);
  88.                             $pObj->loadRow($row, $cRecCnt);
  89.  
  90.                         }else{
  91.                             //add row data to page object
  92.                             $pObj->loadRow($row, $cRecCnt);
  93.                         }
  94.                     }
  95.  
  96.                     if (isset($pObj)){
  97.                         //some data is left to print
  98.                         $pObj->printPage();
  99.  
  100.                         //destroy page object
  101.                         unset($pObj);
  102.                     }
  103.  
  104. ?>
  105.                     </form>
  106.                 </body>
  107.             </html>
  108. <?php
  109.         }else{
  110.             throw new Exception("No records found!");
  111.         }
  112.  
  113.     }else{
  114.         throw new Exception("Query failed.");
  115.     }
  116. }
  117.  
  118. catch (exception $e) 
  119. {
  120.     echo 'An error occured : ' .$e->getMessage();
  121. }
  122.  
  123. class pageObject
  124. {
  125.     //properties
  126.     public $jobId;
  127.     public $boring;
  128.     public $date;
  129.     public $page;
  130.     public $project;
  131.     public $client;
  132.     public $engineer;
  133.     public $webUser;
  134.  
  135.     //detail sample infor for boring
  136.     public $colArray;
  137.  
  138.     //methods
  139.     public function __construct( $row, $cPageCnt ) {
  140.         /*
  141.         this method is called when a new page object is created
  142.         */
  143.  
  144.         //page header info for specific boring
  145.         $this->jobId    = $row["jobid"];
  146.         $this->boring    = $row["boring"];
  147.         $this->date        = $row["sampleDate"];
  148.         $this->page        = $cPageCnt;
  149.         $this->project    = $row['prjctname'];
  150.  
  151.         /*
  152.         detail information is stored in a cross-tab table 
  153.         where the query rowset is transposed into columns
  154.  
  155.         the table is inintialized with the required number
  156.         of cells. each cell is seeded with blank html tag
  157.         */
  158.         $this->colArray = array_fill(0, 22, array_fill(0, 11, "&nbsp;"));
  159.         /*
  160.         -------------------
  161.         fill in row headers 
  162.         -------------------
  163.         */
  164.         $this->colArray[0][0]  = "Boring";                                            
  165.         $this->colArray[1][0]  = "Sample #";                                          
  166.         $this->colArray[2][0]  = "Depth in Feet";                                     
  167.         $this->colArray[3][0]  = "No. of Blows";                                      
  168.         $this->colArray[4][0]  = "Type";                                              
  169.         $this->colArray[5][0]  = "Container Number";                                  
  170.         $this->colArray[6][0]  = "Wet weight of<br/>Sample + Container";              
  171.         $this->colArray[7][0]  = "Dry weight of<br/>Sample + Container";              
  172.         $this->colArray[8][0]  = "Weight of container";"Percent moisture";            
  173.         $this->colArray[9][0]  = "Percent moisture";                       
  174.         $this->colArray[10][0] = "Weight of sample in air";                           
  175.         $this->colArray[11][0] = "Weight of sample in water";                         
  176.         $this->colArray[12][0] = "Weight of water";                                   
  177.         $this->colArray[13][0] = "Disp. Dry unit weight in<br/>pounds per cubic foot";
  178.         $this->colArray[14][0] = "Dim. Dry unit weight in<br/>pounds per cubic foot"; 
  179.         $this->colArray[15][0] = "Dry unit scale weight";                             
  180.         $this->colArray[16][0] = "Dimension<br/>(Diameter * Length)";                  
  181.         $this->colArray[17][0] = "Qu (TSF)";                                          
  182.         $this->colArray[18][0] = "Qu % failure";                                      
  183.         $this->colArray[19][0] = "Qp (TSF)";                                          
  184.         $this->colArray[20][0] = "Tv";                                                
  185.         $this->colArray[21][0] = "Soil description"; 
  186.  
  187.     }                                
  188.  
  189.     public function loadRow($row, $cRecCnt){
  190.         /*
  191.         --------------------------------
  192.         fill in columns with sample data
  193.         --------------------------------
  194.         */
  195.  
  196.         $this->colArray[0][$cRecCnt]  = ( empty($row["boring"] ))        ?"&nbsp;" :$row["boring"];
  197.         $this->colArray[1][$cRecCnt]  = ( empty($row["sample"] ))        ?"&nbsp;" :$row["sample"];
  198.         $this->colArray[2][$cRecCnt]  = ( empty($row["depth"] ))        ?"&nbsp;" :$row["depth"]; 
  199.         $this->colArray[3][$cRecCnt]  = ( empty($row["blows"] ))        ?"&nbsp;" :$row["blows"];
  200.         $this->colArray[4][$cRecCnt]  = ( empty($row["sampleType"] ))    ?"&nbsp;" :$row["sampleType"];
  201.         $this->colArray[5][$cRecCnt]  = ( empty($row["contNumb"] ))        ?"&nbsp;" :$row["contNumb"];
  202.         $this->colArray[6][$cRecCnt]  = ( $row['WWofSC'] == 0)            ?"&nbsp;" :$row['WWofSC'];
  203.         $this->colArray[7][$cRecCnt]  = ( $row['DWofSC'] == 0)            ?"&nbsp;" :$row['DWofSC'];
  204.         $this->colArray[8][$cRecCnt]  = ( $row['contWeight'] == 0)        ?"&nbsp;" :$row['contWeight'];
  205.         $this->colArray[9][$cRecCnt]  = ( $row['moisture'] == 0)        ?"&nbsp;" :$row['moisture'];
  206.         $this->colArray[10][$cRecCnt] = ( $row['sampleInAir'] == 0)        ?"&nbsp;" :$row['sampleInAir'];
  207.         $this->colArray[11][$cRecCnt] = ( $row['sampleInWater'] == 0)    ?"&nbsp;" :$row['sampleInWater'];
  208.         $this->colArray[12][$cRecCnt] = ( $row['water'] == 0)            ?"&nbsp;" :$row['water'];
  209.         $this->colArray[13][$cRecCnt] = ( $row['dryUnit'] == 0)            ?"&nbsp;" :$row['dryUnit'];
  210.         $this->colArray[14][$cRecCnt] = ( $row['dimDryUnit'] == 0)        ?"&nbsp;" :number_format($row['dimDryUnit'], 1, '.', '');
  211.         $this->colArray[15][$cRecCnt] = ( $row['dimWeight'] == 0)        ?"&nbsp;" :number_format($row['dimWeight'], 1, '.', '');
  212.         $this->colArray[16][$cRecCnt] = ( $row["dimDiameter"] == 0)        ?"&nbsp;" :$row["dimDiameter"];
  213.         $this->colArray[17][$cRecCnt] = ( empty($row["qu"] ))            ?"&nbsp;" :$row["qu"];
  214.         $this->colArray[18][$cRecCnt] = ( empty($row["percentFail"] ))    ?"&nbsp;" :$row["percentFail"];
  215.         $this->colArray[19][$cRecCnt] = ( empty($row["qp"] ))            ?"&nbsp;" :$row["qp"];
  216.         $this->colArray[20][$cRecCnt] = ( empty($row["tv"] ))            ?"&nbsp;" :$row["tv"];
  217.         $this->colArray[21][$cRecCnt] = ( empty($row["soilDesc"] ))        ?"&nbsp;" :$row["soilDesc"];
  218.  
  219.     }                                                                                  
  220.  
  221.     public function printPage(){
  222. ?>
  223.         <table 
  224.             bgcolor="#FFFFFF"
  225.             width="100%"
  226.         >
  227.           <tr>
  228.             <td colspan="2">Job #: <span class="subHeading3"><?php echo $this->jobId ?></span>
  229.             &nbsp;Boring #: <span class="subHeading3"><?php echo $this->boring ?></span> Date: <span class="subHeading3"><?php echo $this->date ?></span></td>
  230.             <td align="right">Page: <span class="subHeading3"><?php echo $this->page ?></span></td>
  231.           </tr>
  232.           <tr>
  233.             <td><span class="subHeading3">Project: </span><?php echo $this->project ?></td>
  234.           </tr>
  235.         </table> 
  236.         <table 
  237.             bgcolor="#FFFFFF"
  238.             border="1"
  239.         >
  240.  
  241. <?php
  242.  
  243.         for ($i=0; $i <= 21; $i++) {
  244.             echo '<tr>';
  245.             //set row back ground color
  246.             if ($i == 9 or $i == 13 or $i == 14 or $i == 17 or $i == 19){
  247.                 $cCol="#E0E0E0";
  248.             }else{
  249.                 $cCol="#FFFFFF";
  250.             }
  251.  
  252.             echo '<td width="15%">'.$this->colArray[$i][0].'</td>';
  253.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][1].'</td>';
  254.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][2].'</td>';
  255.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][3].'</td>';
  256.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][4].'</td>';
  257.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][5].'</td>';
  258.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][6].'</td>';
  259.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][7].'</td>';
  260.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][8].'</td>';
  261.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][9].'</td>';
  262.             echo '<td width="8.5%" style="text-align:center;background-color:'.$cCol.';">'.$this->colArray[$i][10].'</td>';
  263.             echo '</tr>';
  264.         }
  265.  
  266.         echo '</table>';
  267.     }
  268. }
  269. ?>
Jul 9 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: porschberg | last post by:
Hi, I have a template class that looks like: template <class T> class Update { friend otl_stream& operator<< (otl_stream&, const shared_ptr<typename T::row>&); friend ostream& operator<<...
2
by: Rafał Maj Raf256 | last post by:
Test case: #include <boost/shared_ptr.hpp> using namespace boost; template <typename typ1> class cData { public: template <typename T> T As() { } // As };
1
by: Ram | last post by:
Hi All, I am using Object Data Source to bind data in the gridview. I have set the property AllowSorting=true. While running the application, I could sort the data only in ascending order. Is...
0
by: mike1402 | last post by:
Hi ! I get the error below sometimes when retrieving a big amount of data using Datadapter.Fill(dataset,"table"). But when I send the command Fill again, there is no error. Is it a fault of...
1
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template...
0
by: sajithamol | last post by:
I have a dll of a VB application which performs image manipulations. I want to build this dll file into a .cab file to embed this in the Object tag in client side. Also how to generate the classid...
1
by: wuych | last post by:
I have a question about using iterator in template function //*****code starts here***************************** #include <vector> using std::vector; template<typename Tvoid foo( vector<T& a...
1
by: neoairus | last post by:
I'm developing a pseudo-library for neural network. To simplify librarary using i wont to implement a sistem to instantiate different implementation of interface(Layer.h Neuron.h) passing a string...
9
by: Duggi | last post by:
I used to wonder why MS implemented C# to accept the following code using (Font f = new Font()) { // some code here. } While the same can be achieved through {
12
by: Duggi | last post by:
I used to wonder why MS implemented C# to accept the following code using (Font f = new Font()) { // some code here. } While the same can be achieved through {
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.