I uploaded some images to a MySQL database using a simple form and some php. The form has the following selection for catagory inputs.
<select name="image_ctgy">
<option value="animals">animals</option>
<option value="vegetables">vegetables</option>
<option value="minerals">minerals</option>
</select>
Each image has one of these categories in the MySQL table 'image_ctgy'. Now I want to echo the images in the category animals to my webpage.
I use the following code to get all the images on my webpage:
<?php
/*** Check the $_GET variable ***/
try {
/*** connect to the database ***/
$dbh = new PDO("mysql:host=;dbname=", '', '');
/*** set the PDO error mode to exception ***/
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** The sql statement ***/
$sql = "SELECT image_id, thumb_height, thumb_width, image_ctgy, image_type, image_name, title FROM testblob";
/*** prepare the sql ***/
$stmt = $dbh->prepare($sql);
/*** exceute the query ***/
$stmt->execute();
/*** set the fetch mode to associative array ***/
$stmt->setFetchMode(PDO::FETCH_ASSOC);
/*** set the header for the image ***/
foreach($stmt->fetchAll() as $array)
{
echo '<div class="thumb" style="width: '.$array['thumb_width'].'px; height: '.$array['thumb_height'].'px;">
<p class="title">' . $array["title"] . '</p>
<p><a href="showfile.php?image_id='.$array['image_id'].'">
<img src="showthumbs.php?image_id='.$array['image_id'].'" alt="'.$array['image_name'].' /">
</a></p>
</div>';
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
I think I need an if statement in the foreach loop to check if the image has to catagry Animals. Can anyone help me with this? Thanks