[SOLVED] Split strings from php and return values

I am trying to pass data from php to js and return the values like in the code. The problem I am facing is where I am testing I have hardcoded to values into a string in js and the code works fine. What I need to do however, is grab these values from php. I have tried various options such as var str = data; but it is not grabbing any data. I would be grateful if someone could point out my error. thanks


  $brtvsql = "SELECT * FROM act WHERE activity='Box Retrieval' AND new = '1'";
  $result = mysqli_query($conn, $brtvsql);
  $brtv_row = mysqli_fetch_assoc($result);
  $count = mysqli_num_rows($result);
  echo 'brtv' . ' ' . $count;

  $brtnsql = "SELECT * FROM act WHERE activity='Box Return' AND new = '1'";
  $result = mysqli_query($conn, $brtnsql);
  $brtn_row = mysqli_fetch_assoc($result);
  $count = mysqli_num_rows($result);
  echo 'brtn' . ' ' . $count;

js code

setInterval(function() {
  $.get('/domain/admin/loadActions.php', function(data) {
    var str = "brtv 30;brtn 0";
    var data = {};
    str.split(";").forEach(function(s) {
      data[s.split(" ")[0]] = s.split(" ")[1];

    // Check the result
    if(data["brtv"]) {
      console.info("brtv: %s", data["brtv"]);

    if(data["brtn"]) {
      console.info("brtn: %s", data["brtn"]);
}, 15000);

For the benefit of others seeing this, what needed to be done to solve the problem?

1 Like

It was actually very simple. Just by changing to this. var str = data; Hope it helps someone else. Sorry should have posted solution. Cheers

1 Like

FTR, you might find it easier to hand data over this way in JSON format and condense your queries into a single query:

  $brtvsql = "SELECT activity,COUNT(new) AS num FROM act WHERE new = '1' GROUP BY activity";
  $result = mysqli_query($conn, $brtvsql);
  $resarray = mysqli_fetch_array($result,MYSQLI_ASSOC);
  $dout = array();
  foreach($resarray['activity'] as $idx => $actname) {
     $dout[$actname] = $resarray['num'][$idx];
  echo json_encode($dout);
 $.get('/domain/admin/loadActions.php', function(data) {
      $(".retrievals").text(data["Box Retrieval"])
      $(".retrievals").text(data["Box Return"])

EDIT: Wait… assoc works the other way around. need to pivot the table.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.