I have a ajax function which gives the response like:
[{"lot_no":"Lot A","sent_weight":"20.8","received_weight"
:"19.3"},{"lot_no":"Lot B","sent_weight":"6.8","received_weight"
:"5.3"},{"lot_no":"Lot C","sent_weight":"7.8","received_weight"
:"10.3"}]
I want to draw the google function chart on basis of Lot no, sent weight and received weight but the problem is :
Type mismatch. Value "20.8" does not match type number in column index 1
How can i make a google function chart with these sort of response data
Since PHP 5.3.3, there's a flag for auto-converting numbers (the options parameter was added in PHP 5.3.0):
$arr = array( 'row_id' => '1', 'name' => 'George' );
echo json_encode( $arr, JSON_NUMERIC_CHECK );
// {"row_id":1,"name":"George"}
This will help you.
Your ajax response is returning the string values in respective keys. Convert the string into float value example: parseFloat("10.33"), this will return number format.
Related
I am using 33_Chart_create_scatter.php to generate a XY scatter chart. I am closing in on completion. All I have left is inducing the linest function. Would someone please provide some code on the use of linest in phpspreadsheet?
I've done a lot of googles, with no luck. Lots of visits to phpspreadsheet docs. With a lot of information, I don't understand. I have not used classes to this point, so I don't know how to form the code to talk with the classes shown in the code section below?
Here is the data I use:
$xAxisTickValues = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$1:$C$18', null, 18),
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$F$1:$F$18', null, 18)
[;
\PhpOffice\PhpSpreadsheet\Calculation\Statistical::LINEST
'LINEST' => [
'category' => Category::CATEGORY_STATISTICAL,
'functionCall' => [Statistical::class, 'LINEST'],
'argumentCount' => '1-4',
],
public static function LINEST($yValues, $xValues = null, $const = true, $stats = false)
{
$const = ($const === null) ? true : (bool) Functions::flattenSingleValue($const);
$stats = ($stats === null) ? false : (bool) Functions::flattenSingleValue($stats);
if ($xValues === null) {
$xValues = range(1, count(Functions::flattenArray($yValues)));
}
Of course, I have no results yet. My expectations would be to have returned the linest statistic derived.
No one has added anything for a week. Here is what I have accomplished in that week: I got the following two data arrays by running the formula wizzard / linest on the data in 33 Chart create scatter. The chart I made. So, I know what the data is that I want from phpspreadsheet/linest.
Six foot data array
0.798178535 18.35040936
0.012101577 0.241020964
0.996335545 0.53274435
4350.269442 16
1234.67843 4.541064671
Ten foot data array
0.819287926 16.98988304
0.007826008 0.15586663
0.998542214 0.344522174
10959.54502 16
1300.848983 1.899128449
setCellValue, puts the formula in cell A32 and cell A32 displays the first number from the Six foot data array above.
$helper->log, gives the following output. The getCell gives the linest formula in the cell.
getCalculatedValue gives the Last value in the Six foot data array above.
19:12:47 Write Xlsx format to 33_Chart_create_scatter.xlsx in 0.0138 seconds
19:12:47 Value of linest six foot from getCell(A32) : =LINEST(D1:D18 , C1:C18 , TRUE, TRUE)
19:12:47 Value of linest six foot used getCalculatedValue get cell A32 : 4.5410646714826
19:12:47 Value of linest ten foot from getCell(A42) : =LINEST(G1:G18 , F1:F18 , TRUE, TRUE)
19:12:47 Value of linest ten foot used getCalculatedValue get cell A42 : 1.8991284485724
I tried
$spreadsheet->getActiveSheet()->fromArray($linestSix ,null,'C32');
That does the same as setCellValue, described above.
Follow this link, enter link description here ,to see the browser output when I run my code to generate my chart, 33 Chart create scatter. Go down about 75 lines into the object to [Worksheet!A32] , There is my data, but, how do I get it out of there? I got that from the print_r of $linest6 below.
$linest6 = $spreadsheet->getActiveSheet()->setCellValue('A32', '=LINEST(D1:D18 , C1:C18 , TRUE, TRUE)');
echo 'The six foot data array : <br>';
echo '<pre>';
print_r($linest6);
echo '</pre>';
Would someone, maybe Mark Baker, help me get the statistics from linest, the same as I described above from the formula wizzard? Thank you.
I finally got it!! Many trips to Google. Reviewing how to retrieve an array from a class-function. Here is my final script.
$stats = new Statistical();
$statArr6 = $stats->LINEST($dataArraySixY, $dataArraySixX, TRUE, TRUE);
$statArr10 = $stats->LINEST($dataArrayTenY, $dataArrayTenX, TRUE, TRUE);
$worksheet->fromArray(
$statArr6,
NULL,
"C22");
$worksheet->fromArray(
$statArr10,
NULL,
'C32'
);
I have a little more to display the info as I want to, but I have the arrays.
Here I want to enter the value of the test result, where the data is integer based.
Plus, I would like to add up with other test results value data.
But why when I enter this integer-based data, its value is always rounded, or I get unsuitable results
in Controller Exam.php
public function Exam()
{
$data_exam = array(
'subjects' => 'Natural Sciences',
'name_of_pupil' => 'Rizky Remangsa',
'the_pure_value_of_the_exam' => 5.6,
'addition_with_another_value' => 11.2,
'student_address' => 'Indonesia/Jakarta Selatan',
'graduation_status' => 'Not graduated yet'
);
$this->exam_model->insertDataExam($data_exam);
}
The results of the data entered above are:
table the_pure_value_of_the_exam = 6
table addition_with_another_value = 11
You're storing your data in the wrong format so it's being converted for you.
5.6 is a float, not an integer. Integers are whole numbers. If you enter that value into an integer field it will round it to the nearest whole number.
You need to change the format you're storing the value in to something more appropriate:
https://dev.mysql.com/doc/refman/5.7/en/fixed-point-types.html
https://dev.mysql.com/doc/refman/5.7/en/floating-point-types.html
I'm making the assumption here that you're using MySQL but the same approach is going to apply regardless of the database type. Check the documentation to determine how best to store the data.
I have a function using array_search not working ... here is my code
function LangFull($name){
$languageCodes = array(
"abkhazian"=>"ab",
"afar"=>"aa",
"afrikaans"=>"af",
"afrikaans"=>"af-za",
"zulu"=>"zu",
"zulu"=>"zu-za"
);
return ucwords(array_search(strtolower($name),$languageCodes));
}
echo LangFull("zu"); /// Gives no output
echo LangFull("zu-za"); /// Gives output
same with af is no output ... please help
If its possible to interchange, (values to keys and keys to values) and won't have those key collisions, then you can do it that way also:
function LangFull($name){
$languageCodes = array(
"ab" => "abkhazian",
"aa" => "afar",
"af" => "afrikaans",
"af-za" => "afrikaans",
"zu" => "zulu",
"zu-za" => "zulu",
);
return isset($languageCodes[$name]) ? ucwords(strtolower($languageCodes[$name])) : 'Not found';
}
echo LangFull("zu"); /// Gives output
echo LangFull("zu-za"); /// Gives output
echo LangFull("yahoo!");
You have two identical array keys:
"zulu"=>"zu",
"zulu"=>"zu-za"
You need to name one of them something else.
As they are the same, trying to access one of them specifically is futile as PHP does not know which of the two you are requesting.
Alternatively, if you are trying to store more than 1 data value for a given key, you can make the value of a key an array, so can then store more data as required.
e.g.
array (
"afrikaans"=> array(
"af",
"af-za",
),
"zulu"=> array(
"zu",
"zu-za",
)
);
EDIT.
In response to you asking about swapping the keys and values:
You can, and Ghost has shown you how.
However retaining your keys as they are (as my above array example) allows you to collate all relevant data into one index, and can access it easily.
Swapping values and keys will likely make it harder to obtain data you need, as your key is now the "data". So to grab data from an array you'd need to know the data (as it's now the key) and you'd actually be grabbing the reference (what was your key).
Which is a bit odd. It can work, but it's not really ideal.
I search a lot in stack and google try to find the answer which seems to be easy but I'm still stuck with it
I write a code to encode json with values I wanted from . and I would like to add a key / value to the JSON
the JSON is as following structure
{
- files: [
{
title: "works",
- tracks: [
{
title: "File",
format: "mp3"
}
]
},
-{
title: "season1",
tracks: [
{
title: "Button-1",
format: "wav"
},
-{
title: "Beep-9",
format: "wav"
}
]
}
]
}
I want to add to that a key and its value at the beginning to the json as properties under the title files , I mean that can be read by code as
json[files][new_key]
I tried to set that value like this
$json['new_key'] = "new_value";
but this causes adding numbers to the arrays in json , I don't why they numbered
this numbers affect my reading way of the json as JSONModel in my iOS app
so , I hope you can help me
thanks in advance
Assuming that the new value you want to add varies by file, you would need to loop through $json[files] and insert them per key/value pair.
<?php
for($i=0; $i<count($json); $i++)
$json[files][$i]["new_key"] = "value";
?>
I'm still not sure what you have exactly, but it seems you are trying to manipulate the json string.
If done correctly, that is probably the most efficient solution, but what you could also do is:
use json_decode to generate an array from your json string;
locate the correct section / sub-array where you want to add your data;
use array_unshift to prepend your new key - value pair;
use json_encode to generate a json string from your complete array.
The reason you're getting numbers appearing is because you're adding a key to an array (which functions more or less as a list in JS). So before you basically have the object "files" as a list of objects zero-indexed like any other JS array. When you add the key, JS simply adds your key to the end of your present keys (in your case 0 and 1).
It seems like you have a list of multimedia objects where each has a title and a list of tracks. The most straightforward way to solve your issue would be as follows:
$fileItem['title'] = 'works';
$fileItem['tracks'] = array(
array(
'title' => 'File',
'format' => 'mp3'
)
);
$json['files'][] = $fileItem;
$fileItem['title'] = 'season1';
$fileItem['tracks'] = array(
array(
'title' => 'Button-1',
'format' => 'wav'
),
array(
'title' => 'Beep-9',
'format' => 'wav'
)
);
$json['files'][] = $fileItem;
Then you JSON encode it and return it as you normally would. You can put the above in a loop as well. I lack enough context to recommend exactly how.
I am using https://stripe.com/docs/api?lang=php#list_charges to get List all Charges but here they specify
count optional — default is 10 A limit on the number of objects to be
returned. Count can range between 1 and 100 items.
and I have thousands of entries, now how can I get all. Though if I set count to 100 it returns 110 records.
You can use the offset argument.
Once you get the 100 transactions, then make another call by adding offset=100 in URL.
This will bring the next 100 transactions, then make offset=200 and so on.
Update:
offset parameter is partly deprecated: API changelog - 2015-09-23
$charges = \Stripe\Charge::all();
foreach ($charges->autoPagingIterator() as $charge) {
// Do something with $charge
}
Reference.
Yes I got it with offset we can get all records.
Here's a PHP example: \Stripe\Charge::all(array("limit" => 3, "offset" => 10));
A Ruby example:
Stripe::Charge.all(limit: 3, offset:3)
As good as the Stripe API docs are, they could be clearer on how to filter.
source: https://stripe.com/docs/api/php#list_charges, https://stripe.com/docs/api/ruby#list_charges
in case offset is deprecated
$result = [];
$created_at = strtotime($request->end_data);
//created_at should be today's date epoch. search google for epoch
$has_more = false;
$a = 0;
do{
print_r($a);
\Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
$temp = \Stripe\BalanceTransaction::all( array(
'limit' => 100,
'created' => array(
'lte' => $created_at,
)
));
$result = array_merge($temp->data,$result);
$created_at = $temp->data[99]->created_at;
//api returns a parameter has_more(boolean), which means there is more
//data or not so you can also put that in while condition, for ex.
// $has_more = $temp->has_more;
$a++;
}while($a < 5);
dd($result);
this worked for me i was able to get 500 records at once as $a < 5 the api hits 5 times and each time created parameter which is lte (less than equal) changes for each api request and return previous records than current request provide. also i am appending the result of each api call to another result array
Unfortunately you can't.
I can see where such a feature would be nice for accounting purposes or whatever, but it's generally a better user experience to implement some sort of paging when displaying copious amounts of data to the user.
If you need absolute control over how many records to display at a time, I would suggest setting up a webhook on the charge.succeeded event and store your charges locally.