array( name => "Animals", description => "All your favorites from aardvarks to zebras.", items => array( array( name => "Pets", ), array( name => "Farm Animals", ), array( name => "Wild Animals", ) ) ), colors => array( name => "Colors", description => "Fresh colors from the magic rainbow.", items => array( array( name => "Blue", ), array( name => "Green", ), array( name => "Orange", ), array( name => "Purple", ), array( name => "Red", ), array( name => "Yellow", ), array( name => "Violet", ) ) ), vehicles => array( name => "Vehicles", description => "Everything from cars to planes.", items => array( array( name => "Cars", ), array( name => "Planes", ), array( name => "Construction", ) ) ) ); // Get the name of the category to display from // the query params for the script. $category_name = ''; if ( $_GET[ 'id' ] ) { $category_name = $_GET[ 'id' ]; } // Now get the category data, by name, from our in-memory // dictionary. This is the part where a script normally fetches // the data from a database. $category_obj = $category_data[ $category_name ]; // Now figure out how the script is being called. If it's being // called via XmlHttpRequest, then send the data back as JSON. // If not, then send it back as a list in an HTML document. if( $_SERVER[ "HTTP_X_REQUESTED_WITH" ] && $_SERVER[ "HTTP_X_REQUESTED_WITH" ] ==="XMLHttpRequest" ) { // Data should be written out as JSON. header("Content-type: application/json"); if ( !$category_obj ) { echo 'null'; } else { echo '{"name":"' . $category_obj[ 'name' ] . '","description":"' . $category_obj[ 'description' ] . '","items":['; $arr = $category_obj[ 'items' ]; $count = count($arr); for ( $i = 0; $i < $count; $i++ ) { if ( $i ) { echo ","; } echo '{"name":"' . $arr[ $i ][ 'name' ] . '"}'; } echo "]}"; } } else { // Data should be written out as HTML. header("Content-type: text/html"); ?> Vehicles

No matches found.