every 2nd record<

liunx

Guest
i've got a php/mysql driven page and i want to have every 2nd row to be of a different background kinda like

<table>
<tr style="background-color: #006;"><td></td></tr>
<tr style="background-color: #009;"><td></td></tr>
<tr style="background-color: #006;"><td></td></tr>
<tr style="background-color: #009;"><td></td></tr>
</table>

and so on... how can i do that?

thxThis is a variation of what I do in ASP and should work. Hopefully I didn't miss any punctuation...)

//outside of loop
$bgcolor = "000066";

//then inside loop

if ($bgcolor == "000099") {
$bgcolor = "000066";
} else {
$bgcolor = "000099";
}
echo "<tr style=\"background-color: #".$bgcolor."\"><td></td></tr>";wow... no... that's not what i meant.

<table>
<?
$query = mysql_query("select....");
$numrow = mysql_num_rows($query);

while ($row = mysql_fetch_array($query)) {
...blabla...
}
?>
</table>

now where the blabla is i'm assuming there's something like

if ($numrow.....) {
<tr style="background-color: #999;>
} else {
<tr style="background-color: #ccc;>
}

!?!?Seems to me that is a variation of the same thing. You put the if statement around the rownumber and display a different <tr> tag based that. I've created a variable for the bg color that gets changed each time through the loop and have the <tr> tag evaluate the variable.

Perhaps your looking for:

if (fmod($numrow, 2) != 0) {
<tr style="background-color: #999;>
} else {
<tr style="background-color: #ccc;>
}

If it's an even number through the loop, it'll be 0, otherwise it won't.ok... now we're on the right track... although numrow is not what i'm looking for it seems.

what i need i guess is the row number of each... like

while (..) {
if fmod(thisrownumber,2) {
}
}

or something like that??! am i writing in chinese yet!? :Dfmod?
Uhm, just add a counter to the loop. If it's uneven, make it one colour, if it's even, another colour...

$bgcolor = ($i % 2 == 0) ? "006" : "009";

Or am I not understanding something? I thought bassrek had it right in the first post.. hmOriginally posted by Rydberg
fmod?
Uhm, just add a counter to the loop. If it's uneven, make it one colour, if it's even, another colour...

$bgcolor = ($i % 2 == 0) ? "006" : "009";

Or am I not understanding something? I thought bassrek had it right in the first post.. hm

I agree,there are several ways to do this...but bassrek's first post had just as viable a method as any other that could be suggested.Something like this?


<table>
<?

$connection = mysql_connect("localhost", "username", "password")
or die (mysql_error());

$query = mysql_query("select...");
$numrow = mysql_num_rows($query);


for ($i = 0; $i < $numrow; $i++) {
echo "<tr style=]"background-color: $background\"><td>"
if ($i % 2)
$background = "#006";
else
$background = "#009";
echo "</tr></td>";
}

mysql_close($connection);

?>

</table>




I haven't tested this and I'm a pretty novice PHP developer so dont laugh. What should happen is the beef of the code being the for loop will continue to run for as many rows as you have, performing a $i modulus 2 calculation on each row number. If the modulus (remainder of a division) returns 1 your certain background color will show up, or else the other background will display (if the modulus calculation returns 0). Good luck, and I would actually appreciate if you told me whether this snippet worked or not because if it does I'm going to save it.^ yes... now that's more like it... but i don't think what i'm looking for is $i < $numrow 'cause $numrow is actually the total amount of rows therefore it's always returning the same value

there's a total of 3 rows so it's always returning 1 'cause 3 % 2 = 1 so... somehow i need to get THE row !?!

so far... here's my code


<?
$result = mysql_query ("select * from membres order by mid");
$numrow = mysql_num_rows($result);
?>

<table width="100%" cellpadding="3" cellspacing="1" border="0" style="background-color: #333;">

<?while ($row = mysql_fetch_array ($result)) {
for ($i = 0; $i < $numrow; $i++) {
if ($i % 2) {?>
<tr style="background-color: #006;">
<?} else {?>
<tr style="background-color: #007;">
<?}
}?>
<td><?=$row['nom']?></td></tr>
<?}?>
</table>


thx for all the help guys!<?
$result = mysql_query("select * from membres order by mid");
?>
<table width="100%" cellpadding="3" cellspacing="1" border="0" style="background-color: #333;">
<?
for ($i = 0; $row = mysql_fetch_array($result); $i++) {
if ($i % 2 == 0) {
echo "<tr style=\"background-color: #006;\">";
} else {
echo "<tr style=\"background-color: #007;\">";
}
echo "<td>{$row['nom']}</td></tr>";
}
?>
</table>


how about that?YES YES YES... that worked!!

finally... thx everyone! :)you guys are doing this the hard way. ;)

<?
$result = mysql_query("select * from membres order by mid");
?>
<table width="100%" cellpadding="3" cellspacing="1" border="0" style="background-color: #333;">
<?
$color = "#006";
while ($row = mysql_fetch_array($result)){
$color = ($color == "#007")?"#006":"#007";
echo "<tr style=\"background-color: $color\">";
echo "<td>{$row['nom']}</td></tr>";
}
?>
</table>Originally posted by scoutt
you guys are doing this the hard way. ;)

You are kidding, I presume? You just made your own mix of the already given solutions, and it's not any easier or harder than the others.less code is always easier. that is the purpose of php, code in as less lines as possible, but sometimes you can't.

sure it does the samething but it uses less lines which in turn makes the files size smaller which in turn makes it run faster.

so yes I am serious. and yes why use a modulus if you don't have to.If I've learned one thing in my day... never mess with a guru!! :cool:

I learned alot too thanks guysOriginally posted by scoutt
less code is always easier. that is the purpose of php, code in as less lines as possible, but sometimes you can't.

sure it does the samething but it uses less lines which in turn makes the files size smaller which in turn makes it run faster.

so yes I am serious. and yes why use a modulus if you don't have to.

Following your logic, we might all as well delete all unnecessary white space we've ever written into a PHP file, since it slows down the processing so much!

This wasn't about that anyway. Bassrek already gave the solution in the first post he made, but karinne didn't understand it(no offense). She required further explinations, and that's what we tried to give her. The idea of modulus came up, and since she understood the concept, we all developed it into a working example. I would say that my example is more intuitive than your mix of bassrek's solution and my first. Not many people know how the ternary operator works.

How do you know that it's faster? Do you have such a deep understanding of the PHP core, that you can just blurt that out? I'm not saying that I know that it isn't, I'm just doubting it. Reading the file from the harddrive is peanuts, it doesn't matter if there are 50 characters more. Does that take 2 a microsecond extra? You're passing around strings on the stack - isn't that normally a performance issue? An int performs a lot faster. You're comparing strings - that takes a lot more overhead than ints. Your output string needs to be parsed - mine doesn't, I could put it in single quotes. I might have two 'echos' but those are accessed in nanoseconds in the RAM. Think about it, which is faster? To parse and reconstruct your string, and then access it from the RAM, or to just take it right out from the RAM in a few ns? There are lots of things like that to think about, before you think you know what's faster. I think they would differ very little, if at all. Such small details can be pretty unpredictable. The database connection and data retrieval will be the 'bottleneck' in this script anyway. The only way we could know, would be to benchmark it a few thousand times, without the database connection. And even then, maybe there would only be a few microseconds difference. BIG DEAL! Truth is, nobody cares. She's got a solution that works just fine, beyond that, nobody gives a damn.

Rydberg out!well apparently you do. get your feathers in a up roar there Rydberg? you take everything to serious. you leave for a few months and come back and all of a sudden you are a php god? come off your high horse for a second. so what if I gave the same solution, it was mine to give. and I am not doubting your skills and you maybe right about comparing might just take that much longer to process. BUT, I have cut files down by k's just by doing such things and they run that much faster compared to their counterparts. we can sit here all day comparing code but it won't get anywhere. to tell you the truth I didn't even see bassrek's code.

and yes your lines had to be parsed anyway. anytime you have it in a echo or print it gets parsed so it was no different then mine. I have used the modulus and like you said, there is no difference in speed compared to a compare statement. unless of course you have a mile long code that does just compares.

and you know as well as I do that I never said anything about white space, just code.

now calm down Rydberg and take a breathe.
 
Back
Top