Display rows in diferent colors

Discussion in 'PHP' started by piropeator, Apr 30, 2010.

  1. #1
    What is the error ??
    DB: tienda
    Table: producto
    //-----------------------
    <table width="300" height="400"> 
    <?php
    $color="#CCCCCC"; 
    mysql_connect ('localhost', 'user', 'password') ; 
    mysql_select_db ('tienda'); 
    $resultado = mysql_query (SELECT * FROM producto); 
    if ($consulta = mysql_fetch_array($resultado)) { 
    for ($i = 0; i < count($consulta); $i++) { 
    if ($i % 2) { 
    echo "<tr bgcolor=#cccccc>"; 
    } else { 
    echo "<tr bgcolor=#999999>"; 
    } 
    } 
    ?> 
    <td><? echo $consulta["producto"]; ?></td> 
    </tr> 
    </table> 
    <? } 
    ?>
    
    Code (markup):
    :confused:
     
    piropeator, Apr 30, 2010 IP
  2. VarriaStudios

    VarriaStudios Member

    Messages:
    246
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #2
    assigned a css class instead of bgcolor
     
    VarriaStudios, Apr 30, 2010 IP
  3. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #3
    try

    
    <table width="300" height="400"> 
    <?php
    $color="#CCCCCC"; 
    mysql_connect ('localhost', 'user', 'password') ; 
    mysql_select_db ('tienda'); 
    $resultado = mysql_query (SELECT * FROM producto); 
    [COLOR="#ff0000"]$i=0;
    while($consulta = mysql_fetch_array($resultado)) { [/COLOR]
    	if ($i % 2) { 
    		echo "<tr bgcolor=#cccccc>"; 
    	} else { 
    		echo "<tr bgcolor=#999999>"; 
    	} 
    ?> 
    <td><? echo $consulta["producto"]; ?></td> 
    </tr> 
    </table> 
    [COLOR="#ff0000"]++$i;[/COLOR]
    <? } 
    ?>
    
    Code (markup):
    I marked the new parts with red
     
    n3r0x, Apr 30, 2010 IP
  4. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #4
    The code show this:

    Parse error: syntax error, unexpected T_STRING in C:\www\prueba_rows.php on line 6
     
    piropeator, Apr 30, 2010 IP
  5. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #5
    line 6: $resultado = mysql_query (SELECT * FROM producto);

    that´s the way you posted it.. should be

    $resultado = mysql_query ("SELECT * FROM producto");
     
    n3r0x, Apr 30, 2010 IP
  6. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #6
    it doesn't work!!

    look the image down.

    This is my table:

    CREATE TABLE IF NOT EXISTS `producto` (
      `id_producto` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `id_categoria` int(10) unsigned NOT NULL,
      `nombre` varchar(20) DEFAULT NULL,
      `descripcion` varchar(64) DEFAULT NULL,
      `precio` decimal(10,0) DEFAULT NULL,
      `stock` int(10) unsigned DEFAULT NULL,
      `importancia` int(10) unsigned DEFAULT NULL,
      `imagen` varchar(20) DEFAULT NULL,
      PRIMARY KEY (`id_producto`),
      KEY `id_categoria` (`id_categoria`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ;
    
    INSERT INTO `producto` (`id_producto`, `id_categoria`, `nombre`, `descripcion`, `precio`, `stock`, `importancia`, `imagen`) VALUES
    (1, 3, 'Osito Hugo con globo', '20 cm. felpa marron corbata azul toko', '50', 20, 4, 'oso_hugo_ch.jpg'),
    (2, 1, 'Arreglo Princesa', 'Rosas y claveles jarron vidrio', '85', 10, 3, 'flor_prince_ch.jpg'),
    (6, 3, 'Cojín Monito', 'Terciopelo relleno con espuma', '85', 15, 3, 'cojin_ch.jpg'),
    (7, 4, 'Aretes Sea Star', 'Estrella de mar con peces y estrellas plata', '100', 10, 1, 'arete_seastar_ch.jpg'),
    (10, 2, 'Zapatos Perla', 'Cuero perlado, taco 7 orificio delantero', '75', 15, 1, 'zapatos_perla_ch.jpg'),
    (9, 4, 'Collar Sunset', 'Concha de Abanico y oro', '45', 20, 3, 'collar_sunset_ch.jpg'),
    (11, 2, 'Zapatos Black Star', 'Cuero negro con perlas incrustadas', '150', 10, 1, 'zapato_bkstr_ch.jpg'),
    (12, 2, 'Zapatos Suecos Limón', 'Lona calada fresca y base de goma', '85', 20, 1, 'zapato_sueco_ch.jpg'),
    (14, 1, 'Clavel Negro', 'Clavel Negro en caja dorada', '10', 15, 4, ''),
    (18, 4, 'Arete Chullo', 'Fantasía fina con aplicaciones de plata', '13', 20, 1, 'aretes_chullo_ch.jpg'),
    (16, 3, 'Parlantes', 'Cuadrafonicos', '150', 2, 2, ''),
    (17, 2, 'Zapatillas Nike Gold', 'Doradas con aplicaciones', '400', 15, 1, 'no_disponible_ch');
    
    PHP:
    :confused::confused:
     

    Attached Files:

    piropeator, Apr 30, 2010 IP
  7. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #7
    Ah lol sorry was half sleeping while putting that together..

    
    <table width="300" height="400"> 
    <?php
    $color="#CCCCCC"; 
    mysql_connect ('localhost', 'user', 'password') ; 
    mysql_select_db ('tienda'); 
    $resultado = mysql_query ("SELECT * FROM producto"); 
    $i=0;
    while($consulta = mysql_fetch_array($resultado)) { 
    	if ($i % 2 == 0) { 
    		echo "<tr bgcolor=#cccccc>"; 
    	} else { 
    		echo "<tr bgcolor=#999999>"; 
    	} 
    ?> 
    <td><? echo $consulta["producto"]; ?></td> 
    </tr> 
    </table> 
    
    <? 
    ++$i;
    } 
    ?>
    
    Code (markup):
     
    n3r0x, Apr 30, 2010 IP
  8. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #8
    Now all screen is grey.
    What mean this line??: <td><? echo $consulta["producto"]; ?></td>
    I think this is a mistake.:confused::confused:
     
    piropeator, Apr 30, 2010 IP
  9. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #9
    you should know since you wrote it... :p
     
    n3r0x, Apr 30, 2010 IP
  10. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #10
    Like n3rox said you should know as you wrote it, but looking at your db structure in post #6 producto is your table name, and their is'nt a column named that, so it would'nt work because of that.
     
    danx10, Apr 30, 2010 IP
  11. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    $bgcol[0]="class='color1'";
    $bgcol[1]="class='color2'"; 
    
    for ($i = 0; [COLOR="red"]$[/COLOR]i < count($consulta); $i++) {
    	echo "<tr ".$bgcol[fmod($i,2)].">\n";
    	// echo everything you need
    	echo "</tr>\n";
    }
    Code (markup):
    Now all you need to do is make 2 classes (color1 and color2) in your css file and done.
     
    fex, May 2, 2010 IP
  12. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #12
    I ask you about this part of code because I don't wrote this, I try to modify this, I don't know what mean this part exactly.
    That is the reason. :)
     
    piropeator, May 6, 2010 IP