• Jim R

    (@jim-r)


    I’m using jQuery (not a plugin) with HighCharts to create a bar graph based on information coming from my own data table. The data table is in the same database as my WP install. The bar graph is displayed on a WP Archive Page via an Include.

    (I know close to zip about jQuery.)

    The jQuery is calling data from data.php, which isn’t hooked into my WP install. I need to get that data to match up to the following:

    $player = single_tag_title(“”, false);

    single_tag_title is basically a basketball player’s name. From there, I’d use the query:

    "SELECT * FROM a_playerRating
    		 WHERE CONCAT(playerFirst,' ', playerLast) = '".$player."'"

    So I can tell the jQuery to just get information relative to that player in my custom data table.

    How do I get the information in single_tag_title onto data.php when it isn’t part of any of my WP Pages?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    jQuery should send the player name data directly to data.php as an AJAX request. The data might look like this: { player_name: "Kobe Bryant"} data.php receives the data in $_POST['player_name']. The query is made, the results are sent back to jQuery in some mutually understood format. I suggest using JSON instead of the ‘X’ in AJAX, XML. jQuery then can place the data in the appropriate page elements.

    Thread Starter Jim R

    (@jim-r)

    I”m not having a problem on that end of it, and it uses JSON. The problem is it sends every row in the data table, and I need it to narrow in on the row dealing with the Slug of the WP Page, in this case, a player’s name.

    I have to be able to pass the Slug to data.php.

    Moderator bcworkz

    (@bcworkz)

    OK, the first three sentences of my previous post still applies. (up to $_POST['player_name']) The data is one of the parameters of the jQuery .ajax() method that you will use.

    Thread Starter Jim R

    (@jim-r)

    I understand what the JSON looks like.

    [{“name”:”Player”,”data”:[null,null,null]},{“name”:”Shooting On Catch”,”data”:[6,4,7]},{“name”:”Shooting off Dribble”,”data”:[7,4,8]}]

    The problem is getting it to just pull one of each. Right now, there are just three for testing, but there will be close to 800 when it’s all said and done.

    Moderator bcworkz

    (@bcworkz)

    My understanding now is you want to extract a player name from this data and send it to data.php, yes? Is your data a JSON object or is it still just a text string? If a string, it needs to be parsed into an object. Do something like var players = JSON.parse( text );

    The resulting object is handled like an array. players[0] is the first data row. The player name value is referenced with players[0].name. If you need to find a particular player statistic to determine the index, I believe you need to loop through each array item until the right value is found. Once the correct index is known, the related data, including player name, is easily referenced.

    That data can then be passed as part of the data array parameter used by .ajax() method, something like this:

    $.ajax({
    type: "POST",
    url: "https://example.com/data.php",
    data: { player_name: players[0].name }
    })
    .done(function( msg ) {
    alert( "Response: " + msg );
    });

    Then, as first mentioned, data.php receives the name value in $_POST['player_name']

    Thread Starter Jim R

    (@jim-r)

    What would that look like concatenated? nameFirst nameLast

    Yes, I’d like to send the players name nameFirst nameLast or id_player to data.php, so I can use it to just extra one row of data for index.php, which is where the jQuery is.

    OR…

    I put the data.php query on the WP Page. At one point I couldn’t get the query to work. Now, it does. It gives me my one row of data when it prints the JSON:

    [{"name":"Player","data":[31]},{"name":"Shooting On Catch","data":[6]},{"name":"Shooting off Dribble","data":[7]}]

    Now I can’t get the jQuery (index.php) to read that data even though I changed the file it’s calling information from.

    Thread Starter Jim R

    (@jim-r)

    At this point, just for testing, I have all the code on one page. This wouldn’t be a WP issue, but is there a way to call the JSON function from the same page? If so, I haven’t been able to figure out the syntax.

    Right now it looks like:

    $.getJSON("data.php", function(json) {
    				options.xAxis.categories = json[0]['data'];
    	        	options.series[0] = json[1];
    	        	options.series[1] = json[2];
    		        chart = new Highcharts.Chart(options);
    	        });
    Moderator bcworkz

    (@bcworkz)

    Concatenation into a data array in javascript and jQuery would look something like this:
    { player_name: players[0].nameFirst + " " + players[0].nameLast }

    Just FYI, the equivalent in PHP would look like this:
    array( 'player_name' => $players[0]['nameFirst'] . " " . $players[0]['nameLast'])

    You’ve taken a slightly different approach than I was suggesting. It’s fine, it’s really the same thing to some extent, but the details vary slightly. If you use .getJSON() you still need to send a data array like I suggested for the .ajax() method. Then on data.php you would get the sent data using $_GET instead of $_POST because of the different request methods employed by each approach.

    OTOH, to use my approach, where I just showed a simple alert response assuming a message (msg) was the response, it needs to be changed to handle the json response just like you did with .getJSON().

    I sense you’re not totally grasping what sort of data exchange needs to happen for all of this to work, or if you do, you are not grasping what parts of the code is doing each part of the data exchange. I think if you take some time to really understand these elements, instead of somewhat blindly sticking together various code snippets, you will be a lot better off.

    I realize this is a lot easier said than done. I’m not able to write a small dissertation to try to explain everything. The information is available though. If there is anything specific that you are struggling with (the overall concept, not the coding syntax), please ask and I will try to explain it as best I can.

    On the plus side, it appears you are very close to getting this figured out code wise even if you don’t fully grasp what’s going on ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Trying to pass a WP variable to a file outside of WP’ is closed to new replies.