473,748 Members | 7,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

alternate table row color

23 New Member
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($par am)
{
echo "<table width=100% valign=center>" ;
echo "<tr>"; foreach ($param as $key => $result)
echo "<td>";

if (is_array($resu lt))
{
printQuery($res ult);
}
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><t d 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($par am)
{
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($resu lt))
{
printQuery($res ult); }
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 4368
acoder
16,027 Recognized Expert Moderator MVP
Check the source code. Does it turn out correct (as alternating classes) in the source code?
Dec 10 '07 #2
JohnDriver
23 New Member
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 Recognized Expert Moderator MVP
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
JohnDriver
23 New Member
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
JohnDriver
23 New Member
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
JohnDriver
23 New Member
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.atwebresult s.com/forum/viewtopic.php?p =346&sid=01c0ea 06fcb13eca154fb 9bb0b535d90

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 Recognized Expert Moderator MVP
When you run this without using Ajax, do you still get this problem?
Dec 12 '07 #8
JohnDriver
23 New Member
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 Recognized Expert Moderator MVP
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

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

Similar topics

18
4572
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" class="frontboxtextandline"><a href="#">Features, Options &amp; Specifications</a></td> </tr> <tr> <td height="42" class="frontboxtextandline"><a href="#">Interactive Product
1
1195
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 but bordercolor is not working in FF. But if i use style like this: <table cellspacing="0" cellpadding="0" style="border-color:white; width: 99%;"> this is working fine in FF but not working in IE......
2
6175
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 blank rows with alternate color as well. Thanks.
12
3345
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 the tables of the site, some sections, edit the css... there is a section on my site where a table has to be created to display some info with specs for the user let's take this exmple Plan Name Space Bandwidth
3
2447
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
6036
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 background colors get confused and go haywire. Nice feature if it worked propertly!
1
1449
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 take the original color back, for this i need to know on which row mouse is hovering at present. Does anybody know how to catch the current row index. Any help would be appreciated. Regards.
3
1115
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 = OrderTable DropDownControl= SideDDL I just want to make it so if the SideDDL Selected.Value ="BUY", the OrderTable backgroup color is Green. If it's a "SELL", then OrderTable
4
2077
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 stumped. What am I doing wrong? <table BORDER=3 WIDTH="500" > <tr> <td WIDTH="100" BGCOLOR="#FFCC00"> <center><b><font face="Arial,Helvetica">Col A</font></b></center> </td>
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.