0

related to this question here

I have multiple ajax calls, each with their own query in the data.php file. There is only one data.php file here, but in practice there would be 2 with their own query. Is there a way to have the 1 data.php file with multiple queries in the 1 file?

Here is my code:

google.charts.load('current', {
  packages: ['corechart', 'bar']
}).then(function () {
  $.ajax({
    url: 'data.php',
    dataType: 'json'
  }).done(drawChart1);

  $.ajax({
    url: 'data.php',
    dataType: 'json'
  }).done(drawChart2);
});

function drawChart1(result) {
  ...
}

function drawChart2(result) {
  ...
}

data.php

<?php
require_once 'database.php';
$stmt = $conn->prepare('select * from product');
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
echo json_encode($results); 
?>

database.php

<?php
$conn = new PDO('mysql:host=192.168.99.100;dbname=demo','root', 'root');
?>
HattrickNZ
  • 4,373
  • 15
  • 54
  • 98
  • 1
    Just thinking out loud here: Send some POST data along with the Ajax requests and use `if(!empty($_POST['foo'])){ // query }` in your PHP file. That way you can control which query is being executed. – icecub May 08 '18 at 02:08

2 Answers2

1

You can achieve this by storing the results from multiple calls into an array:

data.php

$stmt = $conn->prepare('select * from product');
$stmt->execute();
$products = $stmt->fetchAll(PDO::FETCH_OBJ);

$stmt = $conn->prepare('select * from customer');
$stmt->execute();
$customer = $stmt->fetchAll(PDO::FETCH_OBJ);

$data = [
    'products' => $products,
    'customers' => $customers
];

echo json_encode($data); 

then in your javascript:

google.charts.load('current', {
  packages: ['corechart', 'bar']
}).then(function () {
  $.ajax({
    url: 'data.php',
    dataType: 'json'
  }).done(drawCharts(data));
});

From your drawCharts function, access each result set like data.products or data.customers

BizzyBob
  • 12,309
  • 4
  • 27
  • 51
1

In data.php you can create different functions and you can pass function in ajax call.

    google.charts.load('current', {
          packages: ['corechart', 'bar']
    }).then(function () {
      $.ajax({
        url: 'data.php',
        data: {'function': 'firstQuery'},
        dataType: 'json'
      }).done(drawChart1);

      $.ajax({
        url: 'data.php',
        data: {'function': 'secondQuery'},
        dataType: 'json'
      }).done(drawChart2);
    });

data.php

    $function = isset($_REQUEST['function']) ? $_REQUEST['function'] : '';

    if($function != ""){
        $function();
    }

    function firstQuery(){
        $stmt = $conn->prepare('select * from product');
        $stmt->execute();
        $products = $stmt->fetchAll(PDO::FETCH_OBJ);

        $data = [
            'products' => $products,
        ];

        echo json_encode($data); 
    }

    function secondQuery(){
        $stmt = $conn->prepare('select * from customer');
        $stmt->execute();
        $customer = $stmt->fetchAll(PDO::FETCH_OBJ);

        $data = [
            'customers' => $customers
        ];

        echo json_encode($data); 
    }
Sadhu
  • 850
  • 2
  • 9
  • 18