tags:

views:

73

answers:

2

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.

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="data:image/jpeg;base64,'.base64_encode($row['Image']).'" />';
                  echo "Price: " . stripslashes($row['Price']);
                }

            }

Dropdown Menu

    <form action="register_script.php" name="frm" method="post">
        <select name="category" id="category">
            <option value="viewall">View All</option>
            <option value="dress">Dress</option>
            <option value="athletic">Athletic</option>
            <option value="sandals">Sandals</option>
       </select>

      <input type="submit" value="Go" />

  </form>

Edited 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";
    }
+1  A: 

make a function , that accept one parameter ie category name and use default hint as 0

function categoryList($cat=false){
if($cat)
   $sqlQuery = "SELECT * from Products
                        WHERE ProductType={$cat}";
else
   $sqlQuery = "SELECT * from Products";
//do other stuff of Reading option 
} 
JapanPro
@JapanPro thanks, i'll try it out and let u know if it works for me
jc70
@JapanPro it might just be how i have my form setup, but i'm not really sure. in the dropdown menu, for the items dress, atheletic, and sandals--i was able to display those results. but when i chose "view all", the query did not work.
jc70
@JapanPro i edited my code (it's posted at the bottom of my original code) it works, but it's more code, and looks less efficient than what you wrote.
jc70
@crewof look at my updated answer. It is ready to use. And probably the least code you can write this in.
tilman
+1  A: 

Set your 'View All' form option like this:

<option value="">View All</option>

You can use it as it is.

  if (isset($_POST['category']))
    $category = $_POST['category'];

  $sqlQuery = "SELECT * from Products";
  if ( ! empty($category)) {
    if (get_magic_quotes_gpc()) {
      $category = stripslashes($category);
    }
    if ( ! is_numeric($category)) {
      $category = "'" . mysql_real_escape_string($category) . "'";
    }
    $sqlQuery .= " WHERE ProductType='{$category}'";
  }

It has basic security features so people can't inject malicious SQL into your script.

If you call that function without any category, it will be assumed you want to show all values.

You dont need to check if for each and every single case and then write the sqlQuery according to that, as long as you use the same <option value="xxx"> as the categories are called in your db.

tilman