1

I need to create two variables(not sure whether it can also called as an array) by using existing arrays. Let's day I need two variables such as,

$Date =['2022-01-10', '2022-01-11', '2022-01-12', '2022-01-13'];
$Value =[91, 12,15,16];

I am planning to get these two values by using a sql query.

$query = "select Date,Value from shop order by Date asc;";

$result=mysqli_query($conn,$query);

while( $row = mysqli_fetch_assoc( $result)){
    $new_array[] = $row;
}

$date_array = array();
$value_array=array();
foreach($new_array as $array)
{       
   $date_array[]=$array['Date'];
   $value_array[]=$array['Value'];
   

}
$date_array_vals=array_values($date_array);
$value_array_vals=array_values($value_array);

I have now $date_array_vals and $value_array_vals as below.

Array ( [0] => 2022-01-02 [1] => 2022-01-03 [2] => 2022-01-04 [3] => 2022-01-05 )
Array ( [0] => 20.8 [1] => 201.5 [2] => 25.6 [3] => 205.5 ) 

How I could retrieve $Date and $Value as above mentioned?

update: Eventually I need to add these two arrays to following code.

$chartConfig = "{
    type: 'bar',
    data: {
      labels: [//$date should added],
      datasets: [{
        data: [//$value should added],
        backgroundColor: ['#b87333', 'silver', 'gold', '#e5e4e2'],
      }]
    },
    options: {
      title: {
        display: true,
        text: 'Density of Precious Metals, in g/cm^3',
      },
      legend: {
        display: false    
      }
    }
  }";
 
9
  • You don't need the last two assignments. $date_array and $value_array are already what you want. Commented Jan 25, 2022 at 2:36
  • But those assignments are merely redundant, not a problem. Why doesn't this code do what you want? Commented Jan 25, 2022 at 2:37
  • You also don't need $new_array. Just push onto the result arrays in the while loop. Commented Jan 25, 2022 at 2:37
  • Just rename $date_array to $Date and $value_array to $Value and you'll have the two variables that you said you want. Commented Jan 25, 2022 at 2:38
  • Can I get as I mentioned?comma seperated and wrapped with singla commas? Commented Jan 25, 2022 at 2:40

2 Answers 2

1

Create $chartconfig using json_encode() after creating the array.

$query = "select Date,Value from shop order by Date asc;";

$result=mysqli_query($conn,$query);

while( $row = mysqli_fetch_assoc( $result)){
    $Date = $row['Date'];
    $Value = floatval($row['Value']);
}

$chart = [
    "type" => 'bar',
    "data" => [
        "labels" => $Date,
        "datasets" => [[
            "data" => $Value,
            "backgroundColor" => ['#b87333', 'silver', 'gold', '#e5e4e2'],
        ]]
    ],
    "options" => [
        "title" => [
            "display" => true,
            "text" => 'Density of Precious Metals, in g/cm^3',
        ],
        "legend" => [
            "display" => false    
        ]
    ]
];

$chartConfig = json_encode($chart);
Sign up to request clarification or add additional context in comments.

2 Comments

Seems $Date and $Value is not in the fomat what I need. This was not worked
You may need to convert the values to float, I've added that to the answer.
0

Do this $date = implode(',',$Date); And in html page Do this var date = "{$date}"

var datearr = date.split(',');

for value do the same above with

var valuearr = value.split(',').map(Number);

2 Comments

i just noticed you're doing $chartconfig in php file so just added $date in label
Normally label is as labels: [2022-01-12,2022-01-13 ...], So don't we need include [] as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.