I am trying to set up a database of images that can be arranged into categorys (Lining, Fencing & Decking, Brickwork) although I'm having some trouble and can't seem to get unstuck, if anyone can help that would be great, my table consists of 3 columns; id, Img, category
EDIT: Forgot to mention I am trying to achieve this using a drop-down menu, on page load it should show all results, then can be filtered into categorys.. Weirdly it shows all images if the 'category' is set to '1', but if u set them to 'lining' or 'fencing & decking' it doesnt work. Not sure what's going wrong?
<?php
define('DB_NAME', 'wlarter_portfolio');
define('DB_USER', 'wlarter_user');
define('DB_PASSWORD', 'pw');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$qry = "SELECT * FROM image ";
if(isset($_GET['category']) && is_numeric($_GET['category'])){
$qry .= "where category = ".$_GET['category'];
}
$results= mysql_query($qry) or die(mysql_error());
?>
<?php
while ($row = mysql_fetch_assoc($results)) { ?>
<div class="box-portfolio"> <?php echo $row['Img']; ?> </div>
<?php
};
?>
<?php
$category = isset($_GET['category']) && is_numeric($_GET['category']) ? $_GET['category'] : 1; // where 1 is a default category to show!
?>
<select onchange="if(this.value != '') document.location = '/portfolio.php?category=<?php echo $category; ?>&order_by=' + this.value">
<option value="">Choose an option</option>
<option value="Fencing & Decking"<?php if(isset($_GET['order_by']) && $_GET['order_by'] == 'Fencing & Decking') echo ' selected="selected"'; ?>>Fencing & Decking</option>
<option value="Lining"<?php if(isset($_GET['order_by']) && $_GET['order_by'] == 'Lining') echo ' selected="selected"'; ?>>Lining</option>
</select>