clanstyles Posted June 28, 2007 Share Posted June 28, 2007 Okay guys, I have no idea in hell how to do this somebody at least point me in the right direction. I know its possible. I need something like Alexa.com It generates a graph and shows stats on it over time. I know its just an image they use that they write on. How do you go about doing this? How do you make the lines? Thank You Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 read up on the GD libarary Quote Link to comment Share on other sites More sharing options...
clanstyles Posted June 28, 2007 Author Share Posted June 28, 2007 Thx Yeah found a tutorial. its really easy Next problem im having with this. I have "dates" in a field. I need to organize them. I have a table: id, serverid, size, dirname, timestamp For every timestamp that is the same I need to average the entire one out WHILE the serverid is == what ever its on. Thats not the hard part I can't figure out how to do the timestamp part. It is in the format of mm/d/y it has to do with Foreach i would think. I'm not to sure though. ThanK You Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 average the time stamps??? best idea I got would be to convert them to UNIX seconds since Epoch average that then convert that to a timestamp Quote Link to comment Share on other sites More sharing options...
clanstyles Posted June 28, 2007 Author Share Posted June 28, 2007 average the time stamps??? best idea I got would be to convert them to UNIX seconds since Epoch average that then convert that to a timestamp No not average them but like..Heres an example: (id, serverid, size, dir, timestamp) FakeData: 1, 1, 500, "FoldersRock", "05, 28, 2007" 2, 1, 123, "FoldersRock1", "05, 28, 2007" 3, 1, 543, "FoldersRock2", "05, 28, 2007" 4, 1, 6764, "FoldersRock3", "05, 25, 2007" 5, 1, 345677, "FoldersRock4", "05, 25, 2007" 6, 1, 3, "FoldersRock5", "05, 25, 2007" ----------------------------------------- 7, 2, 500, "FoldersRock", "05, 28, 2007" 8, 2, 123, "FoldersRock1", "05, 28, 2007" 9, 2, 543, "FoldersRock2", "05, 28, 2007" 10 2, 6764, "FoldersRock3", "05, 25, 2007" 11, 2, 345677, "FoldersRock4", "05, 25, 2007" 12, 2, 3, "FoldersRock5", "05, 25, 2007" Now with this information...Average all the "Size" fields that are the same as the date. So like all the "05, 28, 2007";s on SERVER1 are the same Then all the "05, 28, 2007" on server2 must be averaged but it goes for every date. so then the "05, 25, 2007" go next Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 try: <?php $result = mysql_query("SELECT size,timestamp FROM `tablename`"); while( $row= mysql_fetch_array($result)){ $sizes[$row['timestamp']][] = $row['size']; } foreach($sizes as $key => $value){ $tempcount = count($value); $average[$key] = array_sum[$value]/$tempcount; echo "Average for ".$key.": ".$average[$key]."<br/>"; } ?> edit: the while function gathers all the info from the table and then organizes them by date the foreach takes each date and then averages and displays its average This only works for 1 table if you have a second copy and paste the whole while function and change the table name. It will just append to the array accordingly Quote Link to comment Share on other sites More sharing options...
clanstyles Posted June 28, 2007 Author Share Posted June 28, 2007 <? include("config.php"); $graphValues = Array(); // Define .PNG image header("Content-type: image/png"); $imgWidth=250; $imgHeight=250; $result = mysql_query("SELECT size,timestamp FROM `serverInfo`") or die(mysql_error()); while( $row= mysql_fetch_array($result)){ $sizes[$row['timestamp']][] = $row['size']; } foreach($sizes as $key => $value) { $tempcount = count($value); $average[$key] = array_sum($value)/$tempcount; array_merge($graphValues, $key); //echo "Average for ".$key.": ".$average[$key]."<br/>"; } // Create image and define colors $image=imagecreate($imgWidth, $imgHeight); $colorWhite=imagecolorallocate($image, 255, 255, 255); $colorGrey=imagecolorallocate($image, 192, 192, 192); $colorBlue=imagecolorallocate($image, 0, 0, 255); // Create border around image imageline($image, 0, 0, 0, 250, $colorGrey); imageline($image, 0, 0, 250, 0, $colorGrey); imageline($image, 249, 0, 249, 249, $colorGrey); imageline($image, 0, 249, 249, 249, $colorGrey); //imagefttext($image, 12.0, 12.0, 0, 0, 0, "", "12"); // Create grid for ($i=1; $i<11; $i++){ imageline($image, $i*25, 0, $i*25, 250, $colorGrey); imageline($image, 0, $i*25, 250, $i*25, $colorGrey); } // Create line graph for ($i=0; $i<10; $i++){ imageline($image, $i*25, (250-$graphValues[$i]), ($i+1)*25, (250-$graphValues[$i+1]), $colorBlue); } // Output graph and clear image from memory imagepng($image); imagedestroy($image); ?> This prints out nothing.. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 check inside the while loop try adding echo "Entry Found:" .current($sizes[$row['timestamp']])."<br/>"; Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 $sizes[$row['timestamp']][] = $row['size']; Actually try: $sizes["$row['timestamp']"][] = $row['size']; There might be an issue with the spaces on your time stamp this will let us know Quote Link to comment Share on other sites More sharing options...
clanstyles Posted June 28, 2007 Author Share Posted June 28, 2007 Well its getting the data fine. I think where its having the problem is adding it to the array ( How do you do that ? ) The only way I know how is $graphValue = array(12, 123,123, 123, 123, 123); Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 one sec i'm testing it with fake data on my server Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 This works on my server with the dummy data. (same as you would get from the mysql.) Below this is one that should work. <?php $data[1]['size'] = 500; $data[1]['timestamp'] = "05, 28, 2007"; $data[2]['size'] = 475; $data[2]['timestamp'] = "05, 28, 2007"; $data[3]['size'] = 354; $data[3]['timestamp'] = "05, 26, 2007"; $data[4]['size'] = 184; $data[4]['timestamp'] = "05, 26, 2007"; $data[5]['size'] = 654; $data[5]['timestamp'] = "05, 26, 2007"; $data[6]['size'] = 475; $data[6]['timestamp'] = "05, 27, 2007"; $data[7]['size'] = 321; $data[7]['timestamp'] = "05, 27, 2007"; $data[8]['size'] = 875; $data[8]['timestamp'] = "05, 27, 2007"; $data[9]['size'] = 552; $data[9]['timestamp'] = "05, 27, 2007"; $data[0]['size'] = 123; $data[0]['timestamp'] = "05, 27, 2007"; foreach ($data as $value) { $temptime = str_replace(", ","",$value['timestamp']); $sizes[$temptime][] = $value['size']; } foreach($sizes as $key => $value){ $tempcount = count($value); $average[$key] = array_sum($value)/$tempcount; echo "Average for ".$key.": ".$average[$key]."<br/>"; } ?> Good one <?php $result = mysql_query("SELECT size,timestamp FROM `tablename`"); while( $row= mysql_fetch_array($result)){ $temptime = str_replace(", ","",$value['timestamp']); $sizes[$temptime][] = $value['size']; } foreach($sizes as $key => $value){ $tempcount = count($value); $average[$key] = array_sum($value)/$tempcount; echo "Average for ".$key.": ".$average[$key]."<br/>"; } ?> You will have to re space out the times in post Quote Link to comment Share on other sites More sharing options...
clanstyles Posted June 28, 2007 Author Share Posted June 28, 2007 okay thank you .. By any chance is it possible to make another array that holds each individual time that is different? BTW that works and I love you I really need to study up on foreach. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 if you have php 5 this will work to space out the dates again $tempkey = str_split($key); $key = $tempkey[0]; $key .= $tempkey[1]; $key .= " "; $key .= $tempkey[2]; $key .= $tempkey[3]; $key .= " "; $key .=$tempkey[4]; $key .=$tempkey[5]; $key .=$tempkey[6]; $key .=$tempkey[7]; what do you mean each time? Quote Link to comment Share on other sites More sharing options...
clanstyles Posted June 28, 2007 Author Share Posted June 28, 2007 Well whats going to happen is this script I made runs at midnight and logs all this info into serverInfo ( I showed you the fields ). It includes the date. This graph is supposed to show over time. So if each date is seperate it can show it better over time. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 I think I'm understanding, but let me get this right. You are saying since the queries are placed into the table in a time ordred fashioned (IE one at 10 am one at 12pm one at 2 pm etc etc) that you would like to have each one be an individual node on your graph. Well to answer your question that is possible. because all the data is in the array $size[The Day][Counting Number] they should ideally be in time order. so you can just say foreach($sizes[$timestamp] as $value) { $node[] = $value; } if that makes any sense. Quote Link to comment Share on other sites More sharing options...
clanstyles Posted June 28, 2007 Author Share Posted June 28, 2007 Yeah I think I get it, thank you! Ill try that now. Wait Im lost lol imageline($image, $i*25, (250-$graphValues[$i]), ($i+1)*25, (250-$graphValues[$i+1]), $colorBlue); I need to make a new line for each one. And how is that above code going to help me with this? Thanks Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 i'll try something give me a sec I'll work it into what u got Quote Link to comment Share on other sites More sharing options...
clanstyles Posted June 28, 2007 Author Share Posted June 28, 2007 Update top. Thanks man so much for this btw. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 you have php 4 or 5? Quote Link to comment Share on other sites More sharing options...
clanstyles Posted June 28, 2007 Author Share Posted June 28, 2007 I have both i prefer 5 Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 something is being stupid for me, but this is what i got going; <?php $keys = array_keys($sizes); foreach ($keys as $value) { if ($initalized == "yes") {$grapharrays .= ", ";} else {$initalized = "yes";} $grapharrays .= "\$sizes["; $grapharrays .= $value; $grapharrays .= "]"; } $graphvalues = array_merge($grapharrays); print_r($graphvalues); ?> which should work, but some reason its taking the string i have of array values as a string and not the arrays it needs to merge Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 if all your data is in order in your mysql this will work $graphValues = $row['size']; inside the while loop try that Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 best idea here though is to expand your timestamp to a full YYYY-MM-DD-HH-MM-SSSS and then on the query say ORDER BY timestamp and then there is no worries at all. then however you will have to do more adjustment to the string but i can show you that if you thinkin gabout going that way Quote Link to comment Share on other sites More sharing options...
clanstyles Posted June 28, 2007 Author Share Posted June 28, 2007 Ill be happy to change that. But what I would like in the end of all this is for it to be like this. I have a table of servers CREATE TABLE `servers` ( `id` int(11) NOT NULL auto_increment, `name` text, `keyfile` text NOT NULL, `password` text NOT NULL, `enabled` tinyint(1) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin1; So if I have two servers, on the graph there needs to be two lines. in serverInfo the field serverid = 1 so tin the table servers it needs to make sure theres a line for that server and average it out ever day. There is a cronjob to index dir info ( have all this done already ). It logs it by date. Does this make more sense? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.