alternate table row color

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JohnDriver
    New Member
    • Nov 2007
    • 23

    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($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:
    [code=CSS]
    <style type="text/css">
    .color1 {
    background-color: white;
    }
    .color2 {
    background-color: black;
    }
    </style>
    [/code]


    [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!
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Check the source code. Does it turn out correct (as alternating classes) in the source code?

    Comment

    • JohnDriver
      New Member
      • Nov 2007
      • 23

      #3
      Originally posted by acoder
      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!

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by JohnDriver
        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]

        Comment

        • JohnDriver
          New Member
          • Nov 2007
          • 23

          #5
          Originally posted by acoder
          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 :-)

          Comment

          • JohnDriver
            New Member
            • Nov 2007
            • 23

            #6
            Originally posted by JohnDriver
            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?

            Comment

            • JohnDriver
              New Member
              • Nov 2007
              • 23

              #7
              Originally posted by JohnDriver
              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

              Code:
              function all_ascii($value){
                 $final = '';
                 $search = array(chr(145),chr(146),chr(147),chr(148),chr(150),chr(151));
                 $replace = array("'","'",'&quot;','&quot;','&ndash;','&ndash;','&trade;','&diams;');
              
                 $hold = str_replace($search[0],$replace[0],$value);
                 $hold = str_replace($search[1],$replace[1],$hold);
                 $hold = str_replace($search[2],$replace[2],$hold);
                 $hold = str_replace($search[3],$replace[3],$hold);
                 $hold = str_replace($search[4],$replace[4],$hold);
                 $hold = str_replace($search[5],$replace[5],$hold);
              
                 if(!function_exists('str_split')){
                     function str_split($string,$split_length=1){
                         $count = strlen($string);
                         if($split_length < 1){
                             return false;
                         } elseif($split_length > $count){
                             return array($string);
                         } else {
                             $num = (int)ceil($count/$split_length);
                             $ret = array();
                             for($i=0;$i<$num;$i++){
                                 $ret[] = substr($string,$i*$split_length,$split_length);
                             }
                             return $ret;
                         }
                     }
                 }
              
                 $holdarr = str_split($hold);
                 foreach ($holdarr as $val) {
                     if (ord($val) < 128) $final .= $val;
                 }
                 return $final;
              }
              A simpler version is
              Code:
              preg_replace('/([^ !#$%@()*+,-.\x30-\x5b\x5d-\x7e])/e',
                      "'\\x'.(ord('\\1')<16? '0': '').dechex(ord('\\1'))",$value);
              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!

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                When you run this without using Ajax, do you still get this problem?

                Comment

                • JohnDriver
                  New Member
                  • Nov 2007
                  • 23

                  #9
                  Originally posted by acoder
                  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.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by JohnDriver
                    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?

                    Comment

                    • JohnDriver
                      New Member
                      • Nov 2007
                      • 23

                      #11
                      Originally posted by acoder
                      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
                      Code:
                      select * from table
                      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.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #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.

                        Comment

                        • JohnDriver
                          New Member
                          • Nov 2007
                          • 23

                          #13
                          Originally posted by acoder
                          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.

                          Comment

                          • JohnDriver
                            New Member
                            • Nov 2007
                            • 23

                            #14
                            Originally posted by JohnDriver
                            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.

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #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.

                              Comment

                              Working...