473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

alternate table row color

Hi,

I am happy to say that with your help, I have been performing good in Ajax. Thanks for helping me to start with. I have a small problem now. I am pulling records from database and passing result of the query in a table. I would like some help with the code to colour the alternate rows please.


[PHP]
function printQuery($param)
{
echo "<table width=100% valign=center>";
echo "<tr>"; foreach ($param as $key => $result)
echo "<td>";

if (is_array($result))
{
printQuery($result);
}
else
{
echo ($result) . '<br />';
}
echo "</td>";
}
}

echo "</td>";
echo "</tr>";
echo "</table>";

[/PHP]

I tried many CSS and Javascript functions to colour table rows alternatively but they don't seem to work. No errors but wont do alternate colouring. I checked this forum also and found the following code useful but it takes the colour code from CSS for color2 CSS in every row not the first one. http://www.thescripts.com/forum/thre...ate+color.html

[PHP]
<?php
# Print the start of the table.
echo "\n<table>";

# Loop throught while $i is less than 10
# each time incrementing $i by one.
for($i = 0; $i < 10; $i++)
{
# $i % 2 divides the number $i by 2 and returns the rest (0 or 1)
# which I then add one to, so this will alwasy be either 1 or 2
$color = "color". ($i % 2 + 1);

# Print a row into the table.
echo "\n\t<tr><td class=\"$color\">Row $i</td></tr>";
}

# Print the end of the table.
echo "\n</table>";
?>
[/PHP]

What I changed in my code was the following:
Expand|Select|Wrap|Line Numbers
  1. <style type="text/css">
  2.  .color1 {
  3.   background-color: white;
  4. }
  5. .color2 {
  6.   background-color: black;
  7. }
  8. </style>
  9.  

[PHP]
function printQuery($param)
{
echo "<table width=100% valign=center>";
echo "<tr>";
foreach ($param as $key => $result)
{
for($i = 0; $i < 10; $i++)
{
$color = "color".($i % 2 + 1);
}
echo "<td width = 15% class=$color>";
if (is_array($result))
{
printQuery($result); }
else { echo ($result) . '<br />'; } echo "</td>";
} }

echo "</td>"; echo "</tr>"; echo "</table>";
[/PHP]

But it can pick up the second color from my CSS and not the alternating first one. Could someone please have a look at it and let me know what is wrong in here?

I am using Ajax call so I am writing CSS code in the initial page and not in the page where PHP code is. Could this be a problem? I tried putting code in both but didnt work. I thought that is what I am supposed to do. Let me know if its incorrect.

Thank you!
Dec 9 '07 #1
18 4327
acoder
16,027 Expert Mod 8TB
Check the source code. Does it turn out correct (as alternating classes) in the source code?
Dec 10 '07 #2
Check the source code. Does it turn out correct (as alternating classes) in the source code?
Hi Acoder

When I call this function,it runs fine and I get the table with values but im just trying to color the alternate rows. When I tried to apply the CSS and javascript function above, the rows took the color from color2 CSS which made me think that its not calculating the no. of row properly. Sorry didn't get the meaning of alternating classes. Let me know if I am not understanding please. Check the source code for what?

Thank you!
Dec 10 '07 #3
acoder
16,027 Expert Mod 8TB
Sorry didn't get the meaning of alternating classes. Let me know if I am not understanding please. Check the source code for what?
I meant the client-side code when you view the source in your browser.

By alternating classes, I mean "color1" and "color2" for each alternate row, so it should appear something like:
[HTML]<tr class="color1" ...>...</tr>
<tr class="color2" ...>...</tr>
<tr class="color1" ...>...</tr>
<tr class="color2" ...>...</tr>[/HTML]
Dec 11 '07 #4
I meant the client-side code when you view the source in your browser.

By alternating classes, I mean "color1" and "color2" for each alternate row, so it should appear something like:
[HTML]<tr class="color1" ...>...</tr>
<tr class="color2" ...>...</tr>
<tr class="color1" ...>...</tr>
<tr class="color2" ...>...</tr>[/HTML]
Hi Acoder

Thanks for explaining. Regarding client side code. since I am using Ajax, all I have on client side is the CSS for color1 and color2. The code to display the table is on server side only - in the php page that im calling. so what i am doing is passing values from client side to server side in PHP through Ajax Get method. and then in PHP page, i have written a sql server query which is passing results to printQuery function... printQuery function has just one <tr> tag and there is a loop on <td> tag. so i think what i was doing was wrong. I was calling color1 or color2 CSS in <td> tag instead of <tr>.

But I tried calling the row number calculation loop in <tr> too and it didnt work. But I think my code design is majorly wrong because I have no <tr class="color1" or "color2"> anywhere on the client side. The code to display the table is in printQuery function not on client side.

I have only an empty div on client side and CSS to handle the table returned from PHP. Sorry if its complicated but I am not able to figure out how to fix it.

Also, another important problem I am getting is when I am passing the results from Ajax PHP to client side in the table... I see a character which is a rhombus with a question mark numerous times.. I wonder why its doing so in only some of the sections and not the rest. I am not able to post the code at this moment for this problem, however, have you ever come across any such issue before? In IE, I dont get the whole result back from PHP page..its just 50% of what I am expecting - no div height set. and the same code in Firefox works, give full results expected but gives the rhombus question mark symbol... I am trying to break the code and run in parts but im still not clear what is going wrong.

Thank you again. Your help and time is much appreciated :-)
Dec 11 '07 #5
Hi Acoder

Thanks for explaining. Regarding client side code. since I am using Ajax, all I have on client side is the CSS for color1 and color2. The code to display the table is on server side only - in the php page that im calling. so what i am doing is passing values from client side to server side in PHP through Ajax Get method. and then in PHP page, i have written a sql server query which is passing results to printQuery function... printQuery function has just one <tr> tag and there is a loop on <td> tag. so i think what i was doing was wrong. I was calling color1 or color2 CSS in <td> tag instead of <tr>.

But I tried calling the row number calculation loop in <tr> too and it didnt work. But I think my code design is majorly wrong because I have no <tr class="color1" or "color2"> anywhere on the client side. The code to display the table is in printQuery function not on client side.

I have only an empty div on client side and CSS to handle the table returned from PHP. Sorry if its complicated but I am not able to figure out how to fix it.

Also, another important problem I am getting is when I am passing the results from Ajax PHP to client side in the table... I see a character which is a rhombus with a question mark numerous times.. I wonder why its doing so in only some of the sections and not the rest. I am not able to post the code at this moment for this problem, however, have you ever come across any such issue before? In IE, I dont get the whole result back from PHP page..its just 50% of what I am expecting - no div height set. and the same code in Firefox works, give full results expected but gives the rhombus question mark symbol... I am trying to break the code and run in parts but im still not clear what is going wrong.

Thank you again. Your help and time is much appreciated :-)

I found some matching problems with encoding and replacing using strtr function in PHP. But unluckily I could not find the ascii value for the question mark inside a black square. I dont know but when I tried to copy paste it here, I get this character � instead. Any idea what could be its value for me to remove this special character please?
Dec 11 '07 #6
I found some matching problems with encoding and replacing using strtr function in PHP. But unluckily I could not find the ascii value for the question mark inside a black square. I dont know but when I tried to copy paste it here, I get this character � instead. Any idea what could be its value for me to remove this special character please?
Sorry Guys

I am deviating from the issue I initially raised but getting correct results is more important then colouring rows alternatively. Kindly help.
I have managed to resolve general special characters like apostrophe but could not fix symbols like question in a black diamond.

The code I found on the internet is as follows:
http://www.atwebresults.com/forum/viewtopic.php?p=346&sid=01c0ea06fcb13eca154fb9bb0b 535d90

Expand|Select|Wrap|Line Numbers
  1. function all_ascii($value){
  2.    $final = '';
  3.    $search = array(chr(145),chr(146),chr(147),chr(148),chr(150),chr(151));
  4.    $replace = array("'","'",'&quot;','&quot;','&ndash;','&ndash;','&trade;','&diams;');
  5.  
  6.    $hold = str_replace($search[0],$replace[0],$value);
  7.    $hold = str_replace($search[1],$replace[1],$hold);
  8.    $hold = str_replace($search[2],$replace[2],$hold);
  9.    $hold = str_replace($search[3],$replace[3],$hold);
  10.    $hold = str_replace($search[4],$replace[4],$hold);
  11.    $hold = str_replace($search[5],$replace[5],$hold);
  12.  
  13.    if(!function_exists('str_split')){
  14.        function str_split($string,$split_length=1){
  15.            $count = strlen($string);
  16.            if($split_length < 1){
  17.                return false;
  18.            } elseif($split_length > $count){
  19.                return array($string);
  20.            } else {
  21.                $num = (int)ceil($count/$split_length);
  22.                $ret = array();
  23.                for($i=0;$i<$num;$i++){
  24.                    $ret[] = substr($string,$i*$split_length,$split_length);
  25.                }
  26.                return $ret;
  27.            }
  28.        }
  29.    }
  30.  
  31.    $holdarr = str_split($hold);
  32.    foreach ($holdarr as $val) {
  33.        if (ord($val) < 128) $final .= $val;
  34.    }
  35.    return $final;
  36. }
  37.  
  38.  
A simpler version is
Expand|Select|Wrap|Line Numbers
  1. preg_replace('/([^ !#$%@()*+,-.\x30-\x5b\x5d-\x7e])/e',
  2.         "'\\x'.(ord('\\1')<16? '0': '').dechex(ord('\\1'))",$value);
  3.  
I figured out why only half of the results were showing in IE. That is because of these special character. I compared them from IE and Firefox. IE managed to display a simple question mark (?) instead of apostrophe and trademark symbol. But as soon as firefox hits the black diamond symbol, it continues to run the code but displaying nearly 20 diamonds but IE does not go any further - leading to code half read.

I checked the database, and its all fine over there. That is how I knew that there was an apostrophe or trademark. But the point where I get the black diamond, there is no character at all in database - SQL server. I tried setting character encoding on all pages to UTF-8 and ISO- 8859-1 also but could not get rid of the black diamond in IE. I feel that if I can resolve the black diamond symbol in Firefox, it will automatically be fixed in IE. Sorry for the long reading, but wanted to explain in detail.

As of know, im deleting the special characters like trademark to get rid of incorrect symbols but would like someone's ideas on this.

Thanks a lot!
Dec 12 '07 #7
acoder
16,027 Expert Mod 8TB
When you run this without using Ajax, do you still get this problem?
Dec 12 '07 #8
When you run this without using Ajax, do you still get this problem?
When I run it without Ajax - In Firefox, it still shows question mark in black diamond. IE works fine - no unwanted characters.

But when using Ajax, IE does not show the results and stops executing queries any further. On the other hand, Firefox displays diamond symbol and continues to run rest of the code.

The datatype in database is also set to nvarchar so that should also not be a problem. I am using UTF-8 for all the pages now. I tried UCS-2 also in the webpages to check encoding for database. I am not sure how to set character encoding for PHP pages in Ajax using GET method. I have seen header been set using POST. I am going to try to call the header function in PHP pages.

The collation of SQL Server shows to be: Latin1_General_CI_AS

Please suggest.
Dec 12 '07 #9
acoder
16,027 Expert Mod 8TB
When I run it without Ajax - In Firefox, it still shows question mark in black diamond. IE works fine - no unwanted characters.
Perhaps you should get this working first. Have you also checked that the browser character encoding is correct?
Dec 13 '07 #10
Perhaps you should get this working first. Have you also checked that the browser character encoding is correct?
Well I opened the table in SQL Server 2005 and the table itself has square boxes after the text and the column width is 255. So if the text is 30 characters, the rest is all boxes. Just right click the table and click Open.

But when I run the query in query analyzer
Expand|Select|Wrap|Line Numbers
  1. select * from table
  2.  
It does not show any squares. I could not find the value of � or the question in black diamond character otherwise would have trimmed the text and removed them somehow. Any idea?

My concern is that these boxes are showing up in the database only so no wonder why firefox is showing diamond symbol there.. I think its because of whitespaces but its just a guess.

On the other hand, I tried UTF-8, UCS-2, ISO-8859-1 encodings but they didn't make any difference. So now I am stuck with a database table I think :-(

I can't change the way its inputting values into database since I am using a third party exe.
Dec 13 '07 #11
acoder
16,027 Expert Mod 8TB
By the way, is this in another language which might require a different encoding?

I think it would be a good idea to ask in the SQL Server forum why you're getting these symbols, so we can at least eliminate that line of enquiry.
Dec 14 '07 #12
By the way, is this in another language which might require a different encoding?

I think it would be a good idea to ask in the SQL Server forum why you're getting these symbols, so we can at least eliminate that line of enquiry.
Thanks Acoder. No this is not in another language. The data field is nvarchar set to 255 but the data is less than that. In the database table itself, it shows boxes instead. Eg. the table data should show :

main meals

but it shows main meals������������������������

so when I write the sql query, instead of passing just 'main meals' to the browser, it is passing main meals������������������������ which firefox is converting into diamond with question mark in it. That is what I think is happening in my case. No foreign language and no characters there.

I would be glad if you could move this post to SQL Server forum or I can open a new thread if you like.

Thank you very much.
Dec 14 '07 #13
Thanks Acoder. No this is not in another language. The data field is nvarchar set to 255 but the data is less than that. In the database table itself, it shows boxes instead. Eg. the table data should show :

main meals

but it shows main meals������������������������

so when I write the sql query, instead of passing just 'main meals' to the browser, it is passing main meals������������������������ which firefox is converting into diamond with question mark in it. That is what I think is happening in my case. No foreign language and no characters there.

I would be glad if you could move this post to SQL Server forum or I can open a new thread if you like.

Thank you very much.
Hello

I have fixed the problem. Found a very useful post from cmdr_skywalker on http://www.sqlteam.com/forums/post.asp?method=Reply&TOPIC_ID=67997&FORUM_ID=5

So I am relieved of the problem of removing special characters. Works fine both in IE and Firefox using Ajax. It was nothing wrong with Ajax calls, the data in the database table is wrongly formatted.

Could you please help me with the issue of colouring alternate rows in the table please. Thank you so much for your help. Really appreciate. Your ideas help a lot.


Please see the PrintQuery function in the first post again. There is no <tr> on client side... the table is displayed on PHP page only and sending it to an empty div.
Dec 14 '07 #14
acoder
16,027 Expert Mod 8TB
Good to see that you've solved that little problem.

Looking back at your original post, I see that the color classes are set for the TD elements, not the TR elements.

If you run this without Ajax, what does the PHP-generated client-side code look like (i.e. the HTML)? If there's nothing wrong with that, then we can look at your Ajax code.
Dec 14 '07 #15
Good to see that you've solved that little problem.

Looking back at your original post, I see that the color classes are set for the TD elements, not the TR elements.

If you run this without Ajax, what does the PHP-generated client-side code look like (i.e. the HTML)? If there's nothing wrong with that, then we can look at your Ajax code.
Hi Acoder

Sorry didn't reply earlier, I got caught up with some work. So I tried as you said and it has half done the job. I have this html page which has
Expand|Select|Wrap|Line Numbers
  1. <style type="text/css">
  2.  .color1 {
  3.   background-color: azure;
  4. }
  5. .color2 {
  6.   background-color: grey;
  7. }
  8. </style>
and in the html page, in the same page I have PHP function which calls results from SQL and passing it to this function in table below.

[PHP]
function printQuery($param)
{
echo "<table width=100% valign=center>";
for($i = 0; $i < 2; $i++) //If I increase $i<3 or $i< 5, it will print the same row 3 or 5 times but if I make it $i<1, then it does not alternate the colour of row and also shows the row just once
{
$color = "color".($i % 2 + 1);
echo "<tr class='$color'>";

foreach ($param as $key => $result)
{

echo "<td>";
if (is_array($result))
{
printQuery($result);
}
else {
echo ($result);
}
echo "</td>";
}
}

echo "</tr>";
echo "</table>";

}[/PHP]

So now this function prints the row in alternating colors but same row is displayed twice. I need to tweak the printQuery function so that it displays the row just once but the colour alternates. Please help me with this. Its just something I am missing in the print query function. Let me know if you need more information.

Cheers.
Dec 18 '07 #16
acoder
16,027 Expert Mod 8TB
There's no need for the for loop. Use a counter instead and update this in the foreach loop.
Dec 18 '07 #17
There's no need for the for loop. Use a counter instead and update this in the foreach loop.
How do I make the counter work. sorry dont know.
Dec 20 '07 #18
acoder
16,027 Expert Mod 8TB
How do I make the counter work. sorry dont know.
By counter, I meant a variable like the $i in the for loop, which you set to 0 outside the loop and then increment within the loop.
Dec 21 '07 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: roN | last post by:
Hi, I have a table with some links in it and I would like to color the whole cell when someone is moving his cursor over it, how can i do that, right now it looks like: <tr> <td height="42"...
1
by: santwize | last post by:
HI, I am using this property of table property... <table border="1" bordercolor="white" cellspacing="0" cellpadding="0" style="width: 99%;"> if i run with this code. it is working fine in IE...
2
by: B. | last post by:
I am able to set the alternate row color using AlternatingRowsDefaultCellStyle. however, if my grid is not fully filled, the alternate row color will not apply for blank rows. How can I fill the...
12
by: thegenius | last post by:
hey everyone, I'm transforming my site to be database driven website for better and faster modification since it uses HTML for the moment. My quetion is, I have a control panel where I can control...
3
by: mukeshrasm | last post by:
Hi I want to print the table with alternate row color dynamically? How can I do that using javascript ? Thanks
2
by: Wayne | last post by:
Has anyone found the "Alternate Background Color" property in Access 2007 forms to be buggy? As I scroll a continuous form that is using an alternate background color for every second record, the...
1
by: =?Utf-8?B?cmFvb2YgYWhtZWQga2hhbg==?= | last post by:
Hi All, I have a gridview in which i set alternate row color property. Now i want to set color change on mouse over and reset it back on mouse out. The problem is that when i mouse out it should...
3
by: pvong | last post by:
Doing this in VS2008 and VB.Net. I new at this and don't know how to program in Java. Can someone help me write a simple little java code to change the backgroup color of my table? Table ID =...
4
by: ronboulder | last post by:
Sorry, I'm new to BYTES.com and posted this first in the articles section.... When I display this HTML in a browser the colors do not work. This has to be something very elementary, but I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.