[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

help with arrays in PHP.

Hi, I need some help with how to write a script for an array. I have a table with 7 rows and 5 columns ( this may vary ). The cells are named in the following way:

11 12 12 14 15 16 17
21 22 23 24 25 26 27
....
....

I need to color certain cells ( with the same color ); I know the starting cell and the ending cell, for eg the first cell could be 14 and the last cell could be 27. I need to shade all cells starting from 14 to 27 ( 11 cells ) . How do I do this? Thanks for the help.

A
0
aej1973
Asked:
aej1973
  • 2
1 Solution
 
designatedinitializerCommented:
You actually don't need arrays for this.
The following code does the trick:
<?php
// Color any table cells
$first = 14;    // first cell to color
$last  = 27;    // last cell to color

$cols  = 7;     // Number of columns in the table
$lines = 5;     // Number of lines in the table

$color  = "white";  // default cell color
$output = "<table>\n";
// iterate through each line
for($l=1; $l<$lines; $l++){
    $output.= "<tr>\n";
    // iterate through each column
    for($c=1; $c<$cols; $c++){
    	// determine this cell number
    	$cellnumber = $l*10+$c;
    	// determine this cell color
    	if($cellnumber >= $first && $cellnumber <= $last){
    		$color = "blue";    // defined range's cell color
    	}else{
    		$color  = "white";  // default cell color
    	}
        $output.= "<td style=\"background-color:$color;\" name=\"$cellnumber\">$cellnumber\n</td>\n";
    }
    $output.= "</tr>\n";
}
$output.= "</table>\n";

// output the table's HTML
echo $output;
?>

Open in new window

0
 
aej1973Author Commented:
Thanks  let me try it...
0
 
Ray PaseurCommented:
I need to color certain cells ( with the same color )
How do you know what cells are to have what colors?  How is this matrix created?  Is it a PHP two-dimensional array or a table of HTML rows and columns?  Please give us a little more to go on, thanks.
0
 
aej1973Author Commented:
Thank you works great!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now