Skip to content

Instantly share code, notes, and snippets.

@paulslugocki
Created August 23, 2011 08:50
Show Gist options
  • Save paulslugocki/1164686 to your computer and use it in GitHub Desktop.
Save paulslugocki/1164686 to your computer and use it in GitHub Desktop.
Modified buildSelect.php to better handle multiple comma separated values
<?php
/**
* Copyright (c) 2009 Travel UCD
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Parses a passage of text and returns a html image tag.
* Img src is either a blank.gif if the string is empty,
* the first src=""
*
* @author Paul Slugocki
* @param string $string
* @return string HTML image tag
*/
function processImage($string) {
$strReturn = '<img src=';
if($string=="") {
$strReturn .= '"blank.gif"';
} else {
$pattern = '/src="([:%\w\s\.\+\/-]+)/';
if(preg_match($pattern, $string, $matches)) {
$strReturn .= '"'.$matches[1].'"';
} else {
$strReturn .= '"'.$string.'"';
}
}
$strReturn .= ' class="thumb" />';
return $strReturn;
}
/**
* simplexml_load_file() wrapper
*
* @author Paul Slugocki
* @param string $hotelData File location
* @return simpleXMLElement
*/
function loadXML($hotelData) {
//if($_SERVER['HTTP_HOST']=="tourcmsdev.macbook") {
$fileLoc = $hotelData;
//} else {
// $fileLoc = '../../xml/'.$hotelData;
//}
$xml = simplexml_load_file($fileLoc);
return $xml;
}
/**
* Prints a HTML select box populated based on the
* contents of a node named with the value of $field
* checks the querystring to set the currently selected
* option
*
* @author Paul Slugocki
* @param simpleXMLElement $xml
* @param string $node the node value(s) to consider
* @param string $unfilteredName
* @return void
*/
function buildSelect($xml, $node, $unfilteredName) {
$options = array();
$xPath = '/tours/tour/'.$node;
$class = "";
$xml = $xml->xpath($xPath);
print('<select name="'.$node.'"><option value="">'.$unfilteredName.'</option>');
foreach($xml as $value) {
$stringValue = strval($value);
// Check for multiples by splitting by comma
$arValues = explode(",", $stringValue);
foreach ($arValues as $value) {
$value = trim($value);
if(!in_array($value, $options) && $value != "")
$options[] = $value;
}
}
sort($options);
foreach($options as $option) {
print("<option");
if(!empty($_GET[$node])) {
if($_GET[$node]==$option)
print(' selected="selected"');
}
print(">".$option."</option>");
}
print("</select>");
}
function getTourNode($xml) {
$xPath = '/tours/tour[sourceId="'.$_GET['tour'].'"]';
$xml = $xml->xpath($xPath);
return $xml;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment