473,471 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Missing array elements

Claus Mygind
571 Contributor
I initialize an array with "$this->colArray = array_fill(0, 11, array_fill(0, 22, " "));" to create a two dimensional array ( an array of arrays) of blank HTML tags so I can display a complete table even if I do not fill in all the cells in the table.

Then I loop through my data and fill in the table cells.

The row data from the query is transposed so rows become columns in the array.

For some reason when I have less than a full page worth of data, some of the blank cells (not all) in the columns with no data in the array are deleted. In the table columns with no data rows below 5 are deleted. (see sample photo of results)



If I comment out the section where the data is loaded into the array then every column has missing cells.

My thought was I was reading the coordinates wrong, but that does not make sense as the data that is displayed are in the correct cells when I have a full page.

Am I reading the array incorrectly or what else could be deleting the array elements.

Here is my code, I have bolded what I think is the relevant text.
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <?php
  3. try   
  4. {
  5. ...some query code...
  6.  
  7.     if ( $result = $db->query($sql) )
  8.     {
  9.         if ( $result->num_rows > 0)
  10.         {
  11. ?>
  12.             <html>
  13.                 <head>
  14.                     <title>Printable Soil Boring Samples</title>
  15.  
  16.                 </head>
  17.  
  18.                 <body>
  19.                     <form>
  20.  
  21. <?php
  22.                     $cPageCnt = 1;
  23.                     $cRecCnt = 0;
  24.                     $cBoring = '';
  25.  
  26.                     while ($row = $result->fetch_array()) 
  27.                     {
  28.  
  29.                         $cRecCnt ++;
  30.  
  31.                         if (!isset($pObj)){
  32.                             //set boring flag
  33.                             $cBoring = $row["boring"];
  34.  
  35.                             //create page object if not exists
  36.                             $pObj = new pageObject($row, $cPageCnt);
  37.                             $pObj->loadRow($row, $cRecCnt);
  38.  
  39.  
  40.                         }else if($cBoring != $row["boring"] or $cRecCnt > 10){
  41.  
  42.                             //print page object if new boring or record count has exceeded page
  43.                             $pObj->printPage();
  44.  
  45.                             //insert page break
  46.                             echo '<span class="newPage"></span>';
  47.  
  48.                             //re-initilize control variables
  49.  
  50.                             //reset record counter
  51.                             $cRecCnt = 1;
  52.  
  53.                             //reset boring flag
  54.                             $cBoring = $row["boring"];
  55.  
  56.                             //destroy page object
  57.                             unset($pObj);
  58.  
  59.                             //advace page count
  60.                             $cPageCnt ++;
  61.  
  62.                             //create new page object
  63.                             $pObj = new pageObject($row, $cPageCnt);
  64.                             $pObj->loadRow($row, $cRecCnt);
  65.  
  66.                         }else{
  67.                             //add row data to page object
  68.                             $pObj->loadRow($row, $cRecCnt);
  69.                         }
  70.                     }
  71.  
  72.                     if (isset($pObj)){
  73.                         //some data is left to print
  74.                         $pObj->printPage();
  75.  
  76.                         //destroy page object
  77.                         unset($pObj);
  78.                     }
  79.  
  80. ?>
  81.                     </form>
  82.                 </body>
  83.             </html>
  84. <?php
  85.         }
  86.     }
  87. }
  88.  
  89. catch (exception $e) 
  90. {
  91.     echo 'An error occured : ' .$e->getMessage();
  92. }
  93.  
  94. class pageObject
  95. {
  96.     //properties
  97.     public $jobId;
  98.     public $boring;
  99.     public $date;
  100.     public $page;
  101.     public $project;
  102.     public $client;
  103.     public $engineer;
  104.     public $webUser;
  105.  
  106.     //detail sample infor for boring
  107.     public $colArray;
  108.  
  109.     //methods
  110.     public function __construct( $row, $cPageCnt ) {
  111.  
  112.         //page header info for specific boring
  113.         $this->jobId    = $row["jobid"];
  114.         $this->boring    = $row["boring"];
  115.         $this->date    = $row["sampleDate"];
  116.         $this->page    = $cPageCnt;
  117.         $this->project    = $row['prjctname'];
  118.         $this->client    = $row['name'];
  119.         $this->engineer    = $row['engineer'];
  120.         $this->webUser    = trim($row['webUser']);
  121.  
  122.         //detail sample infor for boring
  123.         $this->colArray = array_fill(0, 11, array_fill(0, 22, "&nbsp;"));
  124.         /*
  125.         -------------------
  126.         fill in row headers 
  127.         -------------------
  128.         */
  129.         $this->colArray[0][0]  = "Boring";                                            
  130.         $this->colArray[1][0]  = "Sample #";                                          
  131.         $this->colArray[2][0]  = "Depth in Feet";                                     
  132.         $this->colArray[3][0]  = "No. of Blows";                                      
  133.         $this->colArray[4][0]  = "Type";                                              
  134.         $this->colArray[5][0]  = "Container Number";                                  
  135.         $this->colArray[6][0]  = "Wet weight of<br/>Sample + Container";              
  136.         $this->colArray[7][0]  = "Dry weight of<br/>Sample + Container";              
  137.         $this->colArray[8][0]  = "Weight of container";"Percent moisture";            
  138.         $this->colArray[9][0]  = "Percent moisture";                       
  139.         $this->colArray[10][0] = "Weight of sample in air";                           
  140.         $this->colArray[11][0] = "Weight of sample in water";                         
  141.         $this->colArray[12][0] = "Weight of water";                                   
  142.         $this->colArray[13][0] = "Disp. Dry unit weight in<br/>pounds per cubic foot";
  143.         $this->colArray[14][0] = "Dim. Dry unit weight in<br/>pounds per cubic foot"; 
  144.         $this->colArray[15][0] = "Dry unit scale weight";                             
  145.         $this->colArray[16][0] = "Dimension<br/>(Diameter * Length)";                  
  146.         $this->colArray[17][0] = "Qu (TSF)";                                          
  147.         $this->colArray[18][0] = "Qu % failure";                                      
  148.         $this->colArray[19][0] = "Qp (TSF)";                                          
  149.         $this->colArray[20][0] = "Tv";                                                
  150.         $this->colArray[21][0] = "Soil description"; 
  151.  
  152.     }                                
  153.  
  154.     public function loadRow($row, $cRecCnt){
  155.         /*
  156.         --------------------------------
  157.         fill in columns with sample data
  158.         --------------------------------
  159.         */
  160.         $this->colArray[0][$cRecCnt]  = ( empty($row["boring"] ))        ?"&nbsp;" :$row["boring"];
  161.         $this->colArray[1][$cRecCnt]  = ( empty($row["sample"] ))        ?"&nbsp;" :$row["sample"];
  162.         $this->colArray[2][$cRecCnt]  = ( empty($row["depth"] ))        ?"&nbsp;" :$row["depth"]; 
  163.         $this->colArray[3][$cRecCnt]  = ( empty($row["blows"] ))        ?"&nbsp;" :$row["blows"];
  164.         $this->colArray[4][$cRecCnt]  = ( empty($row["sampleType"] ))    ?"&nbsp;" :$row["sampleType"];
  165.         $this->colArray[5][$cRecCnt]  = ( empty($row["contNumb"] ))        ?"&nbsp;" :$row["contNumb"];
  166.         $this->colArray[6][$cRecCnt]  = ( $row['WWofSC'] == 0)            ?"&nbsp;" :$row['WWofSC'];
  167.         $this->colArray[7][$cRecCnt]  = ( $row['DWofSC'] == 0)            ?"&nbsp;" :$row['DWofSC'];
  168.         $this->colArray[8][$cRecCnt]  = ( $row['contWeight'] == 0)        ?"&nbsp;" :$row['contWeight'];
  169.         $this->colArray[9][$cRecCnt]  = ( $row['moisture'] == 0)        ?"&nbsp;" :$row['moisture'];
  170.         $this->colArray[10][$cRecCnt] = ( $row['sampleInAir'] == 0)        ?"&nbsp;" :$row['sampleInAir'];
  171.         $this->colArray[11][$cRecCnt] = ( $row['sampleInWater'] == 0)    ?"&nbsp;" :$row['sampleInWater'];
  172.         $this->colArray[12][$cRecCnt] = ( $row['water'] == 0)            ?"&nbsp;" :$row['water'];
  173.         $this->colArray[13][$cRecCnt] = ( $row['dryUnit'] == 0)            ?"&nbsp;" :$row['dryUnit'];
  174.         $this->colArray[14][$cRecCnt] = ( $row['dimDryUnit'] == 0)        ?"&nbsp;" :number_format($row['dimDryUnit'], 1, '.', '');
  175.         $this->colArray[15][$cRecCnt] = ( $row['dimWeight'] == 0)        ?"&nbsp;" :number_format($row['dimWeight'], 1, '.', '');
  176.         $this->colArray[16][$cRecCnt] = ( $row["dimDiameter"] == 0)        ?"&nbsp;" :$row["dimDiameter"];
  177.         $this->colArray[17][$cRecCnt] = ( empty($row["qu"] ))            ?"&nbsp;" :$row["qu"];
  178.         $this->colArray[18][$cRecCnt] = ( empty($row["percentFail"] ))    ?"&nbsp;" :$row["percentFail"];
  179.         $this->colArray[19][$cRecCnt] = ( empty($row["qp"] ))            ?"&nbsp;" :$row["qp"];
  180.         $this->colArray[20][$cRecCnt] = ( empty($row["tv"] ))            ?"&nbsp;" :$row["tv"];
  181.         $this->colArray[21][$cRecCnt] = ( empty($row["soilDesc"] ))        ?"&nbsp;" :$row["soilDesc"];
  182.  
  183.     }                                                                                  
  184.  
  185.     public function printPage(){
  186. ?>
  187.         <table 
  188.             bgcolor="#FFFFFF"
  189.             width="100%"
  190.         >
  191.           <tr>
  192.             <td colspan="2">Job #:<?php echo $this->jobId ?></td>
  193.           </tr>
  194.         </table> 
  195.         <table border="1">
  196. <?php
  197.         for ($i=0; $i <= 21; $i++) {
  198.             echo '<tr>';
  199.             echo '<td>'.$this->colArray[$i][0].'</td>';
  200.             echo '<td>'.$this->colArray[$i][1].'</td>';
  201.             echo '<td>'.$this->colArray[$i][2].'</td>';
  202.             echo '<td>'.$this->colArray[$i][3].'</td>';
  203.             echo '<td>'.$this->colArray[$i][4].'</td>';
  204.             echo '<td>'.$this->colArray[$i][5].'</td>';
  205. //fails below here when there is no data in column
  206.                      echo '<td>'.$this->colArray[$i][6].'</td>';
  207.             echo '<td>'.$this->colArray[$i][7].'</td>';
  208.             echo '<td>'.$this->colArray[$i][8].'</td>';
  209.             echo '<td>'.$this->colArray[$i][9].'</td>';
  210.             echo '<td>'.$this->colArray[$i][10].'</td>';
  211.             echo '</tr>';
  212.         }
  213.  
  214.         echo '</table>';
  215.     }
  216. }
  217. ?>
Jul 9 '12 #1

✓ answered by Claus Mygind

A simple error, I reversed the rows and columns on the array_fill().

The correct code is:

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

1 1463
Claus Mygind
571 Contributor
A simple error, I reversed the rows and columns on the array_fill().

The correct code is:

$this->colArray = array_fill(0, 22, array_fill(0, 11, "&nbsp;"));
Jul 9 '12 #2

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

Similar topics

6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
3
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields = Obj.GetType().GetFields(); foreach (FieldInfo fi in...
8
by: ljlevend2 | last post by:
If a property's type is an array and the property is exposed in a PropertyGrid, then the property can be expanded so that the user can view and edit the array elements. But, I've noticed that if...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
6
by: Kumar | last post by:
How should I declare the array elements being half word? and the array elements of the type byte in C?? Please help Kumar
8
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
14
by: dan | last post by:
I would like to have the preprocessor automatically generate the number of array elements requested. Each element is zero. The elements get pasted into a larger array. The other elements may be...
1
by: simply123 | last post by:
I doing doing C btw... i have been trying to convert array elements into their respective addresses but Im faced with many problems. eg. int x (array with 7 elements) im trying to set the...
2
by: awaisworld13 | last post by:
I want to write a program which take input and save the poistions of the array elements in another array. let if i enter an array A= 4 A=1 A=2 A=3 now the output array will hold 1 2 3
1
by: eran yurman | last post by:
H, I need to call an API with the follwoing prototype: unsigned long CountDevices (BYTE *p_deviceSize,BYTE *p_device ); I have no knowledge of the content of the API function itself but...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.