Sadece bir veritabanı (PHP / SQL) belirli bir kategori görüntülemek

0 Cevap php

Bir açılır menüden bir kullanıcı seçebilirsiniz: tüm, atletik, elbise, ya da sandalet görüntüleyin. Sadece Ürün Tipi 'Atletik', veritabanından sadece atletik ürün gösterilir - Ben kullanıcı atletik seçerse o bir işlevi oluşturma.

Right now, because how my code is written, if the user selects 'Athletic' they will see athletic items, but also all other products veritabanında işlev showAllProducts adlandırıldı çünkü.

Bir kullanıcı belirli bir ürün tipi seçerse, yalnızca o ürün tipi gösterilir olacağını, yazmak nasıl emin değilim.

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";
    }

0 Cevap