RE: [PHP] Impossible bug! (fwd)

2003-06-27 Thread Ford, Mike [LSS]
 -Original Message-
 From: John Wulff [mailto:[EMAIL PROTECTED]
 Sent: 26 June 2003 22:14
 
 Hmph, just not quite doing the trick... If you'd be so kind 
 here is the
 complete source to give you a little bigger picture.  As you 
 can probably
 see the point of the script is to generate a bar chart of the array.

AAhh, well, now that's a useful clue.

 Thanks so much for the help, let me know what you think!

You've got a lot of near identical repetition -- once for each value in each
month array -- in the body of the loop; whenever I see this, my immediate
thought is arrays!.  You also store essentially Boolean values as
string-representations of integers(!), so here's my first reworking of your
code:

 ?php
 $height = 1;
 $width_bar = 25;
 $width_spacer = 15;
 $colors = array(blue, red, green, purple, yellow);
 
 $example_data = /* as before */;
 
 /* header HTML and initializations snipped */?

 while($num = 100  $num = 1)

Not sure why you have the = 1 test here -- are you expecting to get -ve
values of $num to suppress the chart?  And, in any case, why not:

  for ( ; $num=100; $num++)

 {
 
 ?
 
 tr
 
 ?php
 foreach ($example_data as $key=$value)
 {
 
  $month = shift($value);

  foreach ($value as $i=$val)
  {
 $percent = ($a / $high) * 100;
 $color = $percent = $num;
 /* I've switched the test round here -- seems to
  * me you were printing colour where you wanted
  * whitespace, and whitespace where you wanted
  * colour!
  */
 
 ?
td width=?php print($width_bar); ? height=?php
 print($height);
  ? ?php if($color) { ?bgcolor=?php print($color[$i]);
 ??php } ?nbsp;/td
 td width=?php print($width_spacer); ? 
 height=?php
 print($height);
 ?nbsp;/td
  ?php
  }
  ?
 td width=?php print($width_spacer); ? 
 height=?php
 print($height);
 ?nbsp;/td
 td width=?php print($width_spacer); ? 
 height=?php
 print($height);
 ?nbsp;/td
 ?php
 }
 ?
 /tr
 
 ?php
 $num = $num + 1;

Lose the above line if you use the for loop suggestion rather than while

 }
 ?

Furthermore, it seems to me that you have no real need to save the values of
$percent and $color ('cos if you did you'd need to save arrays of them, not
just singletons!), so I'd collapse all of the $percent/$color code into the
if() test further down -- so you'd end up with:

  foreach ($value as $i=$val)
  {
?
 td width=?php print($width_bar); ? height=?php
  print($height);
  ? ?php if(($a / $high) * 100 = $num)
{ ?bgcolor=?php print($color[$i]); ??php
}
  ?nbsp;/td

A couple of final observations:

(1) as written, this will produce vertical bars with 0 at the top -- if you
want zero at the bottom, you need to run the count in the opposite order,
from 100 to 1:

  for ($num=100; $num0; $num--)

(2) I feel sure there must be a method of doing this where you can just
calculate the size of bar required and then just display it all at once, but
I can't quite figure that out off the top of my head -- something to think
about, though!

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

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



Re: [PHP] Impossible bug! (fwd)

2003-06-26 Thread Andy Fiddaman
You never set *_color back to 0 at any point, so the first time through
the loop it gets set to 1 and stays there.

Replace you lines like:

;  if($a_percent  $num) { $a_color = 1; }

with:

$a_color = $a_percent  $num;


On Thu, 26 Jun 2003, John Wulff wrote:

; Where the heck is my problem?  No matter what the value for *_color is
; always 1!!!
;
; $example_data = array(
; array(Mar-99,100,2000,5945.33,1234,10),
; array(Feb-99,908,3454,47648.90,4321,50),
; array(Jan-99,542,8000,13365.52,6012,60)
; );
;
; $high = 47648.90;
; if(!isset($num))
; {
;  $num = 1;
; }
; while($num = 100  $num = 1)
; {
;
; foreach ($example_data as $key=$value)
; {
;  list($month, $a, $b, $c, $d, $e) = $value;
;
;  $a_percent = ($a / $high) * 100;
;
;  $b_percent = ($b / $high) * 100;
;  if($b_percent  $num) { $b_color = 1; }
;
;  $c_percent = ($c / $high) * 100;
;  if($c_percent  $num) { $c_color = 1; }
;
;  $d_percent = ($d / $high) * 100;
;  if($d_percent  $num) { $d_color = 1; }
;
;  $e_percent = ($e / $high) * 100;
;  if($e_percent  $num) { $e_color = 1; }
;  }
;
; $num = $num + 1;
; }
;
;
;
; --
; 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



Re: [PHP] Impossible bug! (fwd)

2003-06-26 Thread John Wulff
Hmph, just not quite doing the trick... If you'd be so kind here is the
complete source to give you a little bigger picture.  As you can probably
see the point of the script is to generate a bar chart of the array.
Thanks so much for the help, let me know what you think!
-John


?php
$height = 1;
$width_bar = 25;
$width_spacer = 15;
$color_first = blue;
$color_second = red;
$color_third = green;
$color_fourth = purple;
$color_fifth = yellow;

$example_data = array(
array(Mar-99,100,2000,5945.33,1234,10),
array(Feb-99,908,3454,47648.90,4321,50),
array(Jan-99,542,8000,13365.52,6012,60)
);

?

html
head
titleUntitled Document/title
meta http-equiv=Content-Type content=text/html; charset=iso-8859-1
/head

body

table width=100 border=0 cellspacing=0 cellpadding=0

?php
$high = 47648.90;
if(!isset($num))
{
$num = 1;
}
while($num = 100  $num = 1)
{

?

tr

?php
foreach ($example_data as $key=$value)
{

$a_color = 0;
$b_color = 0;
$c_color = 0;
$d_color = 0;
$e_color = 0;

list($month, $a, $b, $c, $d, $e) = $value;

$a_percent = ($a / $high) * 100;
if($a_percent  $num) { $a_color = 1; }

$b_percent = ($b / $high) * 100;
if($b_percent  $num) { $b_color = 1; }

$c_percent = ($c / $high) * 100;
if($c_percent  $num) { $c_color = 1; }

$d_percent = ($d / $high) * 100;
if($d_percent  $num) { $d_color = 1; }

$e_percent = ($e / $high) * 100;
if($e_percent  $num) { $e_color = 1; }

?
td width=?php print($width_bar); ? height=?php
print($height);
? ?php if($a_color = 1) { ?bgcolor=?php print($color_first);
??php } ?nbsp;/td
td width=?php print($width_spacer); ? height=?php
print($height);
?nbsp;/td
td width=?php print($width_bar); ? height=?php
print($height);
? ?php if($b_color = 1) { ?bgcolor=?php print($color_second);
??php } ?nbsp;/td
td width=?php print($width_spacer); ? height=?php
print($height);
?nbsp;/td
td width=?php print($width_bar); ? height=?php
print($height);
? ?php if($c_color = 1) { ?bgcolor=?php print($color_third);
??php } ?nbsp;/td
td width=?php print($width_spacer); ? height=?php
print($height);
?nbsp;/td
td width=?php print($width_bar); ? height=?php
print($height);
? ?php if($d_color = 1) { ?bgcolor=?php print($color_fourth);
??php } ?nbsp;/td
td width=?php print($width_spacer); ? height=?php
print($height);
?nbsp;/td
td width=?php print($width_bar); ? height=?php
print($height);
? ?php if($e_color = 1) { ?bgcolor=?php print($color_fifth);
??php } ?nbsp;/td
td width=?php print($width_spacer); ? height=?php
print($height);
?nbsp;/td
td width=?php print($width_spacer); ? height=?php
print($height);
?nbsp;/td
td width=?php print($width_spacer); ? height=?php
print($height);
?nbsp;/td
?php
}
?
/tr

?php
$num = $num + 1;
}
?

/table
?php if($e_percent  0) { print(1); } print($e_percent);?
/body
/html



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