WordPress.org

Ready to get started?Download WordPress

Forums

From PHP Mysql query to javascrip (5 posts)

  1. chiquiro
    Member
    Posted 1 year ago #

    Hello,
    Im trying to make a search on my mysql database using php. That result I want to get pass it to javascript. The code I'm using now is in PHP:

    global $wpdb;
    	$table_name = $wpdb->prefix . "skapa_courses";
    	$current_user = wp_get_current_user();
    		$sql = "SELECT *
    			FROM <code>$table_name</code>
    			WHERE <code>owner</code> = '$current_user->ID'";
    	require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    	$results = $wpdb->get_results($sql);
    wp_localize_script( 'java_script_file', 'object_name', $results );

    then in my java_script_file I need something similar like I do in php. Something like:

    foreach ( $results as $result )
    {
    	echo $result->course_name;
    	echo $result->participants;
    	echo $result->date;
    }
  2. curlybracket
    Member
    Posted 1 year ago #

    Your javascript file could call an XML or JSON file which you generate on the fly via PHP.

  3. gabrielcastillo
    Member
    Posted 1 year ago #

    try this:

    //jQuery
    $(document).ready(function(){
     $.ajax({
        url: 'url to page with function',
        type: 'GET',
        dataType: 'JSON',
        cache: false,
        success: function(data){
            alert(data.course_name + data.participants + data.date);
        }
        return false;
     });
    });
    //PHP
    foreach($results as $result){
       $data['course_name'] = $result->course_name;
       $data['participants'] = $result->participants;
       $data['date'] = $result->date;
    }
    
    $data = array('course_name' => $data['course_name', 'participants'=> $data['participants'], 'date' => $data['date']);
    
    echo json_encode($data);

    This code will not work as is, you will need to make adjustments. But this will give you a idea of how to construct what you need.

  4. chiquiro
    Member
    Posted 1 year ago #

    I can already create the Json file from the PHP. Im using this:

    $jsonevents = array();
    foreach ( $results as $result )
    {
    	// - json items -
    	$jsonevents[]= array(
        title => $result->course_name,
        start => $result->date,
        );
    
    }
    echo json_encode($jsonevents);

    and I get the correct format! :) But still, dont know how to call the result from this json_encode($jsonevents) to use it in my javascript.
    What I'm trying to do is use this info and feed my FullCalendar events.

  5. gabrielcastillo
    Member
    Posted 1 year ago #

    if you look at the ajax call I made you will see I use success to get the data object.

    //jQuery
    $(document).ready(function(){
     $.ajax({
        url: 'url to page with function',
        type: 'GET',
        dataType: 'JSON',
        cache: false,
        success: function(data){
            var eventTitle = data.title;
            var start = data.start;
            alert('title'); // this will alert the return title.
        }
        return false;
     });
    });

Topic Closed

This topic has been closed to new replies.

About this Topic