Only display a specific category from a database (PHP/SQL)

unkn0wn

New Member
From a dropdown menu a user can choose: view all, athletic, dress, or sandals. I am creating a function that if the user chooses athletic--only the Product Type 'Athletic', only athletic items from the database will be shown.Right now, because how my code is written, if the user selects 'Athletic' they will see athletic items, but also all other products in the database because the function showAllProducts was called.I'm not sure how to write, that if a user selects a specific product type, only that product type will be shown.\[code\]if (isset($_SESSION['valid_user'])) { //echo "I am in the if statement of the session"; echo 'You are logged in as: '.$_SESSION['valid_user'].' <br />'; showAllProducts(); } else { echo "I am not setting the session variable"; //die; } $userCat = getUserCategory(); orderByCategory($userCat); //function athleticCategory --------------------------------------------- function athleticCategory() { echo "I am in the athletic function" . "<br/>"; $con = getConnection(); $sqlQuery = "SELECT * from Products WHERE ProductType='Athletic'"; // Execute Query ----------------------------- $result = mysqli_query($con, $sqlQuery); if(!$result) { echo "Cannot do query" . "<br/>"; exit; } $row = mysqli_fetch_row($result); $count = $row[0]; if ($count > 0) { echo "Query works" . "<br/>"; } else { echo "Query doesn't work" ."<br/>"; } // Display Results ----------------------------- $num_results = mysqli_num_rows($result); for ($i=0; $i<$num_results; $i++) { $row = mysqli_fetch_assoc ($result); // print_r($row); echo '<img src="http://stackoverflow.com/questions/3756712/data:image/jpeg;base64,'.base64_encode($row['Image']).'" />'; echo "Price: " . stripslashes($row['Price']); } }\[/code\]Dropdown Menu\[code\] <form action="register_script.php" name="frm" method="post"> <select name="category" id="category"> <option value="http://stackoverflow.com/questions/3756712/viewall">View All</option> <option value="http://stackoverflow.com/questions/3756712/dress">Dress</option> <option value="http://stackoverflow.com/questions/3756712/athletic">Athletic</option> <option value="http://stackoverflow.com/questions/3756712/sandals">Sandals</option> </select> <input type="submit" value="http://stackoverflow.com/questions/3756712/Go" /> </form>\[/code\]Edited Code:\[code\] $sqlQuery = "SELECT * from Products"; if($pUserCat == "athletic") { $sqlQuery = "SELECT * from Products WHERE ProductType='athletic'"; } elseif ($pUserCat == "dress") { $sqlQuery = "SELECT * from Products WHERE ProductType='dress'"; } elseif ($pUserCat == "sandals") { $sqlQuery = "SELECT * from Products WHERE ProductType='sandals'"; } elseif ($pUserCat == "viewall") { $sqlQuery = "SELECT * from Products"; }\[/code\]
 
Back
Top