Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Is it possible?

  1. #1
    New Coder
    Join Date
    May 2004
    Location
    Portugal
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is it possible?

    Hi there!
    I've just started in the world of PHP and I got one problem.
    I've got an html table where i puted a repetitive region to display the results of a mysql table... everything is working fine but I need to change the colors of the rows for each record... how can I do that.

    you can see the page that i'm refering to at http://doom9.layernetworks.org/

    the design that I want to achieve is at http://www.doom9.org

    Is it possible?

    Thank you
    Rockas Software - Free applications

    Warning!!!
    Too much sex is not healthy! Causes amnesia and some other stuff that I can't remember!!!

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,077
    Thanks
    11
    Thanked 98 Times in 96 Posts
    not knowing how you build your table ... its a guess but logic may look something similar to ..

    PHP Code:
    <?
    while($whatever<10){
        
    $class = ( $class == 'lightgrey' ) ? 'darkgrey' 'lightgrey' ;
        ++
    $whatever;
        echo 
    '<span class="'.$class.'">'.$whatever.'</span>';
    }
    ?>
    <edit>PS: please use a more descriptive thread title next time</edit>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what a bizare approach ...
    We are looking for a table with alternating background colours, right?
    Just check against the modulus to decide if it's an even row or not, while you are looping through the recordset. Like
    PHP Code:
    $i=0;
    while (
    $row=mysql_fetch_assoc($result)){ // supposing you have $result as resource ID for the recordset
       
    $i ++;
       
    $backgr='<tr class="noteven">';   
       if (
    $i == 0){
          
    $backgr='<tr class="even">';
       }
       echo 
    $backgr '<td> ....blabla ...</td></tr>';

    The same can be done if you just set the background attribute instead of using classes.
    Or you could have more then 2 alternating colours (by using a % 5 and then using a switch-structure to set the class or background-attribute), or you could group records by not incrementing $i for each instance but only if the value for one of the fields is different from the previous etc etc.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #4
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Wow, those are quite interesting answers. I must have a mind closer to raf's, I always use the modulus operator for changing bg colors and the like, however, I find firepages' response to be a quite elegant approach.

    I wonder if there's a performance difference between the two? Say if you're cycling through 10,000+ records??? Would be interesting to know, although trivial. I can't think of a reason to spit out 10,000 + records to the screen. Would seem to overwhelm the user. But I'm rambling now, so I digress.

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,077
    Thanks
    11
    Thanked 98 Times in 96 Posts
    well, in my rough test & over 1 million iterations mine is about .05 of a second faster (thats over the whole million !) so basically no difference.

    I dont see it as bizarre just different , to be honest I always found the use of modulus operater strange for these sorts of things though I appreciate that is the common method employed for such things.

    my version looks cleaner to me , though of course thats subjective .
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well, i'm a simple buttonpusher with a very small brain, so i need to restrict the number or tricks and tips that is use

    Two different ways to alternate backgrounds would take up to much space upthere.

    using the modulus is a simple way that works with 2 alternating colours or more, and that even works for alternating columns or for writitng out the results like 'each row must contain the value from 3 records'. So i've always used it as a sort of universal way to decide when some 'flip' needs to be made.

    It's actually surprising that the modulus is just a tad slower. I'd suspect it to be much slower then a simple 'if then'.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    New Coder
    Join Date
    May 2004
    Location
    Portugal
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you all!!!

    As I said I'm just a beginner and so I just don't know where to fit the examples that you gave me:

    <?php

    $colname_Recordset1 = "5";

    if (isset($_GET['mes'])) {

    $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_GET['mes'] : addslashes($_GET['mes']);

    }

    $colname2_Recordset1 = "2004";

    if (isset($_GET['ano'])) {

    $colname2_Recordset1 = (get_magic_quotes_gpc()) ? $_GET['ano'] : addslashes($_GET['ano']);

    }

    mysql_select_db($database_teste, $teste);

    $query_Recordset1 = sprintf("SELECT * FROM noticias WHERE mes >= %s ORDER BY id DESC", $colname_Recordset1);

    $Recordset1 = mysql_query($query_Recordset1, $teste) or die(mysql_error());

    $row_Recordset1 = mysql_fetch_assoc($Recordset1);

    $totalRows_Recordset1 = mysql_num_rows($Recordset1);

    ?>

    This is the piece of the code where I want to put the change of colors of the rows in the table that displays the results... does it help?

    Thank you again... if you ever need anything about dvdbackup... just ask (that is really my area)
    Last edited by Rockas; 05-24-2004 at 11:46 PM.
    Rockas Software - Free applications

    Warning!!!
    Too much sex is not healthy! Causes amnesia and some other stuff that I can't remember!!!

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    your code doesn't loop through the recordset. It only stores the first row indside an associative array.

    You need to use a while loop to cycle through the recordset.

    PHP Code:
    $Recordset1 mysql_query($query_Recordset1$teste) or die(mysql_error());
    if (
    mysql_num_rows($Recordset1) >= 1){
      
    $i=0
      while (
    $row=mysql_fetch_assoc($Recordset1)){ // supposing you have $result as resource ID for the recordset 
        
    $i ++; 
        
    $backgr='<tr class="noteven">';    
        if (
    $i == 0){ 
          
    $backgr='<tr class="even">'
        } 
        echo 
    $backgr '<td> ....blabla ...</td></tr>'
      }
    } else {
      echo 
    'No records found.';

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #9
    New Coder
    Join Date
    May 2004
    Location
    Portugal
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm a dumb ***... I just can't put it to work... should I give up?
    Rockas Software - Free applications

    Warning!!!
    Too much sex is not healthy! Causes amnesia and some other stuff that I can't remember!!!

  • #10
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    perhaps if you posted the entire code including the loop one would be able to give you a functioning code back.

    styleclasses[0]="class1";styleclasses[1]="class2";
    for($i=0;$i<$totalRows_Recordset1;$i++)
    {
    $row=mysql_fetch_array($Recordset1,MYSQL_ASSOC);
    $tr='<tr class="'.styleclasses[($i%2)].'">';

    /*make the cols your self*/
    $tr.='<td>'.$row['name'].'</td>';

    $tr.='</tr>';
    print($tr);
    }

  • #11
    New Coder
    Join Date
    May 2004
    Location
    Portugal
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your help.
    My code is a little of PHP wich collects data from my database and the rest is pure HTML, anyway here goes the entire code... HTML included.
    ****************************************************
    <?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?><?php require_once('Connections/teste.php'); ?>

    <?php

    $colname_Recordset1 = "5";

    if (isset($_GET['mes'])) {

    $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_GET['mes'] : addslashes($_GET['mes']);

    }

    $colname2_Recordset1 = "2004";

    if (isset($_GET['ano'])) {

    $colname2_Recordset1 = (get_magic_quotes_gpc()) ? $_GET['ano'] : addslashes($_GET['ano']);

    }

    mysql_select_db($database_teste, $teste);

    $query_Recordset1 = sprintf("SELECT * FROM noticias WHERE mes >= %s ORDER BY id DESC", $colname_Recordset1);

    $Recordset1 = mysql_query($query_Recordset1, $teste) or die(mysql_error());

    $row_Recordset1 = mysql_fetch_assoc($Recordset1);

    $totalRows_Recordset1 = mysql_num_rows($Recordset1);

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <title>NEWS</title>

    <link rel="stylesheet" type="text/css" href="style.css">

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script language="JavaScript1.1">
    <!--

    //*****************************************
    // Blending Image Slide Show Script-
    // © Dynamic Drive (www.dynamicdrive.com)
    // For full source code, visit http://www.dynamicdrive.com/
    //*****************************************

    //specify interval between slide (in mili seconds)
    var slidespeed=3000

    //specify images
    var slideimages=new Array("images/adver_1.jpg","images/adver_2.jpg","images/adver_3.jpg","images/adver_4.jpg")

    //specify corresponding links
    var slidelinks=new Array("../../contact_info.htm")

    var newwindow=0 //open links in new window? 1=yes, 0=no

    var imageholder=new Array()
    var ie=document.all
    for (i=0;i<slideimages.length;i++){
    imageholder[i]=new Image()
    imageholder[i].src=slideimages[i]
    }

    function gotoshow(){
    if (newwindow)
    window.open(slidelinks[whichlink])
    else
    window.location=slidelinks[whichlink]
    }

    //-->
    </script>

    <style type="text/css">
    <!--
    .style1 {
    font-size: 12pt;
    font-weight: bold;
    color: #0066CC;
    }
    .style2 {color: #990000}
    .style7 {font-size: 12px}
    .style8 {
    font-size: 11px;
    color: #333333;
    font-weight: bold;
    }
    -->
    </style>
    </head>



    <body leftmargin="1" topmargin="1" marginwidth="1" marginheight="1">

    <table width="99%" height="1" border="0" align="center">

    <tr>

    <td width="95%" align="center" valign="top"><table width="100%" height="89" cellpadding="1" cellspacing="4">

    <tbody>

    <tr>

    <td valign="top" colspan="5" height="1"> <table cellspacing="4" cellpadding="0" width="100%" border="0">

    <tbody>

    <tr>

    <td width="30%" valign="top"><b>Bem-vindo</b></td>

    <td width="64%" valign="top"><h5><font face="Verdana, Arial, Helvetica, sans-serif" color="#333333"><b><img src="images/new.gif" width="26" height="9" /></b></font> J&aacute;
    temos um Newsletter em portugu&ecirc;s... inscreva-se
    <a href="http://doom9.layernetworks.org/mailman/listinfo/mirrorpt_doom9.layernetworks.org">aqui</a>.</h5></td>

    <td width="6%"><b>&lt;&lt;</b></td>

    </tr>

    <tr>

    <td bgcolor="#999999" colspan="3"

    height="1"></td>

    </tr>

    </tbody>

    </table>

    Bem-vindo ao Doom9.net Portugu&ecirc;s e Brasileiro [Estamos a precisar

    de <a href="../tradutores.htm">Tradutores</a> para os guias]</td>

    </tr>

    </tbody>

    </table>

    <table width="100%" border="0" align="center" cellspacing="4">
    <tr bgcolor="#a2c0df" class="row1">
    <td width="5%"><font

    face="Verdana, Arial, Helvetica, sans-serif"

    color="#ffffff"><b>Data</b></font></td>
    <td width="95%" bgcolor="#a2c0df"><font

    face="Verdana, Arial, Helvetica, sans-serif"

    color="#ffffff"><b>Not&iacute;cias</b></font></td>
    </tr>
    <tr class="row1">
    <td bgcolor="#CCCCCC">&nbsp;</td>
    <td width="95%" bgcolor="#CCCCCC"><p class="justify"><span class="style1">Aviso!!!</span><br />
    <span class="style2">Aos utilizadores do servi&ccedil;o de mail &quot;<strong>xxxx@bol.com.br</strong>&quot;.</span> <br>
    Se desejarem registar-se no f&oacute;rum, n&atilde;o poder&atilde;o usar esse
    endere&ccedil;o, j&aacute; que existe uma incompatibilidade (n&atilde;o permite
    a sua activa&ccedil;&atilde;o), entre o nosso sistema e o referido servi&ccedil;o
    de e-mail, &agrave; qual somos completamente alheios. J&aacute; que, at&eacute; agora,
    esse &eacute; o &uacute;nico que nos tem dado problemas, partimos do princ&iacute;pio
    que o erro &eacute; provocado por eles e sendo assim nada podemos fazer para
    corrigir a situa&ccedil;&atilde;o. Assim, somos obrigados a pedir-vos, que
    subscrevam uma outra conta de e-mail para poderem efectuar o vosso registo.<br>
    Obrigado.</td>
    </tr>
    <?php do { ?>
    <tr align="left" leftmargin="10" valign="top" bgcolor="#E9E9E9" class="style7">
    <td width="5%"><p align="center" class="postbody"><?php echo $row_Recordset1['dia']; ?>/<?php echo $row_Recordset1['mes']; ?></p></td>
    <td height="50" colspan="-4" bgcolor="#E9E9E9"><p class="justify"><?php echo urldecode($row_Recordset1['noticia1']); ?> </p></td>
    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
    </table>

    <table width="100%" border="0" align="center" cellspacing="5">

    <tr align="left" valign="top">

    <td bgcolor="#CCCCCC"><a href="arquivo.htm">Arquivo de Not&iacute;cias

    </a>- <strong>aqui poder&aacute; encontrar as not&iacute;cias anteriores

    ao m&ecirc;s corrente.</strong></td>

    </tr>

    </table>



    </td>

    <td width="142" rowspan="2" valign="top"> <table border="0" width="141">

    <tr bgcolor="#DADADA">

    <td width="141"><b>Importante</b></td>

    </tr>

    <tr bgcolor="#EEEEEE">

    <td width="141"><p><font face="Verdana, Arial, Helvetica, sans-serif">Antes de usar

    esse site você deverá ler e entender por inteiro o aviso legal.</font></p>

    <p><font face="Verdana, Arial, Helvetica, sans-serif">[<a href="../disclaimer.htm">Leia

    agora</a>]</font></p></td>

    </tr>

    </table>

    <table border="0" width="141">

    <tr bgcolor="#DADADA">

    <td><font face="Verdana, Arial, Helvetica, sans-serif" color="#333333"><strong>

    Tradutores</strong></font></td>

    </tr>

    <tr bgcolor="#EEEEEE">

    <td><p><a href="javascript:gotoshow()"><img src="images/adver_1.jpg" name="slide" border=0 style="filter:blendTrans(duration=3)" width=130 height=177 /></a>
    <script language="JavaScript1.1">
    <!--

    var whichlink=0
    var whichimage=0
    var blenddelay=(ie)? document.images.slide.filters[0].duration*1000 : 0
    function slideit(){
    if (!document.images) return
    if (ie) document.images.slide.filters[0].apply()
    document.images.slide.src=imageholder[whichimage].src
    if (ie) document.images.slide.filters[0].play()
    whichlink=whichimage
    whichimage=(whichimage<slideimages.length-1)? whichimage+1 : 0
    setTimeout("slideit()",slidespeed+blenddelay)
    }
    slideit()

    //-->
    </script>
    </p>

    <p><font face="Verdana, Arial, Helvetica, sans-serif">[<a href="../contact_info.htm" target="_self">Clique

    aqui</a>]</font></p></td>

    </tr>

    </table>

    <table border="0" width="141">

    <tr bgcolor="#DADADA">

    <td width="130"><font face="Verdana, Arial, Helvetica, sans-serif" color="#333333"><b>
    <img src="images/new.gif" width="26" height="9" /> Newsletter</b></font></td>

    </tr>

    <tr bgcolor="#EEEEEE">

    <td width="130"><p>Increva-se para receber a newsletter (notícias por email) do
    site Doom9 bi-semanalmente. As novidades são resumidas e você saberá
    quando os guias forem actualizados.</p>

    <p>[<a href="http://doom9.layernetworks.org/mailman/listinfo/mirrorpt_doom9.layernetworks.org">Inscreva-se

    já</a>]</p></td>

    </tr>

    </table></td>

    </tr>

    <tr>

    <td align="center" valign="top"><p align="center">A duplicação dos links ou

    conteúdo é estritamente proibida.<br />

    (c) Doom9 Networks 2000 - 2004</p>

    <p align="center" style="margin-top: 0; margin-bottom: 0">Hospedagem by

    <a href="mailto:layer@layer.biz?subject=doom9 mirror">layernetworks</a></p>

    <p align="center" style="margin-top: 0">Equipa Técnica: <a href="mailto:layer@layer.biz?subject=doom9%20mirror">Layer</a><br />
    Tradução dos textos:<br />
    <a href="mailto:xslashx@terra.com.br">_XSlashX_</a> - Redin - Arrakis_spk
    - <a href="mailto:maneltimtim@msn.com?subject=doom9%20mirror">Rockas </a>-
    efvb - Sh4D0ww4Lk3R<br />
    (voluntários em <a href="looking_for_staff.htm">ajudar</a> na tradução
    serão muito bem vindos!)</p>
    <p align="center">&nbsp;</p>
    <p align="center"><span class="COPYRIGHT">PHP Code (v.1.0) developed by
    The Portuguese Doom9.net team
    (Rockas, Redin and Layer)<br />
    (c) CopyRight 2004</span></p></td>

    </tr>

    </table>

    </body>

    </html>

    <?php

    mysql_free_result($Recordset1);

    ?>
    *************************************
    the result of this can be seen at http://doom9.layernetworks.org/

    Thank you
    Last edited by Rockas; 05-25-2004 at 01:17 PM.
    Rockas Software - Free applications

    Warning!!!
    Too much sex is not healthy! Causes amnesia and some other stuff that I can't remember!!!

  • #12
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Find the right lines to change 178 to 202 I believe would be close and then use this bit:

    Code:
    <?php do { 
    $i ++; 
       $backgroundColor ='#EAEAEA';    
       if ($i % 2 == 0){ 
          $backgroundColor ='#E9E9E9'; 
       } 
    
    
    
    ?>
    
    	<tr align="left" leftmargin="10" valign="top" bgcolor="<?php print $backgroundColor;?>" class="style7"> 
    	<td width="5%">
    	<p align="center" class="postbody">
    <?php echo $row_Recordset1['dia']; ?>/
    <?php echo $row_Recordset1['mes']; ?>
    	</p>
    	</td>
    	<td height="50" colspan="-4" bgcolor="#E9E9E9">
    	<p class="justify">
    <?php echo urldecode($row_Recordset1['noticia1']); ?> 
    	</p>
    	</td>
    	</tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
    missing closing PHP tag fixed!!
    Last edited by carl_mcdade; 05-26-2004 at 08:27 AM.

  • #13
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you have this segment in your code:

    <?php do { ?>
    <tr align="left" leftmargin="10" valign="top" bgcolor="#E9E9E9" class="style7">
    <td width="5%"><p align="center" class="postbody"><?php echo $row_Recordset1['dia']; ?>/<?php echo $row_Recordset1['mes']; ?></p></td>
    <td height="50" colspan="-4" bgcolor="#E9E9E9"><p class="justify"><?php echo urldecode($row_Recordset1['noticia1']); ?> </p></td>
    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

    replace with:

    <?php
    $styleclasses[0]="#E9E9E9";$styleclasses[1]="#9E9E9E";
    do {
    $tr='<tr align="left" leftmargin="10" valign="top" bgcolor="'.$styleclasses[($i%2)].'" class="style7"><td width="5%"><p align="center" class="postbody">'.$row_Recordset1['dia'].'/'.$row_Recordset1['mes'].'</p></td>
    <td height="50" colspan="-4" bgcolor="#E9E9E9"><p class="justify">'.urldecode($row_Recordset1['noticia1']).'</p></td>
    </tr>';
    print($tr);
    } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

  • #14
    New Coder
    Join Date
    May 2004
    Location
    Portugal
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you all but it isn't working yet.
    The example of Garadon changes the colors of the columns but... I want to change the colors of the rows

    The example of Carl_mcdade just returns me an error:
    Parse error: parse error, unexpected '<' in /home/layer/public_html/doom9/noticias/news.php on line 162

    Can you give me another help?

    Thank you very much
    Rockas Software - Free applications

    Warning!!!
    Too much sex is not healthy! Causes amnesia and some other stuff that I can't remember!!!

  • #15
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how about this then?
    I removed a bgColor that you had on one of the td's, and just a note i don't think setting colspan to negative do anything.
    PHP Code:
    <?php
    $styleclasses
    [0]="#E9E9E9";$styleclasses[1]="#9E9E9E";
    do {
    $tr='<tr align="left" leftmargin="10" valign="top" bgcolor="'.$styleclasses[($i%2)].'" class="style7"><td width="5%"><p align="center" class="postbody">'.$row_Recordset1['dia'].'/'.$row_Recordset1['mes'].'</p></td>
    <td height="50" colspan="-4"><p class="justify">'
    .urldecode($row_Recordset1['noticia1']).'</p></td>
    </tr>'
    ;
    print(
    $tr);
    } while (
    $row_Recordset1 mysql_fetch_assoc($Recordset1)); ?>


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •