Jump to content

[SOLVED] Populating table information


tigomark

Recommended Posts

Hello all,

 

I am trying to dynamically generate bgcolors with to help populate some information for a site. Here is the code snipets

function addColor(){
for ($a = 1; $a <= 12; $a++)
$this->color1 = "#ff0000";
$this->color2 = "#00ff00";
$this->color3 = "#0000ff";
$this->color4 = "#ffff00";
$this->color5 = "#ff00ff";
$this->color6 = "#f3e3d3";
$this->color7 = "#11ee33";
$this->color8 = "#ab55e1";
$this->color9 = "#12c522";
$this->color10 = "#f033a9";
$this->color11 = "#44e2f1";
$this->color12 = "#3af422";

//V4 Pre-Trip Colors
if ($data["zonename$j"] == "BRAKES"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color1);

}else if ($data["zonename$j"] == "RIGHT FRONT"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color2);

}else if ($data["zonename$j"] == "REAR LIGHTING"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color3);

}else if ($data["zonename$j"] == "LEFT REAR"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color4);

}else if ($data["zonename$j"] == "INSIDE BUS"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color5);

}else if ($data["zonename$j"] == "FRONT LIGHTING"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color6);

}else if ($data["zonename$j"] == "ENTERING BUS"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color7);

}else if ($data["zonename$j"] == "ENGINE COMPARTMENT"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color8);

}else if ($data["zonename$j"] == "DRIVERS SEAT AREA"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color9);

}else if ($data["zonename$j"] == "RIGHT REAR"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color10);

}else if ($data["zonename$j"] == "LEFT FRONT"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color11);

}

// Truck Pre-Trip colors
else if ($data["zonename$j"] == "TRAILER FRONT"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color1);

}else if ($data["zonename$j"] == "TRAILER LEFT REAR"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color2);

}else if ($data["zonename$j"] == "TRAILER RIGHT REAR"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color4);

}else if ($data["zonename$j"] == "TRAILER FRONT"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color5);

}else if ($data["zonename$j"] == "FRONT"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color6);

}else if ($data["zonename$j"] == "LF"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color7);

}else if ($data["zonename$j"] == "ENGINE"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color8);

}else if ($data["zonename$j"] == "RF"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color9);

}else if ($data["zonename$j"] == "INSIDE CAB"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color10);

}else if ($data["zonename$j"] == "LR"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color11);

}else if ($data["zonename$j"] == "RR"){

	 $background['$a'] = sprintf("bgcolor=\"%s\"", $this->color12);

}else{

	$background['$a'] = sprintf("bgcolor=\"%s\"", $this->color12);

}



}

Then the code is populated in another function

 

    $reporthtml .= sprintf("<td><div class=\"idColumn\">%d</div></td>\n", $this->rowNumber($rownum));
                    $reporthtml .= sprintf("<td>%s</td><td>%s</td><td>%s</td><td %s title=\"%s\">%s</td><td %s title=\"%s\">%s</td><td %s title=\"%s\">%s</td><td %s title=\"%s\">%s</td><td %s title=\"%s\">%s</td><td %s title=\"%s\">%s</td><td %s title=\"%s\">%s</td><td %s title=\"%s\">%s</td><td %s title=\"%s\">%s</td><td %s title=\"%s\">%s</td><td %s title=\"%s\">%s</td></tr>\n",
                        $data->inspType,
                    	$data->assetName,
  						$data->inspID,
  						$background['$a'],
  						$data->zonename1,
   						$data->timestamp1,
   						$background['$a'],
   						$data->zonename2,
   						$data->timestamp2,
   						$background['$a'],
   						$data->zonename3,
   						$data->timestamp3,
   						$background['$a'],
   						$data->zonename4,
   						$data->timestamp4,
   						$background['$a'],
   						$data->zonename5,
   						$data->timestamp5,
   						$background['$a'],
   						$data->zonename6,
   						$data->timestamp6,
   						$background['$a'],
   						$data->zonename7,
   						$data->timestamp7,
   						$background['$a'],
   						$data->zonename8,
   						$data->timestamp8,
   						$background['$a'],
   						$data->zonename9,
   						$data->timestamp9,
   						$background['$a'],
   						$data->zonename10,
   						$data->timestamp10,
   						$background['$a'],
   						$data->zonename11,
   						$data->timestamp11
   						
   						
   						);
					$rownum++;


 

Currently all of the other information is populated but the backgrounds are not populating.

 

Any help would be great

Link to comment
Share on other sites

Hi,

 

Change $background['$a'] to $background[$a] everywhere and see what that does. By using the single quotes, the $a is not expanded so the key to the array will be the literal string '$a'. I was going to suggest using double-quotes instead (i.e. $background["$a"]) which would expand $a, but is unnecessary, unless you specifically want a string key instead of a number.

 

A minor point, but your addColor function reassigns color1 through color12 each time through the for loop, which isn't very efficient. I would at least put the assignments before the loop declaration:

 

function addColor(){
   $this->color1 = "#ff0000";
   // etc
   for ($a = 1; $a <= 12; $a++)
   { 
       //V4 Pre-Trip Colors
       // etc
   }

 

Just noticed, you're also missing the { } from your for loop. This will cause the first statement after the for loop declaration to be run 12 times, and the rest of the code to be run once, which would also cause problems.

 

Cheers,

Darren.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.