diff --git a/chapter11/project2/browse-images.php b/chapter11/project2/browse-images.php index d64888afff2f7de94dbf13294ad139865e38001e..293a046c4dd9442da65c93f86c381dc412e6db84 100644 --- a/chapter11/project2/browse-images.php +++ b/chapter11/project2/browse-images.php @@ -10,6 +10,7 @@ # Connect to DB. #ConnectViaMySQLi($host, $DBuser, $DBpassword, $database); + #global $pdo; $pdo = ConnectViaPDO($host, $DBuser, $DBpassword, $database, $DBport); @@ -49,11 +50,86 @@ # Merge Cities and Image Details. # Only returns cities with valid image data. - $sql = "SELECT DISTINCT GeoCities.AsciiName + $sql = "SELECT DISTINCT GeoCities.AsciiName, GeoCities.GeoNameID FROM GeoCities, TravelImageDetails WHERE GeoCities.GeoNameID = TravelImageDetails.CityCode;"; $sqlCitiesMatch = $pdo->query($sql); + + if ($_SERVER['REQUEST_METHOD'] == 'GET') { + echo "<br>"; + if ( isset($_GET['city']) ) { + echo $_GET['city']; + } + echo "<br>"; + if ( isset($_GET['country']) ) { + echo $_GET['country']; + } + echo "<br>"; + } + + + # Returns filtered City/Country. + #global $sqlFilterMatch; + /*function GetDBFilterMatch() { + if ($_SERVER['REQUEST_METHOD'] == 'GET') { + if ( isset($_GET['city']) and $_GET['city'] > 0) { + if ( isset($_GET['country']) and $_GET['country'] > 0) { + # If Both City and Country filters are set. + $sql = "SELECT DISTINCT * + FROM GeoCountries, GeoCities, TravelImage, TravelImageDetails + WHERE GeoCountries.ISO = TravelImageDetails.CountryCodeISO AND + GeoCities.GeoNameID = TravelImageDetails.CityCode AND + TravelImage.ImageID = TravelImageDetails.ImageIDID AND + GeoCities.GeoNameID = " . $_GET['city'] . " AND + GeoCountries.ISONumeric = " . $_GET['country'] . ";"; + global $sqlFilterMatch, $pdo; + $sqlFilterMatch = $pdo->query($sql); + } else { + # If only City filter is set. + $sql = "SELECT DISTINCT * + FROM GeoCountries, GeoCities, TravelImage, TravelImageDetails + WHERE GeoCountries.ISO = TravelImageDetails.CountryCodeISO AND + GeoCities.GeoNameID = TravelImageDetails.CityCode AND + TravelImage.ImageID = TravelImageDetails.ImageID AND + GeoCities.GeoNameID = " . $_GET['city'] . ";"; + echo $sql; + global $sqlFilterMatch, $pdo; + $sqlFilterMatch = $pdo->query($sql); + } + } else { + if ( isset($_GET['country']) and $_GET['country'] > 0) { + # If only Country filter is set. + $sql = "SELECT DISTINCT * + FROM GeoCountries, GeoCities, TravelImage, TravelImageDetails + WHERE GeoCountries.ISO = TravelImageDetails.CountryCodeISO AND + GeoCities.GeoNameID = TravelImageDetails.CityCode AND + TravelImage.ImageID = TravelImageDetails.ImageID AND + GeoCountries.ISONumeric = " . $_GET['country'] . ";"; + global $sqlFilterMatch, $pdo; + $sqlFilterMatch = $pdo->query($sql); + } + } + } else { + # If neither City or Country filters are set. + $sql = "SELECT DISTINCT * + FROM GeoCountries, GeoCities, TravelImage, TravelImageDetails + WHERE GeoCountries.ISO = TravelImageDetails.CountryCodeISO AND + GeoCities.GeoNameID = TravelImageDetails.CityCode AND + TravelImage.ImageID = TravelImageDetails.ImageID"; + global $sqlFilterMatch, $pdo; + $sqlFilterMatch = $pdo->query($sql); + } + }*/ + + $sql = "SELECT DISTINCT * + FROM GeoCountries, GeoCities, TravelImage, TravelImageDetails + WHERE GeoCountries.ISO = TravelImageDetails.CountryCodeISO AND + GeoCities.GeoNameID = TravelImageDetails.CityCode AND + TravelImage.ImageID = TravelImageDetails.ImageID;"; + echo $sql; + $sqlFilterMatch = $pdo->query($sql); + ?> <!DOCTYPE html> @@ -101,7 +177,7 @@ <?php if ($sqlCountriesMatch2) { while ($row = $sqlCountriesMatch2->fetch()) { - echo "<option value=" . $row['ISONumeric'] . ">" . $row['CountryName'] . "</option>"; + echo "<option value=" . $row['ISO'] . ">" . $row['CountryName'] . "</option>"; } } else { echo "<option value='-1'>No Countries to display.</option>"; @@ -116,6 +192,19 @@ <div class="well"> <div class="row"> <!-- display image thumbnails code here --> + <?php + #GetDBFilterMatch(); + if ($sqlFilterMatch) { + while ($row = $sqlFilterMatch->fetch()) { + echo "<figure style='display: inline-block; padding-left: 1%; padding-right: 1%; width=150px;'>"; + echo "<a href='" . $row['ImageID'] . "'><img src='images/travel/" . $row['Path'] . "'></a>"; + echo "<figcaption style='max-width: 150px;'>" . $row['Title'] . "</figcaption>"; + echo "</figure>"; + } + } else { + echo "<p>No Images to display. Perhaps try different filters?</p>"; + } + ?> </div> </div> <!-- end images well -->