[help] color changing in php databse codes

Discussion in 'PHP' started by pratik, Nov 7, 2006.

  1. #1
    hi there,

    firstly i would like to thankz Aditya Singh who did the php code for me...

    secondly, here is the image attached about what is it...
    [​IMG]

    as you can see it there are rows in the table in gray color... they are called from mysql databse...

    now i want to change the color of every alternative row in the table like for example ID 1,3,5 would have dark grey and ID 2,4,6.. will have light gray.. so how can i do that i my php code..

    here is the code:
    <BODY topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginheight="0" marginwidth="0">
    <table width="100%" bgcolor="#FFFFFF" height="60">
    <tr>
     <td background="shadow.jpg"></td> </table>
    
    <TABLE border="1" bgcolor="#CCCCCC" bordercolor="#FFFFFF" cellpadding="0" cellspacing="0" align="center" width="1685px">
    <TR>
     <TD background="topbg.jpg" width="20px" align="center"><span class="style1">ID</span></TD>
     <TD background="topbg.jpg" width="75px" align="center" class="style1">Entry Date</TD>
     <TD background="topbg.jpg" width="90px" align="center" class="style1">Release Date</TD>
     <TD background="topbg.jpg" width="90px" align="center" class="style1">Issue Date</TD>
     <TD background="topbg.jpg" width="200px" align="center" bgcolor="#990000" class="style1">Product Advertised</TD>
     <TD background="topbg.jpg"  width="250px" align="center" bgcolor="#990000" class="style1">Magazine Newspaper</TD>
     <TD background="topbg.jpg"  width="70px" align="center" bgcolor="#990000" class="style1">Approved by</TD>
     <TD background="topbg.jpg"  width="60px" align="center" bgcolor="#990000" class="style1">Changes</TD>
     <TD background="topbg.jpg"  width="100px" align="center" bgcolor="#990000" class="style1">Ad Reach</TD>
     <TD background="topbg.jpg"  width="200px" align="center" bgcolor="#990000" class="style1">Communication Type</TD>
     <TD background="topbg.jpg"  width="50px" align="center" bgcolor="#990000" class="style1">Claims</TD>
     <TD background="topbg.jpg"  width="80px" align="center" bgcolor="#990000" class="style1">Bill Recieved Date</TD>
     <TD background="topbg.jpg"  width="80px" align="center" bgcolor="#990000" class="style1">Bill Date</TD>
     <TD background="topbg.jpg"  width="100px" align="center" bgcolor="#990000" class="style1">Bill Forwarded Date</TD>
     <TD background="topbg.jpg"  width="50px" align="center" bgcolor="#990000" class="style1">Bill No</TD>
     <TD background="topbg.jpg"  width="80px" align="center" bgcolor="#990000" class="style1">Bill Details</TD>
     <TD background="topbg.jpg"  width="90px" align="center" bgcolor="#990000" class="style1">Others</TD>
    </TR>
    
    <?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'vertrigo';
    $dbname = 'ad';
    
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    
    mysql_select_db($dbname);
    
    $query  = "SELECT * FROM addetail";
    
    $result = mysql_query($query);
    
    while($row = mysql_fetch_array($result, MYSQL_NUM))
    {
    
    echo "<TR>";
    echo "<TD align='center' height='30'>$row[0]</TD>";
    echo "<TD align='center'>$row[1]</TD>";
    echo "<TD align='center'>$row[2]</TD>";
    echo "<TD align='center'>$row[3]</TD>";
    echo "<TD align='center'>$row[4]</TD>";
    echo "<TD align='center'>$row[5]</TD>";
    echo "<TD align='center'>$row[6]</TD>";
    echo "<TD align='center'>$row[7]</TD>";
    echo "<TD align='center'>$row[8]</TD>";
    echo "<TD align='center'>$row[9]</TD>";
    echo "<TD align='center'>$row[10]</TD>";
    echo "<TD align='center'>$row[11]</TD>";
    echo "<TD align='center'>$row[12]</TD>";
    echo "<TD align='center'>$row[13]</TD>";
    echo "<TD align='center'>$row[14]</TD>";
    echo "<TD align='center'>$row[15]</TD>";
    echo "<TD align='center'>$row[16]</TD>";
    echo "</TR>";
    
    }
    
    mysql_close($conn);
    
    ?>
    
    
    </TABLE>
    </BODY>
    Code (markup):

    thanks in advance :)
     
    pratik, Nov 7, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Place this in your loop, and use $bgcolor wherever you need it.
    
    
    $bgcolor = ($bgcolor == '#CCCCCC' ? '#999999' : '#CCCCCC');
    
    Code (markup):
    EDIT:

    Since you edited, I will as well.

    
    while($row = mysql_fetch_array($result, MYSQL_NUM))
    {
    
    $bgcolor = ($bgcolor == #CCCCCC' ? '#999999' : '#CCCCCC');
    
    echo "<TR style=\"background-color:". $bgcolor .";\">$row[0]</TR>";
    
    .....
    
    Code (markup):
    EDIT:

    Note that there should be a sinlg equote infront of #CCCCCC, but the forum takes it out.
     
    nico_swd, Nov 7, 2006 IP
    pratik likes this.
  3. pratik

    pratik Notable Member

    Messages:
    2,306
    Likes Received:
    114
    Best Answers:
    0
    Trophy Points:
    200
    #3
    thankz for the reply...

    i did this:

    while($row = mysql_fetch_array($result, MYSQL_NUM))
    {
    $bgcolor = ($bgcolor == #CCCCCC' ? '#999999' : '#CCCCCC');
    
    echo "<TR style=\"background-color:". $bgcolor .";\">$row[0]</TR>";
    echo "<TD align='center' height='30'>$row[0]</TD>";
    Code (markup):
    but still itz not wrkin....
    error: Parse error: parse error, unexpected T_ECHO in C:\Program Files\VertrigoServ\www\ad\adphp.php on line 57

    :(
     
    pratik, Nov 7, 2006 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    I edited my post above. The forum takes out a single quote for some reason.
     
    nico_swd, Nov 7, 2006 IP
  5. pratik

    pratik Notable Member

    Messages:
    2,306
    Likes Received:
    114
    Best Answers:
    0
    Trophy Points:
    200
    #5
    now i see this:
    [​IMG]
     
    pratik, Nov 7, 2006 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Ah yeah, my bad.

    This
    
    echo "<TR style=\"background-color:". $bgcolor .";\">$row[0]</TR>";
    
    Code (markup):
    Should be this
    
    echo "<TR style=\"background-color:". $bgcolor .";\">
    
    Code (markup):
     
    nico_swd, Nov 7, 2006 IP
  7. pratik

    pratik Notable Member

    Messages:
    2,306
    Likes Received:
    114
    Best Answers:
    0
    Trophy Points:
    200
    #7
    give out an error " Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in C:\Program Files\VertrigoServ\www\ad\adphp.php on line 58"
     
    pratik, Nov 7, 2006 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Lol, I feel stupid. :(

    
    echo "<TR style=\"background-color:". $bgcolor .";\">";
    
    Code (markup):
     
    nico_swd, Nov 7, 2006 IP
  9. pratik

    pratik Notable Member

    Messages:
    2,306
    Likes Received:
    114
    Best Answers:
    0
    Trophy Points:
    200
    #9
    it worked

    thankz a lot for your help....
     
    pratik, Nov 7, 2006 IP
  10. pratik

    pratik Notable Member

    Messages:
    2,306
    Likes Received:
    114
    Best Answers:
    0
    Trophy Points:
    200
    #10
    stuck with one more problem...

    can anyone help me as to how can i get the data arranged according to groups in the table i mean now when i insert the data in phpmyadmin.. it displays on my php page one after the another... what i want is that when i click on the word "ID" it should arrange in ascending order... and then when i click on Release Data is shd get arranged in the dates ascending order.... like this i have 16 fields....



    [​IMG]
     
    pratik, Nov 24, 2006 IP
  11. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #11
    change each of the headers to links with the id of each header as a parameter in the link - e.g.
    http://www.domain.com/page.php?sort=column_id
    Code (markup):
    then change your query based on the parameter:

    from:
    $query  = "SELECT * FROM addetail";
    Code (markup):
    to:
    $query  = "SELECT * FROM addetail order by " . $_GET['column_id'];
    Code (markup):
     
    daboss, Nov 24, 2006 IP
  12. pratik

    pratik Notable Member

    Messages:
    2,306
    Likes Received:
    114
    Best Answers:
    0
    Trophy Points:
    200
    #12
    hey thankz for the prompt reply...

    i got this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\VertrigoServ\www\ad\adphp.php on line 56

    can u plz have a lok at my code above./..
     
    pratik, Nov 24, 2006 IP
  13. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #13
    post your code that you changed in a new post.
     
    daboss, Nov 24, 2006 IP
  14. pratik

    pratik Notable Member

    Messages:
    2,306
    Likes Received:
    114
    Best Answers:
    0
    Trophy Points:
    200
    #14
    well here is the code....

    <HTML>
    <HEAD><TITLE>iBall Print ADs</TITLE>
    <link href="style.css" rel="stylesheet" type="text/css">
    <style type="text/css">
    <!--
    .style1 {
    	color: #FFFFFF;
    	font-weight: bold;
    }
    .style3 {color: #CC0000}
    -->
    </style>
    </HEAD>
    <BODY topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginheight="0" marginwidth="0">
    <table width="1715px" bgcolor="#FFFFFF" height="60">
    <tr>
     <td background="shadow.jpg"><strong> &nbsp;&nbsp;&nbsp;</strong><span class="style3"> iBall Print AD Details </span></td> 
    </table>
    
    <TABLE border="1" bgcolor="#CCCCCC" bordercolor="#FFFFFF" cellpadding="0" cellspacing="0" align="center" width="1715px">
    <TR>
     <TD background="topbg.jpg" width="20px" align="center"><span class="style1">ID</span></TD>
     <TD background="topbg.jpg" width="75px" align="center" class="style1">Entry Date</TD>
     <TD background="topbg.jpg" width="90px" align="center" class="style1"><a href="http://localhost/ad/adphp.php?sort=column_id">Release Date</a></TD>
     <TD background="topbg.jpg" width="90px" align="center" class="style1">Issue Date</TD>
     <TD background="topbg.jpg" width="200px" align="center" bgcolor="#990000" class="style1">Product Advertised</TD>
     <TD background="topbg.jpg"  width="250px" align="center" bgcolor="#990000" class="style1">Magazine Newspaper</TD>
     <TD background="topbg.jpg"  width="100px" align="center" bgcolor="#990000" class="style1">Approved by</TD>
     <TD background="topbg.jpg"  width="60px" align="center" bgcolor="#990000" class="style1">Changes</TD>
     <TD background="topbg.jpg"  width="100px" align="center" bgcolor="#990000" class="style1">Ad Reach</TD>
     <TD background="topbg.jpg"  width="200px" align="center" bgcolor="#990000" class="style1">Communication Type</TD>
     <TD background="topbg.jpg"  width="50px" align="center" bgcolor="#990000" class="style1">Claims</TD>
     <TD background="topbg.jpg"  width="80px" align="center" bgcolor="#990000" class="style1">Bill Recieved Date</TD>
     <TD background="topbg.jpg"  width="80px" align="center" bgcolor="#990000" class="style1">Bill Date</TD>
     <TD background="topbg.jpg"  width="100px" align="center" bgcolor="#990000" class="style1">Bill Forwarded Date</TD>
     <TD background="topbg.jpg"  width="50px" align="center" bgcolor="#990000" class="style1">Bill No</TD>
      <TD background="topbg.jpg"  width="100px" align="center" bgcolor="#990000" class="style1">Bill Amount</TD>
     <TD background="topbg.jpg"  width="80px" align="center" bgcolor="#990000" class="style1">Bill Details</TD>
     <TD background="topbg.jpg"  width="90px" align="center" bgcolor="#990000" class="style1">Remarks</TD>
    </TR>
    
    <?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'vertrigo';
    $dbname = 'ad';
    
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    
    mysql_select_db($dbname);
    
    $query  = "SELECT * FROM addetail order by " . $_GET['column_id'];
    
    $result = mysql_query($query);
    
    while($row = mysql_fetch_array($result, MYSQL_NUM))
    {
    $bgcolor = ($bgcolor == '#CCCCCC' ? '#999999' : '#CCCCCC');
    
    echo "<TR style=\"background-color:". $bgcolor .";\">";
    echo "<TD align='center' height='30'>$row[0]</TD>";
    echo "<TD align='center'>$row[1]</TD>";
    echo "<TD align='center'>$row[2]</TD>";
    echo "<TD align='center'>$row[3]</TD>";
    echo "<TD align='center'>$row[4]</TD>";
    echo "<TD align='center'>$row[5]</TD>";
    echo "<TD align='center'>$row[6]</TD>";
    echo "<TD align='center'>$row[7]</TD>";
    echo "<TD align='center'>$row[8]</TD>";
    echo "<TD align='center'>$row[9]</TD>";
    echo "<TD align='center'>$row[10]</TD>";
    echo "<TD align='center'>$row[11]</TD>";
    echo "<TD align='center'>$row[12]</TD>";
    echo "<TD align='center'>$row[13]</TD>";
    echo "<TD align='center'>$row[14]</TD>";
    echo "<TD align='center'>$row[15]</TD>";
    echo "<TD align='center'>$row[16]</TD>";
    echo "<TD align='center'>$row[17]</TD>";
    echo "</TR>";
    
    }
    
    mysql_close($conn);
    
    ?>
    
    
    </TABLE>
    </BODY>
    </HTML>
    Code (markup):
     
    pratik, Nov 24, 2006 IP
  15. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #15
    daboss, Nov 24, 2006 IP
  16. pratik

    pratik Notable Member

    Messages:
    2,306
    Likes Received:
    114
    Best Answers:
    0
    Trophy Points:
    200
    #16
    well thankz again... its workin...

    the only think which doesnt work in the main page that is http://www.domain.com/page.php...... the error" Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\VertrigoServ\www\ad\adphp.php on line 56 "...

    but when i click on the link.. it works fine....
     
    pratik, Nov 24, 2006 IP
  17. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #17
    Don't ever in your entire life use GPC variables directly in query strings without filtering the input. Specially at the end of queries. Do something like.

    
    
    $allowed_orders = array('column_id', 'another_one', 'whatever');
    
    $order = in_array($_GET['order'], $allowed_orders) ? $_GET['order'] : 'default_order';
    
    //....
    
    "SELECT * FROM addetail order by ". $order;
    
    
    PHP:
     
    nico_swd, Nov 24, 2006 IP
  18. pratik

    pratik Notable Member

    Messages:
    2,306
    Likes Received:
    114
    Best Answers:
    0
    Trophy Points:
    200
    #18
    totally confused :confused:

    i replaced my tags with what nico_swd gave me....

    and in tht i replaced column_id with my first column id and the rest with others....

    but still i get errors... ;(
     
    pratik, Nov 24, 2006 IP
  19. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #19
    it takes time for people to try to digest your request for help. if you want help, please provide more details and be specific. take the time to write out your questions properly. provide enough details for people to help you.

    i got no idea whatsoever what the problem is from your post.
     
    daboss, Nov 24, 2006 IP
  20. pratik

    pratik Notable Member

    Messages:
    2,306
    Likes Received:
    114
    Best Answers:
    0
    Trophy Points:
    200
    #20
    okie here it goes....

    i changed the file after you gave me the changes..... everything is working fine.. i mean when i click on "release date" the data gets arranged in the desired format.... its working for all the feilds....

    but now the problem is there in the first page i mean the main php page(index.php)...;(

     
    pratik, Nov 24, 2006 IP