2016-10-09 5 views
0

사용자가 장바구니에 추가 한 무관심한 결과를 보여주고 싶습니다.¿ 관련 제품을 장바구니 제품에 표시하는 방법은 무엇입니까?

URL을 클릭하여 GET 방법으로 제품에서 ID을 얻습니다.

<a href=http:./cart/$id/$url> 

내 예를

products 테이블 만 6 기록이, 그 6 개 제품을 구입하는 말을하는 것입니다. 사용자가 지금 내 질문은 id5​id1id3

으로 구입하기 위해 3 개 제품을 추가 할 경우

이제, 내가 어떻게이 경우, 사용자가 구입하지 않은 테이블 products에서 다른 제품을 표시 할 수 있습니다 id2id4id6, 전에 언급 한대로 id5​id1id3을 구입했습니다.

내가 쇼핑 카트의 무관심 결과에 대해 생성됩니다 쿼리는 다음과 같다 :

$result = $c->prepare("SELECT id_tutorial,page,titulo,info,icon,bg,vdo,url,precio,duracion,status FROM tutoriales WHERE id_tutorial!=? and status=1 LIMIT 6"); 

미안 논리 != 또는 <>

하지만이 과정을 알고하지는 지출 이 쿼리 결과로 작업하기 전에 수행해야합니다.

<?php //Shopping Cart (./cart.php) 

//GET -> id of the product. 
if (isset($_GET['articulo'])) { 

    $id_tutorial = $_GET['articulo'] ?: ''; 

    //If the session is defined for Shop Cart. 
    if (isset($_SESSION['carrito'])) { 

     //Get data from session. 
     $arreglo = $_SESSION['carrito']; 
     $encontro = false;  

     for ($i=0; $i<count($arreglo); $i++) { 
      //checking if product has already been added to the cart. 
      if ($arreglo[$i]['Id'] == $_GET['articulo']) { 
       $encontro = true;    
      } 
     } 
     //If find product is false, update de array session (Shop cart). 
     if ($encontro == false) {   
      //Reset 
      $titulo = ""; 
      $precio = 0;    
      $icon = ""; 
      //Get data from DB. 
      $stmt = $c->prepare("SELECT titulo,precio,icon FROM tutoriales WHERE page=? and status=1"); 
      $stmt->bind_param("i",$_GET['articulo']); 
      $stmt->execute(); 
      $stmt->store_result(); 
      if ($stmt->num_rows > 0) { 
       $stmt->bind_result($titulo,$precio,$icon); 
       while ($stmt->fetch()) { 

        //New data for array.   
        $datosnuevos = ['Id' => $_GET['articulo'], 'Titulo' => $titulo, 'Precio' => $precio, 'Icon' => $icon, 'Cantidad' => 1 ]; 

        //array_push($arreglo, $datosnuevos); 
        $arreglo[] = $datosnuevos; 
        $_SESSION['carrito'] = $arreglo; 

        //Count total of products added to the cart. 
        $data = $_SESSION['carrito']; 
        $value_carrito = count($data); 
        $_SESSION['compras'] = $value_carrito; 

       } $stmt->close(); 


      } else { 
       $stmt->close(); 
      } 
     } 

    } else { //If session is not defined for Shop Cart, that is to say that the card is in 0 productos. 

     //Reset. 
     $titulo = ""; 
     $precio = 0; 
     $icon = ""; 
     //Get data from DB. 
     $stmt = $c->prepare("SELECT titulo,precio,icon FROM tutoriales WHERE page=? and status=1"); 
     $stmt->bind_param("i",$_GET['articulo']); 
     $stmt->execute(); 
     $stmt->store_result(); 
     if ($stmt->num_rows > 0) { 
      $stmt->bind_result($titulo,$precio,$icon); 
      while ($stmt->fetch()) { 

       //Started the array for Shop Cart. 
       $arreglo[] = ['Id' => $_GET['articulo'], 'Titulo' => $titulo, 'Precio' => $precio, 'Icon' => $icon, 'Cantidad' => 1 ]; 
       $_SESSION['carrito'] = $arreglo; 

       //Count total of products added to the cart. 
       $data = $_SESSION['carrito']; 
       $value_carrito = count($data); 
       $_SESSION['compras'] = $value_carrito; 

      } $stmt->close(); 

     } else { 
      $stmt->close(); 
     }   
    } 

} 
?> 
+0

'$ arreglo [] = [ 'Id'=> $ _GET [ 'articulo'], ... 'while()'루프의 각 반복에서 동일한 제품 ID를 얻고 있습니다. ? –

+0

예,이 행의 제품 ID는 [ 'Id'=> $ _GET [ 'articulo']입니다. –

+0

아래 답변을 주셨습니다. 바라기를 이것은 당신의 문제를 해결할 것입니다. –

답변

1

이 바이올린은 당신에게 머리를 당신의 SELECT 쿼리를 구성하는 방법에 대한 시작을 줄 것이다,

: 여기

card.php 내 쇼핑 카트 코드

$_SESSION['carrito']에는

SELECT id_tutorial,page,titulo,info,icon,bg,vdo,url,precio,duracion,status,id_nivel,id_estado 
FROM tutoriales 
WHERE status = 1 AND id_tutorial NOT IN (
    SELECT id_tutorial 
    FROM tutoriales 
    WHERE id_tutorial IN (" . rtrim(str_repeat("?,", $_SESSION['compras']), ",") . " 
    ) 
) 

을 그리고 당신의 코드는 다음과 같아야합니다 : 이미 고객이 구입 $_SESSION['compras']이 구매 한 제품의 총 수의 수를 유지하고, 나머지 제품을 가져올 수있는 SELECT 쿼리는 다음과 같이 될 것이다

// your code 

$stmt = $c->prepare("SELECT id_tutorial,page,titulo,info,icon,bg,vdo,url,precio,duracion,status,id_nivel,id_estado FROM tutoriales WHERE status = 1 AND id_tutorial NOT IN (SELECT id_tutorial FROM tutoriales WHERE id_tutorial IN (" . rtrim(str_repeat("?,", $_SESSION['compras']), ",") . "))"); 
$param = array(); 
$paramType = ''; 
for($i = 0; $i < $_SESSION['compras']; ++$i){ 
    switch(gettype($_SESSION['carrito'][$i]['Id'])){ 
     case 'boolean': 
     case 'NULL': 
     case 'integer': 
      $paramType .= 'i'; 
      break; 
     case 'double': 
      $paramType .= 'd'; 
      break; 
     case 'string': 
      $paramType .= 's'; 
      break; 
     default: 
      $paramType .= 'b'; 
    } 
    $param[] = &$_SESSION['carrito'][$i]['Id']; 
} 
array_unshift($param, $paramType); 
call_user_func_array(array($stmt, 'bind_param'), $param); 

$stmt->execute(); 
$stmt->store_result(); 
if ($stmt->num_rows > 0) { 
    $result->bind_result($id_tutorial,$page,$titulo,$info,$icon,$bg,$vdo,$url,$precio,$duracion,$status,$id_nivel,$id_estado); 
    while ($result->fetch()){ 

     // your code   

    } 
} else { 
    $stmt->close(); 
}  
+0

좋은 예 Rajdeep, 공유 주셔서 감사합니다, 그것은 올바르게 작동합니다. –

+0

@ Diablo13 기꺼이 도와 드리겠습니다. *건배! : -) * 문제가 해결되면 답변을 수락하십시오. [스택 오버플로에 대한 대답을 수락하는 방법?] (http://meta.stackexchange.com/a/5235) –

+0

나는 대답을 받아 들였습니다, 나는 버블이 stackoverflow에서 새로운 것입니다. 하지만 많은 것을 배울 수 있도록 코드를 조사하겠습니다. –

관련 문제