maxValue?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tekkie
Forum Newbie
Posts: 2
Joined: Sun Jan 20, 2008 10:02 am

maxValue?

Post 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]
--------------------------------------------------------------------------------------------------
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: maxValue?

Post 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'];
}
tekkie
Forum Newbie
Posts: 2
Joined: Sun Jan 20, 2008 10:02 am

Re: maxValue?

Post 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
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: maxValue?

Post by John Cartwright »

Some more readable (IMO) solutions

Code: Select all

$data[$i]['value'] = intval($chunks[1]);
$data[$i]['value'] = (int)$chunks[1];
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: maxValue?

Post by jimthunderbird »

using intval is more "formal" i think
Post Reply