0

This works when we do this:

$db = $connection->messages;
$collection = $db->messagesCollection;
$messageArray = $collection->find(array('IDto' => '4'));
foreach($messageArray as $messageData){
    $messageFrom = $messageData['IDfrom'];
    $messageTo = $messageData['IDto'];
    $messageTitle = $messageData['messageTitle'];
    $messageIfRead = $messageData['ifRead'];    
}

$JSONData = array('true', $messageFrom, $messageTo, $messageTitle, $messageIfRead); 
echo $_GET['callback']."(".json_encode($JSONData).")";

But when we do this:

$db = $connection->messages;
$collection = $db->messagesCollection;
$messageArray = $collection->find(array('IDto' => '4'));
$JSONData = array('true', $messageArray); 
echo $_GET['callback']."(".json_encode($JSONData).")";

and in the Javascript do this:

$.getJSON("mySite.com/pullData/getMail.php?callback=?",{
request: requestVar},
function(recievedData) {
    alert(recievedData);
})

We get an alert of true, [object Object] When using console log we get Object {}

How do we send that table data correctly?

0

1 Answer 1

1

I believe your biggest problem is the MongoCursor:

$messageArray = $collection->find(array('IDto' => '4'));
$JSONData = array('true', $messageArray); 
echo $_GET['callback']."(".json_encode($JSONData).")";

You are trying to encode an object of MongoCursor there, hence the string representation is [object Object].

Try:

$messageArray = $collection->find(array('IDto' => '4'));
$JSONData = array('true', iterator_to_array($messageArray)); 
echo $_GET['callback']."(".json_encode($JSONData).")";

Instead. All find functions in MongoDB return a MongoCursor, the reason why your first code works is because you iterate the cursor building up your objects:

foreach($messageArray as $messageData){
    $messageFrom = $messageData['IDfrom'];
    $messageTo = $messageData['IDto'];
    $messageTitle = $messageData['messageTitle'];
    $messageIfRead = $messageData['ifRead'];    
}

Note as well that the default json_encode of a document will contain objects, such as MongoId and MongoDate that do not encode too well into reusable JSON syntax. As such you will need to handle these types yourself.

Edit

Maybe a better way is to actually redo the indexes manually:

$messageArray = $collection->find(array('IDto' => '4'));
$d = array();
foreach($messageArray as $row){
    $d = $row;
}

$JSONData = array('true', $d); 
echo $_GET['callback']."(".json_encode($JSONData).")";

This way you will have a 0 based incrementing index instead of the ObjectId as each index base.

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

6 Comments

Ok now i'm getting all of the data as objects, how to I grab them from the brackets from their array key like receivedData[1]['IDFrom']? right now it looks like this : i46.tinypic.com/2j5xdau.png
@Silas What you can do is (since I noticed your using JQuery): $.each(recievedData, function(i, item){ console.log(item._id.$id); }); You might wanna pull out the indexes manually though unless those object id indexes work ok with your application.
this is awesome! it's starting to work. Thanks a bunch for your time. is there a way I can "foreach" this in javascript/jquery so I can handle multiple rows?
recievedData[1]['userIdTo'] when i do that, I get the proper response for one row. How do I get the other rows that come through?
@Silas Ha correction to the code I gave you for the loop: $.each(recievedData[1], function(i, item){ console.log(item._id.$id); }); should be accessing index one there for the var since the data is actually in position 1
|

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.