below are parts of 3 files. it's a free php script set i'm modifying to read data from a file (data.txt) instead of hardwired as it was in index.php (see commented out section)
what's going wrong, is that if i increase any number in data.txt to a 4 digit number, the chart.php script fails to find that 4-digit number as the maxValue. it will only find the max 3-digit number as the maxValue.
any ideas about what i'm doing wrong?
i'm using PHP 5.1.6 on a CentOS 5.x Linux system with FireFox 2.0.0.11
------ data.txt ----------------------------------------------------------------
Monday 254
Tuesday 489
Wednesday 876
Thursday 583
Friday 195
------ index.php ----------------------------------------------------------------
Code: Select all
<?php
require_once('chart.php');
$data = array();
$chunks = array();
$tableSize = 300;
$ourFileName = "data.txt";
$fh = fopen($ourFileName, 'r') or die("Can't open file");
for($i=0; $i<5; ++$i ) {
$chunks = explode(" ", fgets($fh));
$data[$i]['title'] = $chunks[0];
$data[$i]['value'] = $chunks[1];
}
/**************** F I L L D A T A A R R A Y ** COMMENTED OUT *****
$data[0]['title'] = 'Monday';
$data[0]['value'] = 254;
$data[1]['title'] = 'Tuesday';
$data[1]['value'] = 489;
$data[2]['title'] = 'Wednesday';
$data[2]['value'] = 975;
$data[3]['title'] = 'Thursday';
$data[3]['value'] = 563;
$data[4]['title'] = 'Friday';
$data[4]['value'] = 195;
[snip]
------ chart.php ----------------------------------------------------------------
Code: Select all
function drawChart($chartData){
global $tableSize;
$maxValue = 0;
// Now display the table with bars
echo '<table><tr><th>Title</th><th colspan="2">Value</th></tr>';
// First get the max value from the array
foreach ($chartData as $item) {
if ($item['value'] > $maxValue) $maxValue = $item['value'];
echo ' item='; echo $item['value'];
}
// Now set the theoretical maximum value depending on the maxValue
$maxBar = 1;
while ($maxBar < $maxValue) $maxBar = $maxBar * 10;
// Calculate 1px value as the table is 300px
$pxValue = ceil($maxBar/$tableSize);--------------------------------------------------------------------------------------------------