By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,306 Members | 1,621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,306 IT Pros & Developers. It's quick & easy.

how to highlight cell

P: n/a
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
Oct 18 '06 #1
Share this Question
Share on Google+
17 Replies


P: n/a
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>

Oct 18 '06 #2

P: n/a
Hmm toffee <to****@toffee.comwrote:
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();
Oct 18 '06 #3

P: n/a
| 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>
Oct 18 '06 #4

P: n/a
Hmm Steve <no****@example.comwrote:
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();
Oct 18 '06 #5

P: n/a

".:[ ikciu ]:." <no@mail.comwrote in message
news:eh**********@news.dialog.net.pl...
| Hmm Steve <no****@example.comwrote:
| 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...fucking moron!
Oct 18 '06 #6

P: n/a
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****@example.comwrote in message
news:Ac***************@newsfe07.lga...
>
".:[ ikciu ]:." <no@mail.comwrote in message
news:eh**********@news.dialog.net.pl...
| Hmm Steve <no****@example.comwrote:
| 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...fucking moron!


Oct 18 '06 #7

P: n/a
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****@example.comwrote in message
news:Ac***************@newsfe07.lga...
>".:[ ikciu ]:." <no@mail.comwrote in message
news:eh**********@news.dialog.net.pl...
| Hmm Steve <no****@example.comwrote:
| 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...fucking moron!


Oct 18 '06 #8

P: n/a

"toffee" <to****@toffee.comwrote in message
news:eh**********@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
Oct 19 '06 #9

P: n/a

"Breklin" <br*****@sbcglobal.netwrote in message
news:1y******************@newssvr13.news.prodigy.c om...
| 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
Oct 19 '06 #10

P: n/a
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
Oct 19 '06 #11

P: n/a
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 . '">&nbsp;</td>';
}
echo '<td>' . $addendum . '</td>';
}
?>
</tr>
<?
}
?>
</body>
</html>
Oct 19 '06 #12

P: n/a
Rik
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
Oct 19 '06 #13

P: n/a
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 . '">&nbsp;</td>';
}
echo '<td>' . $addendum . '</td>';
}
?>
</tr>
<?
}
?>
</body>
</html>
Oct 19 '06 #14

P: n/a
I would highly reccomend using JavaScript, not php, for what you're
describing.

Oct 19 '06 #15

P: n/a
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
Oct 19 '06 #16

P: n/a
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.
Oct 19 '06 #17

P: n/a
Marcin Dobrucki wrote:
>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:
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();
?>
Oct 24 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.