PHP + Jquery + JSON

2

I have function calling an ajax. As below:

    function verifica(){ 
      var meuid = $('.meuid').attr('id');   
      var datas = "user="+meuid;        
      $.ajax({
             type: "GET",
             url: 'sys/stream2.php',                                  
             data: datas                
          }).done(function( data ) {                                         
            //alert(data);
            $('#nome').html(data);                
          });
    }

And in the stream, I have a select for it along with a foreach as follows:

foreach ($gUsuarios as $usuarios) {
        $agora = $usuarios['AGORA'];   
        if ($agora >= $usuarios['cUsu_Limite']) {            
            echo json_encode(array('usuarioon' => $usuarios['cUsu_ID'],
                                   'status' => 'fa fa-circle-o text-red'));            
        }else{    
            echo json_encode(array('usuarioon' => $usuarios['cUsu_ID'],
                                       'status' => 'fa fa-circle-o text-green'));
        }

and the return, it presents me like this:

{"usuarioon":"1","status":"fa fa-circle-o text-red"}
{"usuarioon":"3","status":"fa fa-circle-o text-red"}    

That means that it returns me correctly, but the problem is: how do you split this individually? I've tried with ParseJson , but it did not work, I think it's because I'm returning array , within foreach . What I really want is:

In the element it changes the class to: 'fa fa-circle-o text-green', but for this I need to know which user, then I bring the user's code. I need to look like this:

  

$ ('#' + data.user) .addClass (data.status);

Someone has already gone through this, can you help me?

    
asked by anonymous 09.02.2016 / 01:53

1 answer

0

This problem is the same as jquery ui autocomplete + php does not work

You are generating more than one JSON.

Change PHP:

Change this:

foreach ($gUsuarios as $usuarios) {
        $agora = $usuarios['AGORA'];   
        if ($agora >= $usuarios['cUsu_Limite']) {            
            echo json_encode(array('usuarioon' => $usuarios['cUsu_ID'],
                                   'status' => 'fa fa-circle-o text-red'));            
        }else{    
            echo json_encode(array('usuarioon' => $usuarios['cUsu_ID'],
                                       'status' => 'fa fa-circle-o text-green'));
        }
}

So:

$jsonAux = array(); 
// Algumas versões do PHP (acho que < PHP 7) exibem no erro se não houver isso!

foreach ($gUsuarios as $usuarios) {
        $agora = $usuarios['AGORA'];   
        if ($agora >= $usuarios['cUsu_Limite']) {            
            $jsonAux[] = array('usuarioon' => $usuarios['cUsu_ID'],
                                   'status' => 'fa fa-circle-o text-red');            
        }else{    
            $jsonAux[] = array('usuarioon' => $usuarios['cUsu_ID'],
                                       'status' => 'fa fa-circle-o text-green');
        }
}

echo json_encode($jsonAux);

Result:

[{"usuarioon":"1","status":"fa fa-circle-o text-red"},{"usuarioon":"3","status":"fa fa-circle-o text-red"}]

Meanwhile in JQuery:

You need to create a loop to execute each array defined above, so it will execute each time.

function verifica(){ 
  var meuid = $('.meuid').attr('id');   
  var datas = "user="+meuid;        
  $.ajax({
         type: "GET",
         url: 'sys/stream2.php',                                  
         data: datas                
      }).done(function( data ) {                                         

         // Cria um loop para cada array do data:
         $.each(data, function(a) {
            $('#'+this.usuarioon).addClass(this.status);         
         }


      });
}

Test me:

For viewing reasons, addClass was changed to text !

var data = [{
  "usuarioon": "1",
  "status": "fa fa-circle-o text-red"
}, {
  "usuarioon": "3",
  "status": "fa fa-circle-o text-red"
}];


$(':button').click(function() {

  $.each(data, function(a) {
    $('#' + this.usuarioon).text(this.status);
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<x id='0'>Meu é ID 0</x>
<br>
<x id='1'>Meu é ID 1</x>
<br>
<x id='2'>Meu é ID 2</x>
<br>
<x id='3'>Meu é ID 3</x>
<br>
<x id='4'>Meu é ID 4</x>
<br>

<button>SIMULAR AJAX</button>
    
03.04.2016 / 17:02