For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > October 2006 > how to highlight cell









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to highlight cell
toffee

2006-10-18, 7:56 am

Hi all,

I have a table with 12 cols and 10 rows. When a user clicks on a table cell;
the page is refreshed and displays some data below the table dependant on
whichever cell was selected.
I would like to make it so that whichever cell was clicked; the background
color is changed - so that when the user sees the data, (s)he can tell which
cell it relates to.

Does anyone know of a clever way to do this ?
I'm afraid my creativity is running a bit dry on this one as the only
working way i could come up with so far is to have an if statement before
each table cell is created, which is long winded.

kind regards

T


Moot

2006-10-18, 6:57 pm

toffee wrote:
> Hi all,
>
> I have a table with 12 cols and 10 rows. When a user clicks on a table cell;
> the page is refreshed and displays some data below the table dependant on
> whichever cell was selected.
> I would like to make it so that whichever cell was clicked; the background
> color is changed - so that when the user sees the data, (s)he can tell which
> cell it relates to.
>
> Does anyone know of a clever way to do this ?
> I'm afraid my creativity is running a bit dry on this one as the only
> working way i could come up with so far is to have an if statement before
> each table cell is created, which is long winded.
>
> kind regards
>
> T


Long winded indeed.

The only other option I can think of at the moment is keeping an array
for each cell as X,Y coordinates, and the value of the array element
would be a string like 'style="background-color:#CCCCCC;"'. Then just
reference array location for each cell and output the contents. If it
is empty, fine, but if you assign a string like that, the color will
change.

Both solutions require you to put code at each cell though...
I've never figured out any way around it.

Ex:
<?
$cellStyles = array();
$cellStyles[1][0] = 'style="background-color:#CCCCCC;"';
?>

<table>
<tr>
<td <?=$cellStyles[0][0]?>>a</td>
<td <?=$cellStyles[1][0]?>>b</td>
</tr>
<tr>
<td <?=$cellStyles[0][1]?>>c</td>
<td <?=$cellStyles[1][1]?>>d</td>
</tr>
</table>

.:[ ikciu ]:.

2006-10-18, 6:57 pm

Hmm toffee <toffee@toffee.com> wrote:
> Does anyone know of a clever way to do this ?


you can use css to show color on clicked cell - bkg color or bkg img but you
will have to remeber in script what cell was clicked
orter solution make make href and set visited option on your bkg color then
you no need script

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

2be || !2be $this => mysql_query();


Steve

2006-10-18, 6:57 pm

| Both solutions require you to put code at each cell though...
| I've never figured out any way around it.
|
| Ex:
| <?
| $cellStyles = array();
| $cellStyles[1][0] = 'style="background-color:#CCCCCC;"';
| ?>
|
| <table>
| <tr>
| <td <?=$cellStyles[0][0]?>>a</td>
| <td <?=$cellStyles[1][0]?>>b</td>
| </tr>
| <tr>
| <td <?=$cellStyles[0][1]?>>c</td>
| <td <?=$cellStyles[1][1]?>>d</td>
| </tr>
| </table>


perhaps this may be easier (if i understood you correctly):


<html>
<style type="text/css">
td
{
background-color : white;
cursor : pointer;
width : 50px;
}
</style>
<script type="text/javascript">
var currentCell;
function colorMe(el)
{
if (!el){ return; }
if (!el.style){ return; }
if (currentCell){ currentCell.style.backgroundColor = ''; }
currentCell = el;
currentCell.style.backgroundColor = '#CCCCCC';
}
</script>
<body>
<table>
<tr>
<td onclick="colorMe(this);">a</td>
<td onclick="colorMe(this);">b</td>
</tr>
<tr>
<td onclick="colorMe(this);">c</td>
<td onclick="colorMe(this);">d</td>
</tr>
</table>
<body>
<html>


.:[ ikciu ]:.

2006-10-18, 6:57 pm

Hmm Steve <no.one@example.com> wrote:
> perhaps this may be easier (if i understood you correctly):


nope :) he wrote about refresh, and what if some1 block js?

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

2be || !2be $this => mysql_query();


Steve

2006-10-18, 6:57 pm


".:[ ikciu ]:." <no@mail.com> wrote in message
news:eh5qgm$3to$1@news.dialog.net.pl...
| Hmm Steve <no.one@example.com> wrote:
| > perhaps this may be easier (if i understood you correctly):
|
| nope :) he wrote about refresh, and what if some1 block js?

if you're going to ridicule me again, but in this thread now, i suggest that
you learn to COMPREHEND english!!! unless you want me to embarrass you
again. in case you missed it, the OP spoke of MANY things...but THIS is his
question:

<quote>
I would like to make it so that whichever cell was clicked; the background
color is changed
</quote>

so yes, my example does EXACTLY that...XXXXing moron!


toffee

2006-10-18, 6:57 pm

thanks everyone for the suggestions and I didnt mean to start an argument
:-/

Steve - apologies for not making it clearer, but when a user clicks on a
cell the page is indeed refreshed; i just wanted to be able to show which
cell caused the refresh by highlighting it.

kind regards

T

"Steve" <no.one@example.com> wrote in message
news:AcuZg.532$_z4.142@newsfe07.lga...
>
> ".:[ ikciu ]:." <no@mail.com> wrote in message
> news:eh5qgm$3to$1@news.dialog.net.pl...
> | Hmm Steve <no.one@example.com> wrote:
> | > perhaps this may be easier (if i understood you correctly):
> |
> | nope :) he wrote about refresh, and what if some1 block js?
>
> if you're going to ridicule me again, but in this thread now, i suggest

that
> you learn to COMPREHEND english!!! unless you want me to embarrass you
> again. in case you missed it, the OP spoke of MANY things...but THIS is

his
> question:
>
> <quote>
> I would like to make it so that whichever cell was clicked; the background
> color is changed
> </quote>
>
> so yes, my example does EXACTLY that...XXXXing moron!
>
>



Breklin

2006-10-18, 6:57 pm

Don't worry about those 2 arguing. They seem to be like the Odd Couple.
I've watched them bicker through 2 threads already within the past 24
hours. Wish they could just all get along...or take it outside...or
something.

As long as you get your solution, this ng proves its worth.

toffee wrote:
> thanks everyone for the suggestions and I didnt mean to start an argument
> :-/
>
> Steve - apologies for not making it clearer, but when a user clicks on a
> cell the page is indeed refreshed; i just wanted to be able to show which
> cell caused the refresh by highlighting it.
>
> kind regards
>
> T
>
> "Steve" <no.one@example.com> wrote in message
> news:AcuZg.532$_z4.142@newsfe07.lga...
>
> that
>
> his
>
>
>
>

Steve

2006-10-18, 9:59 pm


"toffee" <toffee@toffee.com> wrote in message
news:eh65mo$6g2$1@news.freedom2surf.net...
| thanks everyone for the suggestions and I didnt mean to start an argument
| :-/
|
| Steve - apologies for not making it clearer, but when a user clicks on a
| cell the page is indeed refreshed; i just wanted to be able to show which
| cell caused the refresh by highlighting it.
|
| kind regards
|
| T

very good. i actually posted that so you'd flesh out how you wanted the
refresh to run. one way is to use javascript and populate all the data
needed for all the cells and just hide that data in a div in the
html...getting it via js would allow you one trip to the server without the
refresh. as it is, i see you intend not to use js. the solution is fairly
simple...i'll work up a quick example and post it here.

cheers


Steve

2006-10-18, 9:59 pm


"Breklin" <breklin@sbcglobal.net> wrote in message
news:1yxZg.16089$e66.5994@newssvr13.news.prodigy.com...
| Don't worry about those 2 arguing. They seem to be like the Odd Couple.
| I've watched them bicker through 2 threads already within the past 24
| hours. Wish they could just all get along...or take it outside...or
| something.

lol. it does seem that way, doesn't it. i've got him plonked so i won't see
further posts.

l8r


kenoli

2006-10-19, 3:58 am

Suppose you created an array called $class_value. In each cell you had
something like this:

<td class = "<?php $class_value[{$_GET[5]}]" ?>" >

and the way you clicked on the cell sent a "GET" that set the value of
$class_value[{$_GET[5]}] to "current" and then you had a style named
..current that set the background to the color you want the current cell
to be. Each cell would have a different matched "get" index set in the
class php and the "get" that was sent so that only the array with the
index in that cell would be turned to "current.

At the beginning of the script you would reset the value of
$class_value before the "get" set it for the current cell.

I think something like that might work.

--Kenoli

toffee wrote:
> Hi all,
>
> I have a table with 12 cols and 10 rows. When a user clicks on a table cell;
> the page is refreshed and displays some data below the table dependant on
> whichever cell was selected.
> I would like to make it so that whichever cell was clicked; the background
> color is changed - so that when the user sees the data, (s)he can tell which
> cell it relates to.
>
> Does anyone know of a clever way to do this ?
> I'm afraid my creativity is running a bit dry on this one as the only
> working way i could come up with so far is to have an if statement before
> each table cell is created, which is long winded.
>
> kind regards
>
> T


Steve

2006-10-19, 3:58 am

ok toffee. here's the example script...sorry for the text wrapping. hth.


<?
$currentCell = $_REQUEST['currentCell'];

// setup stubbed records
$records = array();
for ($key = 0; $key < 10; $key++)
{
for ($i = 0; $i < 10; $i++)
{
$column = chr(65 + $i);
$records[$key][$column] = 'VALUE ' . $column;
$addendums[$key][$i] = 'ROW ' . $key . ' :: CELL ' . $column;
}
}

// set addendum text
$currentRow = floor($currentCell / 10);
$currentCell = $currentCell % 10;
$addendum = $addendums[$currentRow][$currentCell];

// output html
?>
<html>
<title>Cell Highlighting</title>
<style type="text/css">
a
{
color : #000060;
text-decoration : none;
white-space : nowrap;
}
a:active
{
color : #000060;
text-decoration : none;
}
a:link
{
color : #000060;
text-decoration : none;
}
a:visited
{
color : #000060;
text-decoration : none;
}
a:hover
{
color : 000060;
text-decoration : underline;
}
table
{
border-collapse : collapse;
border-padding : 2px;
border-width : 0px;
border-spacing : 0px;
}
td
{
background-color : #FFFFFF;
color : black;
font-family : verdana, tahoma, arial, 'times new
roman', sans-serif;
font-size : 8pt;
margin : 5px;
padding-left : 5px;
padding-right : 5px;
spacing : 0px;
text-align : left;
vertical-align : middle;
width : 100px;
}
th
{
background-color : lavender;
border-bottom : solid 1px lightsteelblue;
color : black;
font-family : verdana, tahoma, arial, 'times new
roman', sans-serif;
font-size : 8pt;
font-weight : bold;
margin : 5px;
padding-left : 5px;
padding-right : 5px;
text-align : left;
vertical-align : middle;
}
</style>
<body>
<table>
<?
$header = array_keys($records[0]);
echo '<th>' . implode('</th><th>', $header) . '</th>';
$index = 0;
foreach ($records as $row => $record)
{
?>
<tr>
<?
foreach ($record as $column => $value)
{
$backgroundColor = $row == $currentRow && $index == $currentCell ?
'#CCCCCC' : 'white';
$uri = '?currentCell=' . $index;
?>
<td style="background-color:<?= $backgroundColor ?>">
<a href="<?= $uri ?>" title="Click for description"><?= $value
?></a>
</td>
<?
$index++;
}
if ($row == $currentRow)
{
echo '</tr><tr>';
if ($currentCell > 0)
{
echo '<td colspan="' . $currentCell . '"> </td>';
}
echo '<td>' . $addendum . '</td>';
}
?>
</tr>
<?
}
?>
</body>
</html>


Rik

2006-10-19, 3:58 am

toffee wrote:
> thanks everyone for the suggestions and I didnt mean to start an
> argument :-/
>
> Steve - apologies for not making it clearer, but when a user clicks
> on a cell the page is indeed refreshed; i just wanted to be able to
> show which cell caused the refresh by highlighting it.



Well, let me say that first of all when displaying certain data above in a
table, on which the data below depends, I cannot imagine how it's not
possible to use a simple:

for(table_cells loop)}
$class = (selected) ? ' class="selected"':'';
echo "<td{$class}>".$some_value().'</td>';
}

But hey:

Requirements:
1. You buffer your table in variable $table, no direct output.
2. x- and y-coördinates are known.
3. No rowspan or colspan in sight.
4. Classnames are correctly quoted with "";
5. Attribut names or values never hold the invalid '>' (should he &lt; if
desired).

Use only on own tables with ensured above properties.

Example, I'm not testing this, so there are possible type-errors:
---CSS---
td.higlight{
background-color: yellow;
}
---PHP---

$table = //the table as you made it.
$x = //the x coordinate
$y = //the y coordinate
$regex = '|(<table.*?)
((<tr[^>]*>.*?</tr>.*?){'.($y-1).'}) #all previous rows
((<td[^>]*>.*?</td>.*?){'.($x-1).'}) #all previous cells
<td #target cell
([^>])*? #random attributes
(\s*class="([^"])*")? #perhaps existing classvalue
([^>])*? #random attributes
>|six';

$table = preg_replace($regex,'$1$2$4<td$6 class="highlight $8"$9>',$table);


/x is underestimated in regexes :-)

--
Grtz,

Rik Wasmus


Steve

2006-10-19, 3:58 am

ooops. ;^)

only top row highlighted. i rushed without noticing prior to
posting...sorry. here's the correction:

<?
$currentCell = $_REQUEST['currentCell'];

// setup stubbed records
$records = array();
for ($key = 0; $key < 10; $key++)
{
for ($i = 0; $i < 10; $i++)
{
$column = chr(65 + $i);
$records[$key][$column] = 'VALUE ' . $column;
$addendums[$key][$i] = 'ROW ' . $key . ' :: CELL ' . $column;
}
}

// set addendum text
$selectedCell = $currentCell;
$columnCount = count(array_keys($records[0]));
$currentRow = floor($currentCell / $columnCount);
$currentCell = $currentCell % $columnCount;
$addendum = $addendums[$currentRow][$currentCell];

// output html
?>
<html>
<title>Cell Highlighting</title>
<style type="text/css">
a
{
color : #000060;
text-decoration : none;
white-space : nowrap;
}
a:active
{
color : #000060;
text-decoration : none;
}
a:link
{
color : #000060;
text-decoration : none;
}
a:visited
{
color : #000060;
text-decoration : none;
}
a:hover
{
color : 000060;
text-decoration : underline;
}
table
{
border-collapse : collapse;
border-padding : 2px;
border-width : 0px;
border-spacing : 0px;
}
td
{
background-color : #FFFFFF;
color : black;
font-family : verdana, tahoma, arial, 'times new
roman', sans-serif;
font-size : 8pt;
margin : 5px;
padding-left : 5px;
padding-right : 5px;
spacing : 0px;
text-align : left;
vertical-align : middle;
width : 100px;
}
th
{
background-color : lavender;
border-bottom : solid 1px lightsteelblue;
color : black;
font-family : verdana, tahoma, arial, 'times new
roman', sans-serif;
font-size : 8pt;
font-weight : bold;
margin : 5px;
padding-left : 5px;
padding-right : 5px;
text-align : left;
vertical-align : middle;
}
</style>
<body>
<table>
<?
$header = array_keys($records[0]);
echo '<th>' . implode('</th><th>', $header) . '</th>';
$index = 0;
foreach ($records as $row => $record)
{
?>
<tr>
<?
foreach ($record as $column => $value)
{
$backgroundColor = $index == $selectedCell ? '#CCCCCC' : 'white';
$uri = '?currentCell=' . $index;
?>
<td style="background-color:<?= $backgroundColor ?>">
<a href="<?= $uri ?>" title="Click for description"><?= $value
?></a>
</td>
<?
$index++;
}
if ($row == $currentRow)
{
echo '</tr><tr>';
if ($currentCell > 0)
{
echo '<td colspan="' . $currentCell . '"> </td>';
}
echo '<td>' . $addendum . '</td>';
}
?>
</tr>
<?
}
?>
</body>
</html>


dimo414

2006-10-19, 3:58 am

I would highly reccomend using JavaScript, not php, for what you're
describing.

Markus Ernst

2006-10-19, 7:56 am

toffee schrieb:
> Steve - apologies for not making it clearer, but when a user clicks on a
> cell the page is indeed refreshed; i just wanted to be able to show which
> cell caused the refresh by highlighting it.


So you are actually submitting some info along with the refresh to tell
the script what data it has to display below the table. I assume that
this info is also available when generating the table cell - otherwise
you are not able to submit it at all. (An URL to your example would
actually be very helpful for clarifying what you actually want to do.)

Now I don't think that any X and Y values are needed. Let's assume the
info is an item number, then your table generation code will contain
something like this at the moment:

<?php foreach ($rows as $row) { ?>
<tr>
...
<td>
<a href="my_script.php?item_nr=<?php echo $row['item_nr']; ?>">
Click here for info about item nr. <?php echo $row['item_nr']; ?>
</a>
</td>
...
</tr>
<?php } ?>

In order to highlight the cell last clicked on, you evaluate the
transmitted info, and if it corresponds with the current item nr you
assign a class to the td:

<?php
foreach ($rows as $row) {
if (isset($_GET['item_nr']) && $_GET['item_nr'] == $row['item_nr'])
$class = 'highlight';
else
$class = 'standard';
?>
....
<td class="<?php echo $class; ?>">

Then you style the highlight class with CSS.

If you want to use Javascript instead, as many suggest, you should
consider to drop page refreshing at all by
- either outputting all info boxes in divs with display:none, and change
them to display_block when the cell is clicked
- or use Ajax to retrieve the info when the cell is clicked.

For both ways you can include changing the cell background in the
function that you call on Click. Both also need extra coding effort to
make the page useable for non-Javascript browsers.

--
Markus
Marcin Dobrucki

2006-10-19, 7:56 am

toffee wrote:

> I have a table with 12 cols and 10 rows. When a user clicks on a table cell;
> the page is refreshed and displays some data below the table dependant on
> whichever cell was selected.
> I would like to make it so that whichever cell was clicked; the background
> color is changed - so that when the user sees the data, (s)he can tell which
> cell it relates to.
>
> Does anyone know of a clever way to do this ?
> I'm afraid my creativity is running a bit dry on this one as the only
> working way i could come up with so far is to have an if statement before
> each table cell is created, which is long winded.


PEAR::HTML_Table, and then whatever you click inside the cell needs
to carry some coordinates. After that, its a simple matter of:

$table = new HTML_Table(/*your parameters*/);
//...
$table-> updateCellAttributes($x,$y,$attributes);


Other than that, its basically the same solution as the others
suggested without the muck of writing it yourself.
Marcin Dobrucki

2006-10-24, 7:57 am

Marcin Dobrucki wrote:

>
>
> PEAR::HTML_Table, and then whatever you click inside the cell needs to
> carry some coordinates. After that, its a simple matter of:


Okay, here is some proof-of-concept code to go with the suggestion
(you need PEAR::HTML_Page2 and PEAR::HTML_Table installed):

<?php
require_once('HTML/Page2.php'); // so that we don't need to echo
require_once('HTML/Table.php');

$p = new HTML_Page2();
$t = new HTML_Table(array("frame" => "border",
"rules"=>"all",
"cellpadding" => 10));
for ($i=0; $i<3; $i++) {
for ($j=0; $j<3; $j++) {
$t->setCellContents($i,$j,"<a
href=\"".$_SERVER["PHP_SELF"]."?x=$i&y=$j\">highlight</a>");
}
}

if (isset($_GET["x"])) {
$t->updateCellAttributes($_GET["x"], $_GET["y"],
array("bgcolor" => "pink"));
}

$p->addBodyContent($t);
$p->display();
?>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com