2

This function works fine, but it only returns one value. What I'm missing here?

Here's my code:

  <script type='text/javascript'>
$(function () {

<?php
//Get the names and id's
$get_info=mysql_query("select * from table where id = '1'");

if($get_info){
while($row_info=mysql_fetch_array($get_info))
{
$username=$row_info['name'];
$user_id=$row_info['profile_id'];
?>


onDataRequest:function (mode, query, callback) {
 var data = [

{ id:<?php echo $user_id;?>, name:'<?php echo $username;?>', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' }

 ];

     data = _.filter(data, function(item) { return item.name.toLowerCase().indexOf(query.toLowerCase()) > -1 });
     callback.call(this, data);
    }
  });

<?php
}}
?>
});
</script>

Expected result:

{
    id: 295,
    name: 'Mike',
    'avatar': 'http: //cdn0.4dots.com/i/customavatars/avatar7112_1.gif',
    'type': 'contact'
},
{
    id: 296,
    name: 'John',
    'avatar': 'http: //cdn0.4dots.com/i/customavatars/avatar7112_1.gif',
    'type': 'contact'
}

Actual output:

{
    id: 295,
    name: 'Mike',
    'avatar': 'http: //cdn0.4dots.com/i/customavatars/avatar7112_1.gif',
    'type': 'contact'
}

It just returns 1 item.

2 Answers 2

2

You database fetch loop isn't preserving each fetched value. You simply overwrite the previous fetch call with the current fetch. perhaps you meant something like this:

while ($row_info = mysql_fetch_asssoc($result)) {
    $username[] = $row_info['name'];
    $user_id[] = $row_info['profile_id'];
}

The [] notation on the vars tells PHP to treat the vars as array and push the new values into the array.

You'd then insert the arrays into javascript with:

var usernames = <?php echo json_encode($username); ?>;
var user_ids = <?php echo json_encode($user_id); ?>;
Sign up to request clarification or add additional context in comments.

4 Comments

No, the loop isn't finished when they're outputting the values, so they should be both shown. Edit: on second thought, it's a javascript problemm as in they're overwriting the onDataRequest event every time.
This doesn't seem to work, or maybe I'm not able to integrate your answer with my existing code, I don't know...
well, since your onDataRequest is inside the loop, it'll be output on EVERY iteration. that means you end up with the equivalent of a many-element array all sharing the same key - last entry in the list "wins".
I thought this could be done with a simple php to js array conversion, but I guess I was wrong. Anyway thanks. I still can't get it working though.
2

Every time you loop, you are assigning new values for your onDataRequest javascript function, however, it's the same function every time. You should think about execution order - first, you do PHP&MySQL server-side and what is generated as HTML or Javascript code there gets rendered and/or executed client-side later.

Basically, you have a PHP loop that goes over a set of values and those values are put inside a Javascript code that is within a HTML code block. So whatever you fetch from your database server last, is what is actually executed client-side.

The main idea here is that you shouldn't mix your server-side PHP code with client-side HTML or Javascript code.

1 Comment

I kinda knew that, but this is only for testing, good advice tho.

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.