1

I've got this code

Javascript

$http({
    method: 'GET',
    url: '../../php/getMesas.php',

  }).then(function successCallback(response) {
      $scope.mesas = response.data;
  }, function errorCallback(response) {
      $scope.mesas = 'No Response';
  });

That is trying to get some table numbers, but when this really works it shows me the parameter name and the value, i need only the value, what can i do to get only the value so not the parameter name? I'm using PHP as database connector.

PHP Code

<?php 
include('base.php');
$data = array();
$result = mysql_query('SELECT table_number FROM waiters_assigned ORDER BY id',$connect);
if(mysql_num_rows($result) > 0){
    while($row = mysql_fetch_assoc($result)){ 
        $data[] = $row; 
    } 
} else { 
    echo "0 results"; 
}; 
echo json_encode($data);
mysql_close($connect); 
?>

The result is this one: {"table_number":"3"} what i need is just: 3

3
  • can you provide an example of the returned data, and what you expect the result to be please. Similarly the PHP code used to generate the response would be lovely Commented Feb 10, 2017 at 2:19
  • ok! right. Here's the php code. <?php include('base.php'); $data = array(); $result = mysql_query('SELECT table_number FROM waiters_assigned ORDER BY id',$connect); if(mysql_num_rows($result) > 0){ while($row = mysql_fetch_assoc($result)){ $data[] = $row; } } else { echo "0 results"; }; echo json_encode($data); mysql_close($connect); ?> the result is this one: {"table_number":"3"} what i need is just: 3. Commented Feb 10, 2017 at 2:20
  • mysql_ functions are deprecated; use the mysqli_ ones. Commented Feb 10, 2017 at 2:33

3 Answers 3

1

Firstly, please dont use mysql_query you should have a look at PDO or at the very least mysqli, for security and because it is deprecated.

As for returning just the number, update your while to return the field you desire:

while($row = mysql_fetch_assoc($result)){ $data[] = (int)$row['table_number']; }

When looking at your PHP i believe you're actually getting [{"table_number":"3"}], as you're json_encodeing an array.

The reason updating your PHP is better than updating your Javascript is that you appear to be returning an array of objects currently, when you actually want to return an array of numbers. Doing things the JS way you'll need to cycle through the response, parseInt on the strings, and strip the object down to a number. Far easier and more efficient to just send the correct data.

Sign up to request clarification or add additional context in comments.

Comments

0

In your php code use mysqli or pdo extension because mysql extension is deprecated.

$http({
    method: 'GET',
    url: '../../php/getMesas.php',

  }).then(function successCallback(response) {
      $scope.mesas = response.data.table_number;//outputs 3
  }, function errorCallback(response) {
      $scope.mesas = 'No Response';
  });

Comments

0

It's very simple:

$http({
    method: 'GET',
    url: '../../php/getMesas.php',
}).then(function successCallback(response) {
    $scope.mesas = response.data ? "" + response.data.table_number : "";
}, function errorCallback(response) {
    $scope.mesas = 'No Response';
});

Comments

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.