Re: [PHP] Re: Alternet row colors

2003-11-17 Thread Chris Hayes
At 01:28 16-11-03, you wrote:
It's so much easier to use the mod (%) operator:
Ah, the alternating rowcolors.
I have a proposal that's a lot lighter on your server.
I prefer this as it does not require a calculation for each row (%2), and 
no counter (and no numrows call). I think it will be noticably faster with 
longer result sets. And your code is shorter.

$rowcolor=FALSE; //initialise

echo 'table';

while(...walk trough resultset...)
 {
 echo 'tr bgcolor='
 .($rowcolor?'#FF':'#00')
  .'td'.$somevalue.' /td/tr';
 $rowcolor=!$rowcolor;  //toggles true/false and hence the row color
 }
echo '/table';





Re: [PHP] Re: Alternet row colors

2003-11-16 Thread Kim Steinhaug
Thanks for this tip, i used to use this when I programmed Perl.
But I didnt know it worked here aswell, hehe.

Absolutely true that this is much better code practise, and it
eases up the lines in the code.

Great tip!

-- 
Kim Steinhaug
---
There are 10 types of people when it comes to binary numbers:
those who understand them, and those who don't.
---


Keith Greene [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
It's so much easier to use the mod (%) operator:

using the mod operator, you can check if a variable is divisible by some
other number without leaving a remainder.
For this example, we want to change every other row, so we would compare
our $count against 2 to see if it leaves a remainder:
$bg = ($count%2==0) ? #00 : FF;

What this line does is FIRST, it checks to see if $count/2 leaves no
remainder. If this is true, it sets the $bg var to #00
If it is false, it sets $bg to #FF

It only requires the addition of one line of code, and to replace your row
background color with a php variable.
Also, you don't need to use printf(), since you aren't specifying any
formatting.

See code below:

code:---

$result = mysql_query(SELECT * FROM albums where id 15);

$Count = @mysql_num_rows($result);

  echo table border=1 cellpadding=3 cellspacing=0
bordercolor='#00'\n;
echo trtd bgcolor='#66'ID/tdtd
bgcolor='#66'ARTIST/tdtd bgcolor='#66'TITLE/tdtd
bgcolor='#66'LABEL/tdtd bgcolor='#66'PRICE/td/tr\n;

  for ($count = 0; $count  $Count; $count++) {
// Extract post details from database
 $myrow = mysql_fetch_array($result);
$id = $myrow ['id'];
$artist = $myrow ['artist'];
  $title = $myrow ['title'];
  $label = $myrow ['label'];
  $price = $myrow ['price'];

$bg = ($count%2==0) ? #00 : FF;
echo tr
bgcolor='.$bg.'td$id/tdtd$artist/tdtd$title/tdtd$label/td
td£$price/tr\n;
}
  echo /table\n;




At 01:08 PM 11/15/2003, you wrote:
Well, first of all Ill just scrap you script since this one is so easy
its better to do it from scratch.

OK, somewhere in your script you have the code that accually
aoutputs the tables you are working with. Im refferring to lines
here, and Im meaning the bottom of this document which is
the scipt you posted, and I have numbered the lines.

Overview of your script.
Line 5 - we print out the table header
Line 11 - 23 is the loop which prints out all the lines, or rows.
Line 24 closes the table.

So what we have to do?

First we need to declare the values we want to use as backround
colours, lets use logical names :

(fig a)
 $backcolor1=#fafafa;
 $backcolor2=#c0c0c0;
 $backcolor=$backcolor1;// we assign color 1

This code has to be written before the loop starts, so somewhere
before line 11.

In the loop (11-23) we need to switch between the colours where
we write the colour of the tr. So we write something like :

(fig b)
 echo 'tr style=background-color:' . $backcolor . ';';
 // continue with the rest of td... /td/tr here
 // which is - your code.

This will print out the first background color, nice. Now we need it
to switch color, so we need to add a little logic. This will be inserted
right before the loop ends (infact, you can put it where ever you like
aslong as its in the loop).

(fig c)
 if($backcolor=backcolor1)
 $backcolor=$backcolor2;
 else
 $backcolor=$backcolor1;

As you see above the logic is quite simple, if the color is 1 - we set it
to 2,
else we set it to 1. If you think of it, if you process this logic over and
over again
you will infact get 1, 2, 1, 2, 1, 2, 1, 2 all the time, :) Nice!

There you have it, and I hope you got the hang of it.

To take your code and implement my colorswither all you need to do is,

1. On line 21 replace #00 width $backcolor
2. Insert the logic (figc), all lines, into line 19
3. Place fig a in line 4.

--
Kim Steinhaug
---
There are 10 types of people when it comes to binary numbers:
those who understand them, and those who don't.
---


The code for return the top ten result is :
1 $result = mysql_query(SELECT * FROM albums where id 15);
2
3 $Count = @mysql_num_rows($result);
4
5  echo table border=1 cellpadding=3 cellspacing=0
6 bordercolor='#00'\n;
7echo trtd bgcolor='#66'ID/tdtd
8 bgcolor='#66'ARTIST/tdtd bgcolor='#66'TITLE/tdtd
9 bgcolor='#66'LABEL/tdtd bgcolor='#66'PRICE/td/tr\n;
10
11 for ($count = 0; $count  $Count; $count++) {
12   // Extract post details from database
13$myrow = mysql_fetch_array($result);
14   $id = $myrow ['id'];
15   $artist = $myrow ['artist'];
16 $title = $myrow ['title'];
17 $label = $myrow ['label'];
18 $price = $myrow 

[PHP] Re: Alternet row colors

2003-11-15 Thread Kim Steinhaug
Well, first of all Ill just scrap you script since this one is so easy
its better to do it from scratch.

OK, somewhere in your script you have the code that accually
aoutputs the tables you are working with. Im refferring to lines
here, and Im meaning the bottom of this document which is
the scipt you posted, and I have numbered the lines.

Overview of your script.
Line 5 - we print out the table header
Line 11 - 23 is the loop which prints out all the lines, or rows.
Line 24 closes the table.

So what we have to do?

First we need to declare the values we want to use as backround
colours, lets use logical names :

(fig a)
$backcolor1=#fafafa;
$backcolor2=#c0c0c0;
$backcolor=$backcolor1;// we assign color 1

This code has to be written before the loop starts, so somewhere
before line 11.

In the loop (11-23) we need to switch between the colours where
we write the colour of the tr. So we write something like :

(fig b)
echo 'tr style=background-color:' . $backcolor . ';';
// continue with the rest of td... /td/tr here
// which is - your code.

This will print out the first background color, nice. Now we need it
to switch color, so we need to add a little logic. This will be inserted
right before the loop ends (infact, you can put it where ever you like
aslong as its in the loop).

(fig c)
if($backcolor=backcolor1)
$backcolor=$backcolor2;
else
$backcolor=$backcolor1;

As you see above the logic is quite simple, if the color is 1 - we set it
to 2,
else we set it to 1. If you think of it, if you process this logic over and
over again
you will infact get 1, 2, 1, 2, 1, 2, 1, 2 all the time, :) Nice!

There you have it, and I hope you got the hang of it.

To take your code and implement my colorswither all you need to do is,

1. On line 21 replace #00 width $backcolor
2. Insert the logic (figc), all lines, into line 19
3. Place fig a in line 4.

-- 
Kim Steinhaug
---
There are 10 types of people when it comes to binary numbers:
those who understand them, and those who don't.
---


The code for return the top ten result is :
1 $result = mysql_query(SELECT * FROM albums where id 15);
2
3 $Count = @mysql_num_rows($result);
4
5  echo table border=1 cellpadding=3 cellspacing=0
6 bordercolor='#00'\n;
7echo trtd bgcolor='#66'ID/tdtd
8 bgcolor='#66'ARTIST/tdtd bgcolor='#66'TITLE/tdtd
9 bgcolor='#66'LABEL/tdtd bgcolor='#66'PRICE/td/tr\n;
10
11 for ($count = 0; $count  $Count; $count++) {
12   // Extract post details from database
13$myrow = mysql_fetch_array($result);
14   $id = $myrow ['id'];
15   $artist = $myrow ['artist'];
16 $title = $myrow ['title'];
17 $label = $myrow ['label'];
18 $price = $myrow ['price'];
19
20   printf(tr
21bgcolor='#00'td$id/tdtd$artist/tdtd$title/tdtd$label/t
d
22td£$price/tr\n);
23}
24 echo /table\n;

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Alternet row colors

2003-11-15 Thread Keith Greene
It's so much easier to use the mod (%) operator:

using the mod operator, you can check if a variable is divisible by some 
other number without leaving a remainder.
For this example, we want to change every other row, so we would compare 
our $count against 2 to see if it leaves a remainder:
$bg = ($count%2==0) ? #00 : FF;

What this line does is FIRST, it checks to see if $count/2 leaves no 
remainder. If this is true, it sets the $bg var to #00
If it is false, it sets $bg to #FF

It only requires the addition of one line of code, and to replace your row 
background color with a php variable.
Also, you don't need to use printf(), since you aren't specifying any 
formatting.

See code below:

code:---
$result = mysql_query(SELECT * FROM albums where id 15);
$Count = @mysql_num_rows($result);

 echo table border=1 cellpadding=3 cellspacing=0 bordercolor='#00'\n;
   echo trtd bgcolor='#66'ID/tdtd 
bgcolor='#66'ARTIST/tdtd bgcolor='#66'TITLE/tdtd 
bgcolor='#66'LABEL/tdtd bgcolor='#66'PRICE/td/tr\n;

 for ($count = 0; $count  $Count; $count++) {
   // Extract post details from database
$myrow = mysql_fetch_array($result);
   $id = $myrow ['id'];
   $artist = $myrow ['artist'];
 $title = $myrow ['title'];
 $label = $myrow ['label'];
 $price = $myrow ['price'];
$bg = ($count%2==0) ? #00 : FF;
   echo tr 
bgcolor='.$bg.'td$id/tdtd$artist/tdtd$title/tdtd$label/tdtd£$price/tr\n;
}
 echo /table\n;



At 01:08 PM 11/15/2003, you wrote:
Well, first of all Ill just scrap you script since this one is so easy
its better to do it from scratch.
OK, somewhere in your script you have the code that accually
aoutputs the tables you are working with. Im refferring to lines
here, and Im meaning the bottom of this document which is
the scipt you posted, and I have numbered the lines.
Overview of your script.
Line 5 - we print out the table header
Line 11 - 23 is the loop which prints out all the lines, or rows.
Line 24 closes the table.
So what we have to do?

First we need to declare the values we want to use as backround
colours, lets use logical names :
(fig a)
$backcolor1=#fafafa;
$backcolor2=#c0c0c0;
$backcolor=$backcolor1;// we assign color 1
This code has to be written before the loop starts, so somewhere
before line 11.
In the loop (11-23) we need to switch between the colours where
we write the colour of the tr. So we write something like :
(fig b)
echo 'tr style=background-color:' . $backcolor . ';';
// continue with the rest of td... /td/tr here
// which is - your code.
This will print out the first background color, nice. Now we need it
to switch color, so we need to add a little logic. This will be inserted
right before the loop ends (infact, you can put it where ever you like
aslong as its in the loop).
(fig c)
if($backcolor=backcolor1)
$backcolor=$backcolor2;
else
$backcolor=$backcolor1;
As you see above the logic is quite simple, if the color is 1 - we set it
to 2,
else we set it to 1. If you think of it, if you process this logic over and
over again
you will infact get 1, 2, 1, 2, 1, 2, 1, 2 all the time, :) Nice!
There you have it, and I hope you got the hang of it.

To take your code and implement my colorswither all you need to do is,

1. On line 21 replace #00 width $backcolor
2. Insert the logic (figc), all lines, into line 19
3. Place fig a in line 4.
--
Kim Steinhaug
---
There are 10 types of people when it comes to binary numbers:
those who understand them, and those who don't.
---
The code for return the top ten result is :
1 $result = mysql_query(SELECT * FROM albums where id 15);
2
3 $Count = @mysql_num_rows($result);
4
5  echo table border=1 cellpadding=3 cellspacing=0
6 bordercolor='#00'\n;
7echo trtd bgcolor='#66'ID/tdtd
8 bgcolor='#66'ARTIST/tdtd bgcolor='#66'TITLE/tdtd
9 bgcolor='#66'LABEL/tdtd bgcolor='#66'PRICE/td/tr\n;
10
11 for ($count = 0; $count  $Count; $count++) {
12   // Extract post details from database
13$myrow = mysql_fetch_array($result);
14   $id = $myrow ['id'];
15   $artist = $myrow ['artist'];
16 $title = $myrow ['title'];
17 $label = $myrow ['label'];
18 $price = $myrow ['price'];
19
20   printf(tr
21bgcolor='#00'td$id/tdtd$artist/tdtd$title/tdtd$label/t
d
22td£$price/tr\n);
23}
24 echo /table\n;
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php