In these days i'm trying to draw a graph from a file using PhpMyGraph5.0,
on the autor's site (http://phpmygraph.abisvmm.nl/) there is this example file:
<?php
//Set content-type header
header("Content-type: image/png");
//Include phpMyGraph5.0.php
include_once('phpMyGraph5.0.php');
//Set config directives
$cfg['title'] = 'Example graph';
$cfg['width'] = 500;
$cfg['height'] = 250;
//Set data
$data = array(
'Jan' => 12,
'Feb' => 25,
'Mar' => 0,
'Apr' => 7,
'May' => 80,
'Jun' => 67,
'Jul' => 45,
'Aug' => 66,
'Sep' => 23,
'Oct' => 23,
'Nov' => 78,
'Dec' => 23
);
//Create phpMyGraph instance
$graph = new phpMyGraph();
//Parse
$graph->parseHorizontalLineGraph($data, $cfg);
?>
Because i need to get the input from a file i've modded the example file changing the $data assignment with:
$data = file("$PATH/$MYFILE");
I've formatted the text inside MYFILE and these are some lines of the file:
'00:00' => 19,
'00:05' => 19,
'00:10' => 21,
...
'17:10' => 21,
'17:15' => 21,
'17:20' => 21,
But when i try to draw the graph i obtain this message instead of the graph:
"exception `Exception` with message `The value of the key %s` is not numeric.`"
I've searched in PhpMyGraph5.0.php and i've found the test that throws the exception:
//Loop
foreach($data as $key => $value) {
//Test
if(!is_numeric($value)) {
throw new Exception('The value of the key "%s" is not numeric.');
}
...
I've tried to substitute the "throw Exception" with this cast:
$value=(int)$value;
but i obtain only an empty graph.
If i manually paste the content of MYFILE inside $data = array(PASTE_HERE); it works, but i can't do it manually.
I think that the problem is about the data type of the value, but i've no ideas on how to solve this problem.
Thanks to everyone and sorry for my bad english.
That exception seems to be badly coded, try changing it to this and it should give you the value of the key where it finds the value is not numeric, that may help identify where the error is :-
throw new Exception(sprintf('The value of the key "%s" is not numeric.',$key));
EDIT
Ok I see the problems, you are not getting what you think you are getting from the $data = file("$PATH/$MYFILE");
if you test using this
$data = file("$PATH/$MYFILE");
print_r($data);
You get the output :
Array
(
[0] => '00:00' => 19,
[1] => '00:05' => 19,
[2] => '00:10' => 21,
[3] => '17:10' => 21,
[4] => '17:15' => 21,
[5] => '17:20' => 21
)
So index [0] is actually an array and not a number, hence the error.
You are going to have to rethink the way you input your data.
Try this for size:
Change you data file to look like this
'00:00',19
'00:05',19
'00:10',21
'17:10',21
'17:15',21
'17:20',21
And your code to do this
$data = array();
$handle = fopen('tst.txt', 'r');
while (!feof($handle)) {
$line = fgets($handle, 8192);
list($time,$count) = explode(',',$line);
$data[$time] = $count;
}
fclose($handle);
print_r($data);
This will generate the following array
Array
(
['00:00'] => 19
['00:05'] => 19
['00:10'] => 21
['17:10'] => 21
['17:15'] => 21
['17:20'] => 21
)
Which I assume is what you wanted in the first place.
EDIT 2
Dont change the package, change what you send it
Replace this line
$data[$time] = $count;
With
$data[$time] = (int)$count;
That should do it.
Related
Consider the following:
$characterStats = [
['strength' => 500],
['dexterity' => 200],
['agility' => 1000],
['intelligence' => 1200],
['health' => 675],
];
$stat = array_search(max($characterStats), $characterStats);
echo $stat;
What I expect: ['intelligence' => 1200]
What I get: 0
Can some one help me out to achieve what I want?
Try the following:
$characterStats = array(
'strength' => 500,
'dexterity' => 200,
'agility' => 1000,
'intelligence' => 1200,
'health' => 675,
);
$stat = array_search(max($characterStats), $characterStats);
echo $stat;
I changed the way the array is declared. I believe you may need to indicate the field name you would like to search if using nested arrays with the following call:
$stat = array_search(max($characterStats), array_column($characterStats, 'KEYNAME'));
However, since each sub array has only 1 element with different "key" it may not be the best approach. For your scenario, you may need to use another approach, where you loop through each element and store the max value found.
With the array as you have it at the moment, the easiest way I can think of doing it as a standard foreach() and keep the maximum value as well as the element where it's found (save doing another search to get the full entry)...
$characterStats = [
['strength' => 500],
['dexterity' => 200],
['agility' => 1000],
['intelligence' => 1200],
['health' => 675],
];
$maxStat = null;
$max = null;
foreach ( $characterStats as $stat ){
if ( current($stat) > $max ) {
$max = current($stat);
$maxStat = $stat;
}
}
print_r( $maxStat);
I'm new to web pages and dynamic content:
I have a server running php and I'm able to read in and display CSV text data - how may I graph this? I was using http://phpmygraph.abisvmm.nl/ but I'm unsure how to parse my CSV data into columns.
In addition, how do I create date-time objects in PHP (is this possible?)
A code example:
<?php
//Set content-type header
header("Content-type: image/png");
//Include phpMyGraph5.0.php
include_once('phpMyGraph5.0.php');
//Set config directives
$cfg['title'] = 'Flow data site#3, John Radcliff Hospital';
$cfg['width'] = 500;
$cfg['height'] = 250;
//Set data
$data = array(
'Jan' => 12,
'Feb' => 25,
'Mar' => 0,
'Apr' => 7,
'May' => 80,
'Jun' => 67,
'Jul' => 45,
'Aug' => 66,
'Sep' => 23,
'Oct' => 23,
'Nov' => 78,
'Dec' => 23
);
//Create phpMyGraph instance
$graph = new phpMyGraph();
//Parse
$graph->parseVerticalLineGraph($data, $cfg);
?>
This image shows an example of data I can generate on a webpage and the graph I would like to see - this is not a screen shot, only a 'wish list' of what I would like to generate:
if I can recommend, I would adapt your data and would use Google Charts
https://developers.google.com/chart/
$result = $proxy->salesOrderInvoiceCreate((object)array('sessionId' => $sessionId->result, 'itemsQty' => array('order_item_id' => 15, 'qty' => '1')));
$mainarray[];
$itemarray[];
I need multiple of this
array('order_item_id' => 15, 'qty' => '1')
Which means i need a array in a array.
foreach(statement){
array_push($itemarray, "order_item_id", echo $item->product_id;);
array_push($itemarray, "qty", echo $item->qty);
array_push($mainarray, $itemarray);
}
enter code here
Request Example SOAP V2 (WS-I Compliance Mode)
http://www.magentocommerce.com/api/soap/sales/salesOrderInvoice/sales_order_invoice.create.html
In fact i'm also not sure what do i replace the current
array('order_item_id' => 15, 'qty' => '1')
with
array($mainarray) ??
That is not the correct way of using array_push your current $itemarray output will look something like
Array
(
[0] => 'order_item_id'
[1] => '200'
[2] => 'qty'
[3] => '2'
)
I would go back to basics and use something like to generate your multi dimensional array:
$itemarray[] = array("order_item_id" => $item->product_id, "qty" => $item->qty);
array_push($mainarray, $itemarray);
Edit:
Ok I reread your questions, ignore $mainArray.
$result = $proxy->salesOrderInvoiceCreate((object)array('sessionId' => $sessionId->result, 'itemsQty' => $itemarray));
That should work as with the other examples qty/itemsQty show it accepting multikey arrays.
Im trying to make a multidimensional array with two columns. Name and Counter. I can do a single array with all the names. But I dont know how to make it multidimensional and be able to still update the counters. Code i got so far is
if (!in_array($prodname, $da)){
array_push($da, $prodname);
}
and then I can dump it back out with a foreach. How do I make it two dimensional? How can I say alright this exists update the old value? etc.
If you only need name and counter then you should just be able to use a normal array:
$nameCountArray = array();
foreach($names as $name){
if(!array_key_exists($name,$nameCountArray)){
$nameCountArray[$name] = 1;
}else{
$nameCountArray[$name] = $nameCountArray[$name] + 1;
}
}
If you do need multidimensional arrays these are just arrays of arrays and can be accessed as such. A good example of this is using a 2d array to store locations (say on a 3 by 3 grid):
$twoDArray = array(
0 => array(0 => 1,
1 => 4,
2 => 7),
1 => array(0 => 2,
1 => 5,
2 => 8),
2 => array(0 => 3,
1 => 6,
2 => 9)
);
//Grab the item at 1,2
$item = $twoDArray[1][2];//Will give '8'
Supposing you want $da to look like this:
Array(
"name1" => array("score1" => 80, "score2" => 100),
"name2" => array("score1" => 50, "score2" => 60),
"name3" => array("score1" => 90, "score2" => 80),
...
)
Then all you need to do is something like:
function setScore($prodName, $scoreName, $score)
{
global $da;
if (!array_key_exists($prodName, $da)) {
$da[$prodName] = array();
}
$da[$prodName][$scoreName] = $score;
}
setScore("name1", "score1", 80);
setScore("name1", "score2", 100);
setScore("name2", "score1", 50);
...
Unless I'm misunderstanding your question, which is very possible.
I'm trying to calculate the volume of each box but I keep coming across errors and I don't know how to fix it. Everything seem correct and I followed how the book's example.
This is the error it gave me in this code:
"Notice: Undefined offset: 0 in C:\wamp\www\BoxArray.php on line 16"
<?php
$BoxMeasurements = array("sBox" => array("length" => 12, "width" => 10, "depth" => 2.5),
"mBox" => array("length" => 30, "width" => 20, "depth" => 4),
"lBox" => array("length" => 60, "width" => 40, "depth" => 11.5));
$BoxMeasurements = array_slice($BoxMeasurements, 0, 3);
echo "The box sizes are:";
for($i = 0; $i < count($BoxMeasurements); ++$i)
{
echo "$BoxMeasurements[$i]";
}
?>
When I tried doing it the other way I got this error:
"Parse error: syntax error, unexpected '=', expecting ')' in C:\wamp\www\BoxArray.php on line 8"
<?php
$sBox = array("length" => 12, "width" => 10, "depth" = 2.5);
$mBox = array("length" => 30, "width" => 20, "depth" = 4);
$lBox = array("length" => 60, "width" => 40, "depth" => 11.5);
$dimension = array($sBox, $mBox, $lBox);
echo "$dimension[0][0]";
?>
Is there a special way to call the variable/array name?
$BoxMeasurements is declared as an associative array, which means you should access its values with the keys you defined in the declaration: "sBox", "mBox" and "lBox".
In order to iterate over this kind of arrays you can use the foreach form:
<?php
$BoxMeasurements = array("sBox" => array("length" => 12, "width" => 10, "depth" => 2.5),
"mBox" => array("length" => 30, "width" => 20, "depth" => 4),
"lBox" => array("length" => 60, "width" => 40, "depth" => 11.5));
echo "<pre>";
echo "The box sizes are:\n";
foreach($BoxMeasurements as $name => $size)
{
$volume = $size['length'] * $size['width'] * $size['depth'];
echo " - $name: $volume\n";
}
echo "</pre>";
?>
OUTPUT
The box sizes are:
- sBox: 300
- mBox: 2400
- lBox: 27600
You seem not to understand difference between variable name and variable content, as indicated by using " around names. It's pointless. It should be
echo $BoxMeasurements[$i];
While assigning your array, correct syntax is:
key => value
while some of your "rows" are assigned just "key = value" which throws syntax error.
Also you try to access array by numerical indices, while your array does not use them. Use foreach to iterate the array:
foreach( $BoxMeasurements as $key=>$val ) {
echo $val;
}
Finally, you should be doing post increment in your for loop, not pre increment.
I strongly suggest to spend some time and go through some tutorials as you made too many elementary errors.