Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/47773-solved-alternating-rows/
Share on other sites


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

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>

 

 

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.

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

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

 

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 :D

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

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?

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.

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

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.