...

View Full Version : Is it possible?



Rockas
05-24-2004, 04:44 AM
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

firepages
05-24-2004, 10:18 AM
not knowing how you build your table ... its a guess but logic may look something similar to ..



<?
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>

raf
05-24-2004, 11:18 AM
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


$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 % 2 == 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.

bcarl314
05-24-2004, 03:06 PM
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.

firepages
05-24-2004, 04:47 PM
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 .

raf
05-24-2004, 05:22 PM
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 :D

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

Rockas
05-25-2004, 12:44 AM
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) :p

raf
05-25-2004, 01:03 AM
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.



$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 % 2 == 0){
$backgr='<tr class="even">';
}
echo $backgr . '<td> ....blabla ...</td></tr>';
}
} else {
echo 'No records found.';
}

Rockas
05-25-2004, 01:26 PM
I'm a dumb ***... I just can't put it to work... should I give up? :confused:

Garadon
05-25-2004, 01:51 PM
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);
}

Rockas
05-25-2004, 02:15 PM
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

carl_mcdade
05-25-2004, 03:01 PM
Find the right lines to change 178 to 202 I believe would be close and then use this bit:


<?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!!

Garadon
05-25-2004, 03:29 PM
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)); ?>

Rockas
05-26-2004, 02:09 AM
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

Garadon
05-26-2004, 07:48 AM
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
$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)); ?>

carl_mcdade
05-26-2004, 09:28 AM
:eek: fixed the missing tag in the above code

Rockas
05-26-2004, 01:50 PM
Thank you again.
I know that i am becoming a real pain in the **** but it just don't work here is how I wrote the code:

*****
<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="#dadada">&nbsp;</td>
<td width="95%" bgcolor="#dadada"><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 registrar-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 registro.<br>
Obrigado.</td>
</tr>
<?php
$styleclasses[0]="#dadada";$styleclasses[1]="#eeeeee";
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)); ?>
</table>
****
and it prints all rows with the same color.

the result is here http://doom9.layernetworks.org/

thanx

Rockas
05-26-2004, 02:08 PM
AAAAAAAALLLLLLLLLLLLLLLEEEEEEEEEEELLLLLLLLLUUUUUUUUUUIIIIIIIIIIAAAAA

THANK YOU ALL

I almost commited suicide the correction made by carl_mcdade on his code made it work. I own you one man.

Thank you Garadon as well for your patience.

Do you give me the permission to put your names at the borrom of my page as a recognition of your help?

Anyway THANX

Garadon
05-26-2004, 02:26 PM
I am just curious are u the person behind doom9?

Rockas
05-26-2004, 11:32 PM
I'm one of the ones that are behind the portuguese doom9... but you didn't answer my question :)

Thanx again.

carl_mcdade
05-27-2004, 01:55 AM
I missed that. Sure, put my name down. I'll take all the publicity I can get :thumbsup:

Rockas
05-27-2004, 02:04 AM
Can you help me with another question?

I have a similar page that I use as a search page that collects the search data from a form on another frame. The php code is:

<?php require_once('Connections/teste.php'); ?>
<?php
$colname_Recordset1 = "1";
if (isset($_GET['noticia1'])) {
$colname_Recordset1 = (get_magic_quotes_gpc()) ? $_GET['noticia1'] : addslashes($_GET['noticia1']);
}
mysql_select_db($database_teste, $teste);
$query_Recordset1 = sprintf("SELECT dia, mes, ano, noticia1 FROM noticias WHERE noticia1 LIKE '%%%s%%' ORDER BY mes DESC, dia 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);
?>


Now my problem is this. When I want to search for the word (ex. XviD) i have to write exactly as it is on the Data Base. So if I submit XviD it finds all the records where it appears but if I submit xvid it does'nt return any data. Is there a way to correct this?

Thanx.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum