From cac203291eb9294a2be3f69ce0d5ae954e3a069d Mon Sep 17 00:00:00 2001 From: Kin Blas Date: Wed, 14 Sep 2011 18:19:46 -0700 Subject: [PATCH] Switched the sample-reuse-page-external.html sample over to calling a PHP script that supplies JSON data when called via XHR, or HTML when called directly. --- docs/pages/dynamic-samples/category.php | 150 +++++++++++++ .../sample-reuse-page-external.html | 210 ++++++------------ 2 files changed, 218 insertions(+), 142 deletions(-) create mode 100644 docs/pages/dynamic-samples/category.php diff --git a/docs/pages/dynamic-samples/category.php b/docs/pages/dynamic-samples/category.php new file mode 100644 index 00000000000..1d461b8d402 --- /dev/null +++ b/docs/pages/dynamic-samples/category.php @@ -0,0 +1,150 @@ + 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.

+ +

+
    +" . $arr[ $i ][ 'name' ] . "\n"; + } +?> +
+ +
+
+ + + -