> -----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 } ?>> </td>
> <td width="<?php print($width_spacer); ?>"
> height="<?php
> print($height);
> ?>"> </td>
<?php
}
?>
> <td width="<?php print($width_spacer); ?>"
> height="<?php
> print($height);
> ?>"> </td>
> <td width="<?php print($width_spacer); ?>"
> height="<?php
> print($height);
> ?>"> </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
}
?>> </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; $num>0; $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