Search code examples
phpmysqlfpdf

inserting an array in a fpdf data for graph


I have to put an array inside a graph data this is the original format

$data = array( "DATA_1title" => DATA1, "DATA_2title" => DATA2);

this is statement with php

    $sqlb = "SELECT date_format(datesales, '%M %D %Y') as datesales,
    sum(amount) as amount from sales
    where date(datesales) <= curdate() and
    date(datesales) >= curdate() - interval 6 day
    group by DATE_FORMAT(datesales, '%M %D %Y')";
    $resultb = $conn->query($sqlb);
    while($rowb = $resultb->fetch_assoc())
    {
        $datesales = $rowb['datesales'];
        $amount = $rowb['amount'];
    }

I tried this code

    $sqlb = "SELECT date_format(datesales, '%M %D %Y') as datesales,
    sum(amount) as amount from sales
    where date(datesales) <= curdate() and
    date(datesales) >= curdate() - interval 6 day
    group by DATE_FORMAT(datesales, '%M %D %Y')";
    $resultb = $conn->query($sqlb);
    while($rowb = $resultb->fetch_assoc())
    {
        $datesales=$rowb['datesales'];
        $amount=$rowb['amount'];

        $data = array( "$datesales" => $amount,);
    }

but it only displays one value and not an array of values I also tried this

$sqlb = "SELECT date_format(datesales, '%M %D %Y') as datesales,
    sum(amount) as amount from sales
    where date(datesales) <= curdate() and
    date(datesales) >= curdate() - interval 6 day
    group by DATE_FORMAT(datesales, '%M %D %Y')";
    $resultb = $conn->query($sqlb);
    while($rowb = $resultb->fetch_assoc())
    {
        $data = array( "$rowb['datesales']" => $rowb['amount'],);
    }

but I got the same result what I want is that instead of manually typing the key and value every time, it can instead be pulled out of the database with the sql statement I did.

Instead of this 
$data = array( "DATA_1title" => DATA1, "DATA_2title" => DATA2);
I want it to be like this
**SQL STATEMENT HERE**
$data = array( "$datetimes" => $amount);

$datetimes refers to the alias in the sql statement which is an array
so is $amount

Solution

  • You can simply use one value as a key and the other as a value. For example:

    // instead of your while loop
    foreach($resultb as $rowb) {
        $data[$rowb['datesales']] = $rowb['amount'];
    }