Helping you Build Your Own Social Network!

Faster, better and easier!

AJAX n mySQL Please Help

(5 posts) (2 voices)
  • Started 2 years ago by Andrew Wayne
  • Latest reply from Andrew Wayne

  1. how do you process the data sent via AJAX to your server in PHP:
    for example i have the following code:
    <?
    $input = $_GET["q"];
    $data = array();
    // query your DataBase here looking for a match to $input
    $query = mysql_query("SELECT * FROM my_table WHERE my_field LIKE '%$input%'");
    while ($row = mysql_fetch_assoc($query)) {
    $json = array();
    $json['value'] = $row['id'];
    $json['name'] = $row['username'];
    $json['image'] = $row['user_photo'];
    $data[] = $json;
    }
    header("Content-type: application/json");
    echo json_encode($data);
    ?>

    1) how do i customize this so that i can fetch the usernames of friends of a user on bp1.2?
    2) how do i customize this so that i can fetch all the members on bp 1.2?

    please help

    Posted 2 years ago #
  2. hi Andrew
    here are the ways to find the all the usernames of friends of a user and all the members of a site

    1.Finding the username of all the friends of a user

    $user_id=1;//change it with the user id dynamically
    $friends_ids=friends_get_friend_user_ids($user_id);//we have ids of all friends of user
    $flist=join(",",$friends_ids);
    $flist="(".$flist.")";//prepare the set
    global $wpdb;
    $qfriends="SELECT user_login as username FROM {$wpdb->users} where ID in {$flist}";
    $r=$wpdb->get_results($wpdb->prepare($qfriends),ARRAY_A);
    echo json_encode($r); //echo as json, it will echo as [{"username":"xyz"},{"username":"abc"},....]

    2.Finding all the members of a site

    global $wpdb;
    $qmembers="SELECT user_login as username from {$wpdb->users}";
    $members=$wpdb->get_results($wpdb->prepare($qmembers),ARRAY_A);
    echo json_encode($members);

    For the ajax part, I will advise to take a look at doing it with wp-load.php(you can use ajaxurl variable in your javascript).

    Posted 2 years ago #
  3. For ajax part, take a look at bpdefult themes global.js and ajax.php

    In Php you should use something like this

    add_action( 'wp_ajax_Your_Action_Name', 'Your_Action_Handler_Function' );

    Please note you append your action name to "wp_ajax_" and when you send a request from javascript whose action is "Your_Action_Name", wordpress calls Your_Action_Handler_Function where you can handle the input/output.

    Something like this in your javascript

    jQuery.get( ajaxurl, { //this is the data which is sent to server
    			action: 'Your_Action_Name',
    			'cookie': encodeURIComponent(document.cookie),
    			'other_input_var':'value of the input var'
    	},
    function(response){
    //handle the response from server here		
    
    });

    Please note, you should mostly use jQuery.post() instead of get(I kept get as your example had get) and use nonce for security purposes.

    Posted 2 years ago #
  4. thanks Brajesh! I will give it a try thanks for all the help! :)

    one more question, i have the following code, using jquery,

    <script type="text/javascript">
    jQuery(document).ready(function($)
    {
    //hide the all of the element with class msg_body
    $(".msg_body").hide();
    //toggle the component with class msg_body
    $(".msg_head").click(function()
    {
    $(this).next(".msg_body").slideToggle(600);
    });
    });
    </script>

    and the following html:
    <div class="msg_list">
    <p class="msg_head">Header-1 </p>
    <div class="msg_body">
    Some text
    </div>
    <p class="msg_head">Header-2</p>
    <div class="msg_body">
    Some text
    </div>
    <p class="msg_head">Header-3</p>
    <div class="msg_body">
    Some text
    </div>
    </div>

    it works but,
    how do i get the .msg_body to hide when i have clicked on another .msg_head and when i click outside it..???

    Posted 2 years ago #
  5. getting the members of the site doesnt seem to work..

    im trying to implement an autosuggest function to a custom search bar..

    i get
    Fatal error: Call to a member function get_results() on a non-object in /,,,/suggest.php on line 4
    could you possibly turn this into a plugin or supply me with a code that will work?

    thanks for the help

    Posted 2 years ago #

Reply

You must log in to post.