pass array to chart.js option [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
i have this array:
array (
0 => 'rgba(202, 63, 20, 1)',
1 => 'rgba(35, 14, 225, 1)',
2 => 'rgba(73, 128, 13, 1)',
3 => 'rgba(238, 62, 10, 1)',
4 => 'rgba(85, 152, 95, 1)',
5 => 'rgba(57, 156, 150, 1)',
)
but i have to use it as a parameter in a chart.js chart and i need this format:
['rgba(202, 63, 20, 1)', 'rgba(35, 14, 225, 1)', 'rgba(73, 128, 13, 1)']
and i have to do it in PHP.
some ideas ?
thanks
C.

This is how I fixed it: I pass my rgba array to the "backgroundColor" option in Chart.js with this string:
backgroundColor: <?php echo json_encode($ColorArray); ?>,
and now it works.
My mistake was in passing the php variable directly to the backgroundColor option of chart.js like this:
backgroundColor: $ColorArray,

Related

Implode php array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an array like this:
$Array = [
[1, 33, 55, 18],
[8, 9, 12, 67],
[3, 33, 76, 88],
];
And I want add ; to output like this:
1, 33, 55, 18;
8, 9, 12, 67;
3, 33, 76, 88
Anyone know how to do this?
Use array map to achieve this
$temp = array_map(function($item){
return implode(", ", $item);
}, $Array);
foreach($temp as $v){
// implode with ';' and \n for line break
// you can use "<br/>" if you are using web.
echo $v.";\n";
}
Demo.
One looper:
foreach($Array as $v){
echo implode(", ",$v).";\n";
}
Demo.
Output
1, 33, 55, 18;
8, 9, 12, 67;
3, 33, 76, 88;

How to get array key with less than values

I have 2 arrays:
$arr_1=array(200, 300, 200, 200);
$arr_2=array(
1 => array(70, 90, 70, 20),
2 => array(115, 150, 115, 35),
3 => array(205, 250, 195, 55),
4 => array(325, 420, 325, 95),
5 => array(545, 700, 545, 155)
);
Now I need some way to get array keys for arrays in $arr_1 where all their values are less than all values from $arr_2.
In the above example it must return key 1 AND key 2 from $arr_2 without using a foreach loop.
You can use array_filter to filter the elements (it preserves keys) and then pass the result to array_keys to receive an array of keys.
Also, your condition can be spelled this way: "return subarrays from $arr_2 where highest value is smaller than smallest value of $arr_1."
$arr_1=array(200, 300, 200, 200);
$arr_2=array(
1 => array(70, 90, 70, 20),
2 => array(115, 150, 115, 35),
3 => array(205, 250, 195, 55),
4 => array(325, 420, 325, 95),
5 => array(545, 700, 545, 155)
);
$filtered = array_filter($arr_2, function($value) use ($arr_1) {
return max($value) < min($arr_1);
});
$keys = array_keys($filtered);
var_dump($keys);
If you are only interested in comparing the subarrays against the lowest value in $arr_1, then best practices dictates that you declare that value before entering the array_filter(). This will spare the function having to call min() on each iteration. (Demo)
$arr_1=[200,300,200,200];
$arr_2=[
1=>[70,90,70,20],
2=>[115,150,115,35],
3=>[205,250,195,55],
4=>[325,420,325,95],
5=>[545,700,545,155]
];
$limit=min($arr_1); // cache this value, so that min() isn't called on each iteration in array_filter()
$qualifying_keys=array_keys(array_filter($arr_2,function($a)use($limit){return max($a)<$limit;}));
var_export($qualifying_keys);
/*
array(
0=>1,
1=>2,
)
*/

How to make a table like this with fpdf using PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How do you make a table like this with FPDF using PHP?
I can't seem to figure out how to do this with $this->Cell.
OK .. let's try. Just an example .. an idea. ok ?
$pdf->SetFillColor(255,0,0);
$pdf->SetTextColor(255);
$pdf->SetDrawColor(128,0,0);
$pdf->SetLineWidth(.3);
// Title row
$pdf->SetFont('', 'B');
$pdf->Cell(100, 8, "LEARNING POTENTIAL", 1, 0, 'L', true);
$pdf->SetFont('', '');
$pdf->Cell(20, 8, "Score", 1, 0, 'C', true);
$pdf->Cell(20, 8, "Results", 1, 0, 'C', true);
$pdf->>Ln();
// Data rows
// (loop) foreach($data as $row) {
$pdf->SetFont('', 'B');
$pdf->Cell(100, 8, "Reasoning", "LTR", 0, 'L');
$pdf->Cell(20, 8, "", "LTR");
$pdf->Cell(20, 8, "", "LTR");
$pdf->>Ln();
$pdf->SetFont('', 'B');
$pdf->Cell(100, 8, "The ability to interpret information and", "LR", 0, 'L');
$pdf->SetFont('', '');
$pdf->Cell(20, 8, "6", "LR");
$pdf->Cell(20, 8, "Effective", "LR");
$pdf->>Ln();
$pdf->SetFont('', 'B');
$pdf->Cell(100, 8, "drawing accurate conclusions", "LBR", 0, 'L');
$pdf->Cell(20, 8, "", "LBR");
$pdf->Cell(20, 8, "", "LBR");
$pdf->>Ln();
// (end loop)
I divided each data row in 3 rows.

Sending multidimensional array from jQuery to php

I am passing an array from jQuery to php.
The array is generated from a table with this code:
var stitchChartArray = [];
var row = 0;
// turn stitch chart into array for php
$('#stitchChart').find('tr').each(function (index, obj) {
//first row is table head- "Block #"
if(index != 0){
stitchChartArray.push([]);
var TDs = $(this).children();
$.each(TDs, function (i, o) {
var cellData = [$(this).css('background-color'), $(this).find("img").attr('src')];
stitchChartArray[row].push(cellData);
});
row++;
}
});
In console it looks like this:
[[["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(75, 75, 60)", "symbols/184.png"], ["rgb(75, 90, 60)", "symbols/177.png"], 7 more...], [["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(75, 75, 60)", "symbols/184.png"], ["rgb(75, 90, 60)", "symbols/177.png"], 7 more...], [["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(75, 75, 60)", "symbols/184.png"], 7 more...], [["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(98, 119, 57)", "symbols/210.png"], 7 more...], [["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(150, 150, 195)", "symbols/72.png"], 7 more...], [["rgb(75, 165, 105)", "symbols/187.png"], ["rgb(134, 158, 134)", "symbols/64.png"], ["rgb(165, 180, 180)", "symbols/171.png"], 7 more...], [["rgb(60, 150, 75)", "symbols/189.png"], ["rgb(120, 120, 90)", "symbols/225.png"], ["rgb(143, 163, 89)", "symbols/209.png"], 7 more...]]
It represents each row of a table->each cell of row->[0]rgb value of bg of cell [1]icon in cell.
This jQuery code returns the correct element(and rgb value) from the array:
alert(stitchChartArray[1][1][0]); //row 1,cell 1, first value(rgb)
But when it gets sent to the php script with this:
$.post('makeChartPackage.php', {'stitchChart[]': stitchChartArray }, function(data){
alert(data);
});
The php throws an error:
Cannot use string offset as an array in /Users/tnt/Sites/cross_stitch/makeChartPackage.php on line 33
$stitchChart = $_POST['stitchChart'];
echo $stitchChart[1][1][0]; //line 33
I am assuming I am either constructing the array incorrectly or passing it to the php script incorrectly.
EDIT:
I did this to return the array to jQuery:
$stitchChart = $_POST['stitchChart'];
print_r($stitchChart);
And here was the result:
Array
(
[0] => rgb(75, 90, 60),symbols/177.png,rgb(75, 75, 60),symbols/184.png,rgb(75, 90, 60),symbols/177.png,rgb(98, 119, 57),symbols/210.png,rgb(180, 195, 105),symbols/388.png,rgb(165, 165, 120),symbols/235.png,rgb(75, 75, 60),symbols/184.png,rgb(90, 90, 45),symbols/195.png,rgb(120, 120, 75),symbols/156.png,rgb(105, 105, 105),symbols/163.png
[1] => rgb(105, 105, 105),symbols/163.png,rgb(75, 75, 60),symbols/184.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 90, 60),symbols/177.png,rgb(165, 165, 120),symbols/235.png,rgb(120, 120, 75),symbols/156.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 90, 60),symbols/177.png,rgb(105, 105, 105),symbols/163.png,rgb(120, 120, 90),symbols/225.png
[2] => rgb(105, 105, 105),symbols/163.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 75, 60),symbols/184.png,rgb(75, 90, 60),symbols/177.png,rgb(98, 119, 57),symbols/210.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 75, 60),symbols/184.png,rgb(105, 105, 105),symbols/163.png,rgb(120, 120, 90),symbols/225.png,rgb(105, 105, 105),symbols/163.png
It appears the array is not multidimensional?
$_POST['stitchChart'] in the context you have addressed it there is (effectively) a JSON representation of a multidimensional array, stored as a string. When you treat a string as a multidimensional indexed array in PHP, you will get that error. The first [x] is treated as a "string offset" - i.e. the character at position x - but the next and any subsequent [x] addresses can only be treated as arrays (you cannot get a substring of a single character) and will emit the error you have received.
To access your data as an array in PHP, you need to use json_decode():
$stitchChart = json_decode($_POST['stitchChart'],TRUE);
echo $stitchChart[1][1][0];
EDIT
Because the jQuery data argument seemingly can't deal with multidimensional arrays, you should use Douglas Crockford's JSON-js library and pass the result into data as a string. NB: use json2.js.
Here is how you could do this:
stitchChartArray = JSON.stringify(stitchChartArray);
$.post('makeChartPackage.php', {'stitchChart': stitchChartArray }, function(data){
alert(data);
});
If you use this code, my original PHP suggestion should work as expected.

converting a php array into a database schema

Currently I am storing adjacencies in a php file in an array. Here's a sample of it:
$my_neighbor_lists = array(
1=> array(3351=> array (2, 3, 5 , 6, 10)),
2=> array(3264=> array (322, 12, 54 , 6, 10), 3471=>array (122, 233, 35 , 476, 210)),
3=> array(3309=> array (52, 32, 54 , 36, 210), 3469=>array (152, 32, 15 , 836, 10)),
etc
I would like to basically migrate this into a db. Any suggestions on how many table I should have? I am looking at three tables here.
two tables:
1. vertices (id)
2. edgecost (idfrom, idto, time, cost)

Categories