Solved

help with arrays in PHP.

Posted on 2012-04-02
4
284 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
Comment Utility
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
Comment Utility
Thanks  let me try it...
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Thank you works great!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now