Solved

help with arrays in PHP.

Posted on 2012-04-02
4
289 Views
Last Modified: 2012-04-03
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
Comment
Question by:aej1973
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
designatedinitializer earned 175 total points
ID: 37798533
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
 

Author Comment

by:aej1973
ID: 37798539
Thanks  let me try it...
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37798942
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
 

Author Closing Comment

by:aej1973
ID: 37802983
Thank you works great!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question