Note that there are some explanatory texts on larger screens.

plurals
  1. POOnly display a specific category from a database (PHP/SQL)
    primarykey
    data
    text
    <p>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.</p> <p><strong>Right now, because how my code is written, if the user selects 'Athletic' they will see athletic items, but also all other products</strong> in the database because the function showAllProducts was called.</p> <p>I'm not sure how to write, that if a user selects a specific product type, only that product type will be shown.</p> <pre><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'].' &lt;br /&gt;'; 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" . "&lt;br/&gt;"; $con = getConnection(); $sqlQuery = "SELECT * from Products WHERE ProductType='Athletic'"; // Execute Query ----------------------------- $result = mysqli_query($con, $sqlQuery); if(!$result) { echo "Cannot do query" . "&lt;br/&gt;"; exit; } $row = mysqli_fetch_row($result); $count = $row[0]; if ($count &gt; 0) { echo "Query works" . "&lt;br/&gt;"; } else { echo "Query doesn't work" ."&lt;br/&gt;"; } // Display Results ----------------------------- $num_results = mysqli_num_rows($result); for ($i=0; $i&lt;$num_results; $i++) { $row = mysqli_fetch_assoc ($result); // print_r($row); echo '&lt;img src="data:image/jpeg;base64,'.base64_encode($row['Image']).'" /&gt;'; echo "Price: " . stripslashes($row['Price']); } } </code></pre> <p><strong>Dropdown Menu</strong></p> <pre><code> &lt;form action="register_script.php" name="frm" method="post"&gt; &lt;select name="category" id="category"&gt; &lt;option value="viewall"&gt;View All&lt;/option&gt; &lt;option value="dress"&gt;Dress&lt;/option&gt; &lt;option value="athletic"&gt;Athletic&lt;/option&gt; &lt;option value="sandals"&gt;Sandals&lt;/option&gt; &lt;/select&gt; &lt;input type="submit" value="Go" /&gt; &lt;/form&gt; </code></pre> <p><strong>Edited Code:</strong></p> <pre><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></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload