Home » Javascript » Problems to generate correct graphics with php, google charts and Postgresql

Problems to generate correct graphics with php, google charts and Postgresql

Posted by: admin October 29, 2018 Leave a comment

Questions:

Currently I am trying to show through a graph the query of a postgresql record from php but I only manage to show one of the consulted fields. For example I want to show the total packages sent by a mail office in total was received by the post office 2275 packages and those were sent 353 when I run the following described only brings me the packages that were received and sent not, without However I run a pg_fetch_assoc to see what data the query is bringing to me and it shows me in effect 2275 and 353 but in the pie chart it only brings me one.

<?php

include ("conexion.php");
error_reporting(-1);
ini_set('display_errors', 'On');

$query = "SELECT recived, sends FROM postal_recived WHERE id_sends = '123456'";
$result = pg_query($query);
$result2 = pg_query($query);

//this step is working well

$felo = pg_fetch_assoc($result2);
echo $felo['recived'] . "<br>";
echo $felo['sends'];
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
        google.charts.load('current', {'packages':['corechart']});
        google.charts.setOnLoadCallback(drawChart);

        function drawChart() {

            var data = google.visualization.arrayToDataTable([
                ['recived', 'sends'],
                <?php
                if($result){
                    while($row = pg_fetch_assoc($result)){
                        echo "['".$row['recived']."', ".$row['sends']."],";
                    }
                }
                ?>
            ]);

            var options = {
                title: 'Postal Office',
                width: 900,
                height: 500,
            };

            var chart = new google.visualization.PieChart(document.getElementById('piechart'));

            chart.draw(data, options);
        }
    </script>
</head>
<body>
<!-- Display the pie chart -->
<div id="piechart"></div>
</body>
</html>
Answers: