- preg_match: documentation ( source)
<html>
<body onload="showProduct()">
<div id="dropdown">
<?php
/**
* Download & parse Google spreadsheet content
*
* @param unknown_type $link
*/
function google_spreadsheet_to_array($key) {
// initialize URL
$url = 'http://spreadsheets.google.com/feeds/cells/' . $key . '/1/public/values';
// initialize curl
$curl = curl_init();
// set curl options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
// get the spreadsheet using curl
$google_sheet = curl_exec($curl);
// close the curl connection
curl_close($curl);
// import the xml file into a SimpleXML object
$feed = new SimpleXMLElement($google_sheet);
// get every entry (cell) from the xml object
// extract the column and row from the cell's title
// e.g. A1 becomes [1][A]
$array = array();
foreach ($feed->entry as $entry) {
$location = (string) $entry->title;
preg_match('/(?P<column>[A-Z]+)(?P<row>[0-9]+)/', $location, $matches);
$array[$matches['row']][$matches['column']] = (string) $entry->content;
}
// return the array
return $array;
}
$SP_KEY = "0AjzADOdg6QnadHZTVDI5MU83NVE4MFpxMmFHOXR2S1E";
$data = google_spreadsheet_to_array($SP_KEY);
echo' <select name="Product" id="Product" onchange="showProduct()">';
foreach($data[1] as $products){
echo'<option value="'.$products.'">'.$products.'</option>';
}
echo'</select>\n\n';
echo' <select name="Category" id="Category" onchange="showProduct()">';
foreach($data as $categories){
if($categories[A] != "Product" and $categories[A] != "")
echo'<option value="'.$categories[A].'">'.$categories[A].'</option>';
}
?>
<div id="demo"> </div>
<script>
function showProduct() {
var product = document.getElementById('Product').value;
document.getElementById("demo").innerHTML="bloop "+product+"!";
}
</script>
</div>
</body>
</html>