0

I know JSON could help me to process multiple data from php by storing all data in an array. But what about one of the data is in a loop? See the code below:

$data1='<p>hehe</p>';
$data2='<h1>wawa</h1>'; 
$backarr=array($data1,$data2);

echo json_encode($backarr);

Now I have something that want to be data3

$sql="SELECT username FROM users WHERE username != '$username'";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
    $names = $row['username'];
    echo '<p>'.$names.'</p>';
} 

Is it possible to do that? I want the ajax to receive all of the three data, how to process the data3?

3 Answers 3

2

It's not that hard really:

$names = array();
while($row = mysql_fetch_array( $result )) 
{
   array_push($names, "<p>" . $row['username'] . "</p>");
}

echo json_encode($names);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, but what about other two data? data1 and data2 are also stored in $names?
0

Try this

$backarray = array();
$backarray['data1'] = '<p>hehe</p>';
$backarray['data2'] = '<h1>wawa</h1>';


$sql="SELECT username FROM users WHERE username != '$username'";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
    $names[] = $row['username'];
}

//Push $names array into $backarray
$backarray['names'] = $names;

var_dump(json_encode($backarray));

which will output something like under with all names in an array.

'{"data1":"<p>hehe<\/p>","data2":"<h1>wawa<\/h1>","names":["Name1","Name2","Name3","Name4","Name5","Name6"]}'

4 Comments

I have tried all of the answers, they seem work, but I do not know how to pass them in ajax? Thank you.
@user2049259 Assuming responseObject is your response object received from ajax call, responseObject.data1 will give you data1, responseObject.data2 will give you data2 responseObject.names will give you an array of names which you can iterate by using a loop like responseObject.names[1],responseObject.names[2]...
@user2049259 Are you using jQuery?
No, I am not using JQuery, just raw ajax, your answer works perfectly for me, thank you very much! @LoneWOLFs
0

You could combine all in one array and then start to encode.

$dataSet = array();
$data1='<p>hehe</p>';
$data2='<h1>wawa</h1>'; 
$dataSet[] = $backarr = array($data1,$data2);


$sql="SELECT username FROM users WHERE username != '$username'"; 
$result = mysql_query($sql) or die(mysql_error()); 
while($row = mysql_fetch_array( $result )) { 
    $names[] = $row['username'];
} 
$dataSet[] = $names;

echo json_encode($dataSet);

EDIT::

And here the jQuery stuff:

jQuery.post('url/script.php',{fieldName : fieldValue},function(res){
   console.log(res);
   var res = jQuery.parseJSON(res);
   console.log(res);
});

Pure AJAX:

<script type="application/javascript">
function loadJSON()
{
   var data_file = "http://www.example.com/data.json";
   var http_request = new XMLHttpRequest();
   try{
      // Opera 8.0+, Firefox, Chrome, Safari
      http_request = new XMLHttpRequest();
   }catch (e){
      // Internet Explorer Browsers
      try{
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      }catch (e) {
         try{
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         }catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
         }
      }
   }
   http_request.onreadystatechange  = function(){
      if (http_request.readyState == 4  )
      {
        // Javascript function JSON.parse to parse JSON data
        var jsonObj = JSON.parse(http_request.responseText);

        // jsonObj variable now contains the data structure and can
        // be accessed as jsonObj.name and jsonObj.country.
        document.getElementById("Name").innerHTML =  jsonObj.name;
        document.getElementById("Country").innerHTML = jsonObj.country;
      }
   }
   http_request.open("GET", data_file, true);
   http_request.send();
}
</script>

1 Comment

Thank you, but how to receive the data in ajax? $('data1').innerHTML=myownrequest2result[0]; $('data2').innerHTML=myownrequest2result[1]; $('data3').innerHTML=myownrequest2result[2]; I hope the [2] should be the data3..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.