Reputation: 711
Hi I was able to display a json data(List of conversation result) but I want to display them just like how I display records from database like this:
foreach(res->fetchAll(PDO::FETCH_ASSOC) as result):
$username = $result['username'];
$message = $result['message'];
endforeach;
are there any similar procedure like this but with json_encode()
?
here is my php script
$sql6="SELECT msgid FROM thread WHERE combination1=:msgids OR combination2=:submsgids LIMIT 1";
$msg_id = $con4->prepare($sql6);
$msg_id->bindParam(':msgids', $comb, PDO::PARAM_STR);
$msg_id->bindParam(':submsgids', $comb, PDO::PARAM_STR);
$msg_id->execute();
$msgd = $msg_id->fetchColumn();
$tbpre = $msgd;
$sql7 = "SELECT * FROM ".$tbpre."chat_conversation WHERE msgid=:chat";
$stmt7=$con3->prepare($sql7);
$stmt7->bindValue( 'chat', $msgd, PDO::PARAM_STR);
$stmt7->execute();
$rows = $stmt7->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);
This php script are getting its data from my ajax script below
function AjaxRetrieve()
{
var rid = document.getElementById('trg').value,
data = {chat: uid, rid: rid, name: user};
$.ajax({
url: "includes/getChat.php",
type: "GET",
data: data,
dataType: 'json',
success: function(result){
var container = $("#clog");
$.each(result, function(i, message) {
$.each(message, function(key, value) {
container.append($('<p />').html(key + ':' + value));
});
});
}
});
The php and javascript are working fine the JSON are being displayed.. But I'm trying to figure out if there is a way that I can display only the username and the message just how I use the foreach
statement that I mentioned above...
Upvotes: 1
Views: 114
Reputation: 2173
I may be misunderstanding, but you should be able to filter what is displayed by adding a condition with an if
$.each(result, function(i, message) {
$.each(message, function(key, value) {
if (key == "username" || key == "message")
container.append($('<p />').html(key + ':' + value));
});
});
Upvotes: 1