0

I want to insert while loop user data in jQuery array.

<?php 

    while($get_users_table_details = mysql_fetch_array($deatails)) {


        if(empty($get_users_table_details["photo"])) { $pics = "photos/avatar.gif"; } else { $pics = "photos/".strip_tags($get_users_table_details["photo"]).""; }

        ?>

Script code between while loop

$(document).ready(function(){

        $("#full").mention({
            users: [{
                name: '<?php echo $get_users_table_details['fullname'];?>',
                username: '<?php echo $get_users_table_details['username'];?>',
                image: '<?php echo $pics;?>'
                   }]     });
});
    </script>

end of script

}
end of while loop

The problem is that this shows the user details of only one user. However, I want all user details to be shown, as in this picture:

enter image description here

scopchanov
  • 7,966
  • 10
  • 40
  • 68
Pk Prajapati
  • 11
  • 1
  • 6
  • You should iterate loop into javascript code instead of PHP code. And need to push every element into users javascript array. – Ashok Gujjar Sep 13 '18 at 12:54
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Zain Farooq Sep 13 '18 at 12:54
  • Please provide your PHP array data structure. – Ashok Gujjar Sep 13 '18 at 12:57
  • id, username, pic, full name, – Pk Prajapati Sep 13 '18 at 12:58
  • build the users array completely in php, then json_encode the whole thing and 'pass' it to js. Get the javascript out of the while loop. – Jeff Sep 13 '18 at 13:03
  • _and_: do not use `mysql_*` functions any more. They are old, deprecated, unsecure and removed in the latest php version. Use mysqli or PDO – Jeff Sep 13 '18 at 13:05

2 Answers2

0

Please try this sample javascript code.

    var users = <?php echo json_encode($get_users_table_details ) ?>;
    $.each(users, function(key, value) {
        console.log('stuff : ' + key + ", " + value);
        // Do everything you want
    });
Ashok Gujjar
  • 441
  • 5
  • 21
0

You should first build the array of users in php:

<?php

$users = []; // instatiate an empty array

while($get_users_table_details = mysql_fetch_array($deatails)) {
    // build a user-array as needed later
    $user = [];
    $user['name'] = $get_users_table_details['fullname'];
    $user['username'] = $get_users_table_details['username'];
    if(empty($get_users_table_details["photo"])) { 
         $user['image'] = "photos/avatar.gif"; 
    } else { 
         $user['image'] = "photos/".strip_tags($get_users_table_details["photo"]); 
    }
    $users[] = $user;  // add this user to the array of users
}
?>

Then pass (=echo) that to javascript via json_encode:

<script>
$(document).ready(function(){

    $("#full").mention({
        users: <?php echo json_encode($users); ?>
    });
});
</script>
Jeff
  • 6,895
  • 1
  • 15
  • 33