2017-05-15 10 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([$ _GET ['brands'])を選んでいるのですか? データベースにbrandという名前の属性がないことがわかります。

あなたはどこが間違っているのか教えてください。

+0

[確定PHPのISSETへのガイド、空](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']))を使用して、そのセットをチェッククエリ文字列、です。 if is Set Then 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

    関連する問題