Ninjakreborn Posted April 19, 2007 Share Posted April 19, 2007 <?php $selecttrans = "SELECT * FROM transactions WHERE userid = '" . $_SESSION['cymember'] . "' AND status <> 'Pending' ORDER BY submitted DESC;"; $querytrans = mysql_query($selecttrans); $color = "yes"; while ($rowtrans = mysql_fetch_array($querytrans)) { ?> <tr <?php if ($color == "yes") { echo 'bgcolor="#eeeeee"'; }?>> <td>10/30/06</td> <td>cy52860231015</td> <td>Deposit</td> <td> </td> <td>$500.00</td> <td><b>$1,677.65</b></td> </tr> <?php } ?> This is based on PHP. What I am trying to figure out is how to get one row to display background color and the next to not. All the way through the list. Any advice? That is what I have so far, but I can't seemed to get it to just do what I want it to do. Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/ Share on other sites More sharing options...
MadTechie Posted April 19, 2007 Share Posted April 19, 2007 <?php $selecttrans = "SELECT * FROM transactions WHERE userid = '" . $_SESSION['cymember'] . "' AND status <> 'Pending' ORDER BY submitted DESC;"; $querytrans = mysql_query($selecttrans); $color = true; while ($rowtrans = mysql_fetch_array($querytrans)) { ?> <tr <?php if ($color) { echo 'bgcolor="#eeeeee"'; $color = !$color; }?>> <td>10/30/06</td> <td>cy52860231015</td> <td>Deposit</td> <td> </td> <td>$500.00</td> <td><b>$1,677.65</b></td> </tr> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/#findComment-233353 Share on other sites More sharing options...
rcorlew Posted April 19, 2007 Share Posted April 19, 2007 you should use css and two different td id types like so: In your head do this with a text/css style layout <style type="text/css"> <?php if ($color == "yes") { echo " #yesColor td\{ background-color\: \#eeeeee\; \} "; } ?> </style> And then the table should layout nicely, notice that if no color is chosen the table will have no color at all <tr> <td id ="noColor">10/30/06</td> <td id="yesColor">cy52860231015</td> <tdid="noColor">Deposit</td> <td id="yesColor"> </td> <td id="noColor">$500.00</td> <td id="yesColor"><b>$1,677.65</b></td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/#findComment-233356 Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Author Share Posted April 19, 2007 Hmm, the formatting will help me format better in tighter situations, however I am faced with the same problem. Looking at the login behind it, it has to be Colored row Non-colored row Colored row Non-colored row all the way down. I am just wondering if there is an easier way without getting down and starting to do calculations to check even and odd numbers and all that other stuff, for something like just alternating row's, because I also have to build a pagination system into this, but it's not a normal one. It's going to have a bunch of features into it, like a select box for the pages, and single page prev-next and jump to the last row, first row. you should use css and two different td id types like so: In your head do this with a text/css style layout <style type="text/css"> <?php if ($color == "yes") { echo " #yesColor td\{ background-color\: \#eeeeee\; \} "; } ?> </style> And then the table should layout nicely, notice that if no color is chosen the table will have no color at all <tr> <td id ="noColor">10/30/06</td> <td id="yesColor">cy52860231015</td> <tdid="noColor">Deposit</td> <td id="yesColor"> </td> <td id="noColor">$500.00</td> <td id="yesColor">$1,677.65</td> </tr> Yes, and I have done this before to get "alternating color cells", however in this situation it has to be the actual rows that alternate in color. I saw some stuff on really advanced calculations and checking for the odd/even number, but I want to keep it even simpler than that if possible. So with all of the code I know I am going to have to add to this, I wanted to keep this specific step, as light as possible. Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/#findComment-233359 Share on other sites More sharing options...
bsprogs Posted April 19, 2007 Share Posted April 19, 2007 What I did for this was create an is_even function. <?php function is_even($num) { return (is_numeric($num)&(!($num&1))); } ?> Then I placed the code in a for loop <?php $selecttrans = "SELECT * FROM transactions WHERE userid = '" . $_SESSION['cymember'] . "' AND status <> 'Pending' ORDER BY submitted DESC;"; $querytrans = mysql_query($selecttrans); $rowcount = mysql_num_rows($querytrans); for ($x=1; $x<=$rowcount; $x++) { $row = mysql_fetch_array($result); if (is_even($x)) {?> <tr bgcolor="#eeeeee"> <td>10/30/06</td> <td>cy52860231015</td> <td>Deposit</td> <td> </td> <td>$500.00</td> <td><b>$1,677.65</b></td> </tr> <?php } else {?> <tr> <td>10/30/06</td> <td>cy52860231015</td> <td>Deposit</td> <td> </td> <td>$500.00</td> <td><b>$1,677.65</b></td> </tr> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/#findComment-233361 Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 About the easiest way of doing this is to use CSS <style> .color { background-color: #EEEEEE; } .nocolor { background-color: transparent; } </style> The in your script: <?php $cnt = 1; while ($rowtrans = mysql_fetch_array($querytrans)) { $bgc = ($cnt % 2 == 0)?'color':'nocolor'; //is count divisible by 2 $cnt++; ?> <tr class="<?php echo $bgc; ?>"> <td>10/30/06</td> <td>cy52860231015</td> <td>Deposit</td> <td> </td> <td>$500.00</td> <td><b>$1,677.65</b></td> </tr> <?php } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/#findComment-233367 Share on other sites More sharing options...
bsprogs Posted April 19, 2007 Share Posted April 19, 2007 About the easiest way of doing this is to use CSS <style> .color { background-color: #EEEEEE; } .nocolor { background-color: transparent; } </style> The in your script: <?php $cnt = 1; while ($rowtrans = mysql_fetch_array($querytrans)) { $bgc = ($cnt % 2 == 0)?'color':'nocolor'; //is count divisible by 2 $cnt++; ?> <tr class="<?php echo $bgc; ?>"> <td>10/30/06</td> <td>cy52860231015</td> <td>Deposit</td> <td> </td> <td>$500.00</td> <td><b>$1,677.65</b></td> </tr> <?php } ?> Ken You just helped me cut out many many lines of code from my project and I wasn't even the person being helped here! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/#findComment-233370 Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Author Share Posted April 19, 2007 <?php $selecttrans = "SELECT * FROM transactions WHERE userid = '" . $_SESSION['cymember'] . "' AND status <> 'Pending' ORDER BY submitted DESC;"; $querytrans = mysql_query($selecttrans); $color = 1; while ($rowtrans = mysql_fetch_array($querytrans)) { ?> <tr <?php if (is_even($color)) { echo 'bgcolor="#eeeeee"'; }?>> <td>10/30/06</td> <td>cy52860231015</td> <td>Deposit</td> <td> </td> <td>$500.00</td> <td><b>$1,677.65</b></td> </tr> <?php $color++; } ?> Based off that function I did this, and it worked perfectly. Do you have an is_odd function at all that works this well? Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/#findComment-233378 Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Author Share Posted April 19, 2007 I saw what ken did, I have created this just a few minutes before seeing that, and it seems to work all right. Even if I hadn't of figured this out using that other function, then I would of had the answer right here anyway. Thanks for all the advice Now about that is_odd function? Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/#findComment-233380 Share on other sites More sharing options...
bsprogs Posted April 19, 2007 Share Posted April 19, 2007 <?php public function is_odd($num) { return (is_numeric($num)&($num&1)); } ?> However, you could use the is_odd function to see if a number is odd. Just use it like this if (!is_even($num)) { //stuff here } Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/#findComment-233381 Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Author Share Posted April 19, 2007 Hmm I didn't even think of that, and it'll save space. I am going to officially steal your is_even function and re-use it in further projects, it was a good idea, thanks. In the end I had the problem solved, and there is a standing post here to help anyone get alternating rows in a variety of different way's, thanks again for all the feedback. Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/#findComment-233385 Share on other sites More sharing options...
Nameless12 Posted April 19, 2007 Share Posted April 19, 2007 What I did for this was create an is_even function. <?php function is_even($num) { return (is_numeric($num)&(!($num&1))); } ?> the below will suffice as there is no need to do is_numeric when working with bitwise <?php //via bitwise function is_even($n) { return $n & 2; } function is_odd($n) { return $n & 1; } //via modulus function is_even($n) { return $n % 1; } function is_odd($n) { return $n % 2; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/#findComment-233388 Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Author Share Posted April 19, 2007 Thanks for the clarification on that, I will keep that in mind, thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/#findComment-233392 Share on other sites More sharing options...
bsprogs Posted April 19, 2007 Share Posted April 19, 2007 Hmm I didn't even think of that, and it'll save space. I am going to officially steal your is_even function and re-use it in further projects, it was a good idea, thanks. In the end I had the problem solved, and there is a standing post here to help anyone get alternating rows in a variety of different way's, thanks again for all the feedback. Well, I shouldn't say it's my function. I don't want to take credit for making it (although it is simple, but I did not make it) I found it a while ago but I don't remember where. That was when I first started programming. Now, you can enjoy it too. Very nice upgrade to the is_even and is_off functions! Quote Link to comment https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/#findComment-233404 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.