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):
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
The code show this: Parse error: syntax error, unexpected T_STRING in C:\www\prueba_rows.php on line 6
line 6: $resultado = mysql_query (SELECT * FROM producto); that´s the way you posted it.. should be $resultado = mysql_query ("SELECT * FROM producto");
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:
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):
Now all screen is grey. What mean this line??: <td><? echo $consulta["producto"]; ?></td> I think this is a mistake.
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.
$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.
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.