Page 1 of 1

maxValue?

Posted: Sun Jan 20, 2008 11:06 am
by tekkie
hello,

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);
[snip]
--------------------------------------------------------------------------------------------------

Re: maxValue?

Posted: Sun Jan 20, 2008 12:02 pm
by califdon
explode() creates text chunks, which are compared as left-to-right text. You need to force them to be numeric before you compare them. Try adding zero to them, like this:

Code: Select all

foreach ($chartData as $item) {
  [color=#00BF80]$item['value'] += 0;[/color]
  if ($item['value'] > $maxValue)
    $maxValue = $item['value'];
  echo ' item='; 
  echo $item['value'];
}

Re: maxValue?

Posted: Sun Jan 20, 2008 1:29 pm
by tekkie
califdon: excellent. i opted to add the zero as i read them, and that works. thank you for the fix!

for($i=0; $i<5; ++$i ) {
$chunks = explode(" ", fgets($fh));
$data[$i]['title'] = $chunks[0];
$data[$i]['value'] = $chunks[1] + 0; // force a numeric value
}

Re: maxValue?

Posted: Sun Jan 20, 2008 3:54 pm
by John Cartwright
Some more readable (IMO) solutions

Code: Select all

$data[$i]['value'] = intval($chunks[1]);
$data[$i]['value'] = (int)$chunks[1];

Re: maxValue?

Posted: Sun Jan 20, 2008 9:47 pm
by jimthunderbird
using intval is more "formal" i think