2017-05-15 32 views
-3

我正在學習關於如何在php中創建電子商務網站的教程,以及現在我主要懷疑的是什麼時候使用isset函數。什麼時候可以使用isset和GET函數

所以首先我顯示使用此代碼在側欄中的所有產品品牌:

function getBrands(){ 
    global $con; 

    $get_brands = "select * from brands"; 
    $run_brands = mysqli_query($con, $get_brands); 

    while ($row_brands=mysqli_fetch_array($run_brands)){ 
     $brand_id = $row_brands['brand_id']; 
     $brand_name = $row_brands['brand_name']; 
     echo "<li><a href='index.php?brand=$brand_id'>$brand_name</a></li>"; 
    } 
} 

再有就是這個代碼是顯示所有基於該品牌的產品:

function get_brand_pro(){ 
    if(isset($_GET['brand'])){ 

     $brand_id = $_GET['brand']; 

     global $con; 

     $get_brand_pro = "select * from products where product_brand='$brand_id'"; 

     $run_brand_pro = mysqli_query($con, $get_brand_pro); 

     $count_brands = mysqli_num_rows($run_brand_pro); 

     if($count_brands==0){ 
      echo "<h3>No products associated with this brand where foundd</h3>"; 
     } 

     while($row_brand_pro = mysqli_fetch_array($run_brand_pro)){ 

      $product_id = $row_brand_pro['product_id']; 
      $product_cat = $row_brand_pro['product_cat']; 
      $product_brand = $row_brand_pro['product_brand']; 
      $product_title = $row_brand_pro['product_title']; 
      $product_price = $row_brand_pro['product_price']; 
      $product_image = $row_brand_pro['product_image']; 

      echo " 
       <div class='single_product'>  
        <h3 class='product_title'>$product_title</h3> 
        <img src='admin-area/product_images/$product_image' alt='product image' width='250' height='250' /> 
        <p class='product_price'>$ $product_price</p> 
        <div class='single_links'> 
         <a href='details.php?product_id=$product_id' class='details'>Details</a> 
         <a href='index.php?product_id=$product_id' class='add_cart'><button>Add to Cart</button></a> 
        </div> 
       </div> 

      "; 
     } 
    } 
} 

從我的理解是,isset函數的工作原理與單擊按鈕相同。

爲什麼選擇isset([$ _ GET ['brands']])? 因爲我看到數據庫中沒有名爲品牌的屬性。

希望你能糾正我錯在哪裏。

+0

[權威指南PHP的isset和EMPTY(http://kunststube.net/isset/) – deceze

+0

'brands'是在這裏'回聲 「

  • $brand_name
  • 」 創建的查詢串; '所以在另一個頁面上,您首先需要檢查品牌是否在嘗試使用之前設置 –

    +0

    brand_id ...其 –

    回答

    0

    那麼isset確定該變量是否設置或NULL。在您的代碼和數據庫中,您確實有一個名爲product_brand的屬性,它使用該值。

    您使用此行獲得值:$brand_id = $_GET['brand']; 然後在您的查詢中,您正在使用它。

    0

    isset()函數函數

    的isset()函數功能用於檢查變量是否被設置或不 。如果一個變量已經被unset()函數取消設置,那麼將不會再設置 。如果測試變量 包含NULL值,則isset()函數返回false。

    在你的代碼說明:

    echo "<li><a href='index.php?brand=$brand_id'>$brand_name</a></li>"; 
    

    這裏,brands=$brand_id查詢字符串,其中分配ID,以 '品牌' 然後我們檢查使用if(isset($_GET['brand']))其設定。如果ise Set Then, 我們獲得品牌在另一個變量中的價值($brand_id),然後您獲取select ...where product_brand = '$brand_id'中的數據。

    if(isset($_GET['brand'])){ //Check brand Is Set Or Not in Query String 
    
         $brand_id = $_GET['brand']; // If is set then assign to $brand_id variable 
    
         global $con; 
    
         $get_brand_pro = "select * from products where product_brand='$brand_id'"; //Here fetch the data of product table based on $brand_id which is equal to product_brand in your product table. 
    
    +0

    https://eval.in/795211 ...檢查這個... –

    -1

    isset()函數實際檢查如果一個變量或一個陣列被定義或不是, 示例:

    $name = $_GET["name"]; 
    

    最好最佳方式,以檢查是否HTTP _GET的值已被設置爲可變$ name是

    if(empty($name)){ 
    echo 'variable $name is empty'; 
    } 
    

    但是,如果你想直接檢查全局變量/數組$ _GET沒有它賦值給一個變量,那就是當isset()函數;功能是需要的。 實施例

    if(!isset($_GET["name"])){ 
    echo '$_GET["name"] is not set'; 
    } 
    
    +0

    這是'空'的可怕和荒謬的使用。 – deceze

    相關問題