Database

Can anyone explain how to use Allen’s database in PHP cuz I cannot seem to understand how to use it in php

Here are some helpful hints. You want to get the values from the API. Adjust the request string for whatever attributes you are looking for to limit it down to a smaller set. Use drop downs or other things to help with the API call limiting it or just hard code it.

Reference of Allen’s API:
http://vex.us.nallen.me/the_data

Let’s start with this small set of 75
http://api.vex.us.nallen.me/get_teams?country=United%20States&grade=High%20School&is_registered=1&region=Pennsylvania

You will want to json_decode( ) on the results to put them into objects or a multi-dimensional array. There’s a top level and then you get to the good stuff underneath.

Here is some reference material for you:

http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP

http://www.developphp.com/view_lesson.php?v=860

Do you want to work with the results as objects or a multi-dimensional array? Referencing the results becomes different for each method.

http://stackoverflow.com/questions/9471592/processing-multidimensional-json-array-with-php

you are my savior :')

Decode it using json_decode( … ) and put that into a variable

Ex:


// Gets info for a team with number 123
$json = file_get_contents("http://api.vex.us.nallen.me/get_teams?team=123&program=VRC");
$decoded = json_decode($json);

Then you can get data from it like this:


$team_name = $decoded->result[0]->team_name;

Which gets the team name out of the first result (hence the 0 in result[0])of the JSON query.

To loop through all the results (for example if you do a query to get all the VRC teams and want to list all the team numbers), you can do this:


// Gets all VRC (VEX) teams
$json = file_get_contents("http://api.vex.us.nallen.me/get_teams?program=VRC");
$decoded = json_decode($json);

// Each team is stored in a JSON array called "result"
// Store that "result" array into $resultentries
$resultentries = $decoded->result;
foreach($resultentries as $resultentry){ // Loops through each entry in the array stored in $resultentries
	$number = $resultentry->number; // In each entry, get the team number and store it into $number
	echo $number . "<br/>"; // List the team numberse
}

EDIT: fixed an error
EDIT2: added comments

Thank you!!

So i tried it and all I am getting is some of the code I wrote printed out and it doesn’t go away no matter what i do
is it because of the fact that I am not trying to launch it from a server?

What is your PHP run time environment?

PHP on a local version of apache using WAMP/MAMP would work too.

If you are behind a proxy getting the API data directly may be tougher. Then use the curl method where you can set a bunch of stuff to poke through.

I personally use XAMPP (AMPP stands for Apache+mysql+php+perl) for running code locally.
However you can also get a website with a host and they’ll usually have it already. A free webhost is a good option if you want a few other people to be able to test your app.

I installed XAMPP but the same thing happened
EDIT: so I tested a couple things out and it seems to be happening whenever I try to assign the result array and when I try to assign the number
EDIT2: So now it stopped messing up at the results part but the number part still doesn’t work

Are you using an integer for the team number? It should be string. I’m by no means proficient in PHP, so I’m not sure if that’s even relevant. At least in C#, JSON is always deserialized into strings, so you’ll need to do implicit conversions if that’s what you want (you can’t convert team number to an integer though - see 0000A)

Robotics Scouting
 <?php

//Gets info for a team with number 123
// Gets all VRC (VEX) teams
// Gets all VRC (VEX) teams
$url = ‘http://api.vex.us.nallen.me/get_teams?program=VRC’;

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);     //We want the result to be saved into variable, not printed out
$response = curl_exec($handle);                         
curl_close($handle);

$decoded = json_decode($response, true);

// Each team is stored in a JSON array called “result”
// Store that “result” array into $resultentries

foreach($decoded’result’] as $resultentry){

echo $resultentry'number']; 

}

?>

that is my code and now it just show’s me a blank screen

I tested it and it works fine for me, it just might take a while because it’s getting 5000 teams.

This is what shows up for me

It’s all the teams but with no line breaks (so they’re all on one line)

If you want them on separate lines change


echo $resultentry'number']; 

to


echo $resultentry'number'] . "<br>";

Which adds a
(linebreak) after each number.

EDIT: I’ll test it out locally with XAMPP
EDIT2: Works fine with XAMPP too for me. Is your code saved as a .php file? Is it in the XAMPP folder (mine is in C:/xampp/htdocs)? If it’s in the right place you should be typing in “localhost” or “127.0.0.1” in your address bar in your browser to get to your code.

For example, if your file is called “getteams.php” and you put it in the right xampp folder, to get to it you go to “localhost/getteams.php”

lol oops sorry I wasn’t using it properly with XAMPP so it was not displaying but it works now, any suggestions on a css or a method to keep all of this in a table?

Here’s an sample to get you started, this makes a table with the first column having the team name and the second column having the team number. :smiley:

It’s basically your code (I removed a few comments), but I added code that echo’s a table, table rows, and table cells around the data.
[PHP]

<?php $url = 'http://api.vex.us.nallen.me/get_teams?program=VRC'; $handle = curl_init($url); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); //We want the result to be saved into variable, not printed out $response = curl_exec($handle); curl_close($handle); $decoded = json_decode($response, true); echo ""; echo ""; // Table headers foreach($decoded'result'] as $resultentry){ echo ""; echo ""; echo ""; echo ""; } echo "
NameNumber
" . $resultentry'number'] . "" . $resultentry'team_name'] . "
"; ?>

[/PHP]

(By default only 5000 teams are returned, i haven’t messed with getting more than that but on the VexDB site there are some instructions on how to get more data if you need)

omg I <4 u man this is so awesome rite now thanks so much guys!