2017-03-06 24 views
1

我想做一個會話存儲,然後顯示用戶看到的最後5個產品...但不適合工作php session只能存儲和顯示最近的5個視圖產品

首先存儲相同ID產品,如果我referesh,我想只存儲不同的ID(產品) 2.其次是不顯示我超過1行(1查詢),即使我將有更多的ID商店(不同的產品看到像用戶ID: 3,5,5,6,2)只顯示我在這種情況下的最後一個ID商店 2 ....我想顯示所有的查詢..

if (isset($_GET['id'])) { 
    // Connect to the MySQL database 
    include "storescripts/connect_to_mysql.php"; 
    $id = preg_replace('#[^0-9]#i', '', $_GET['id']); 
    $_SESSION['lastViewProducts'][] = $id; 
    if(count ($_SESSION['lastViewProducts']) > 5){ 
     array_shift($_SESSION['lastViewProducts']); 
     } 
    foreach($_SESSION['lastViewProducts'] as $keyLASTview=>$valueLASTview) { 
     $stmtLastVIEW = $con->prepare('SELECT id, product_name, price, details, category, subcategory, size, date_added, image, brand_name, product_color, vizualizari FROM products WHERE id=?'); 
     $stmtLastVIEW->bind_param('i', $valueLASTview); 
     $stmtLastVIEW->execute(); 
     $stmtLastVIEW->bind_result($idSelectDetalii, $produsNumeDetalii, $priceDetalii, $produsDetalii, $produsCategory, $produsSubcategory, $produsSize, $produsDate_added, $imageLocationDetalii, $brandProdusSelectat, $produsColor, $produsVizualizari); 
     $lastVIEWproduct = ''; 
     while ($stmtLastVIEW->fetch()) { 
      $lastVIEWproduct .='titlu: '.$idSelectDetalii.' <img src="'.$imageLocationDetalii.'" class="img-responsive">'; 
      } 
     $stmtLastVIEW->free_result(); 
    } 
} 

我真的卡住了...我無法找出解決這個問題,也許其他變種? 另外3.第三如果用戶在同一頁上的products.php?id = 3和id = 3它存儲在會話中我不想顯示這個... WHERE id=?(for the showing last 5 products views) and id is not (the $id)?如何編寫mysql select語句和where條款?如果我想也否定

+0

你需要刪除此行'$ lastVIEWproduct = '';' – cmorrissey

+0

ü需要'$ stmtLastVIEW->使用fetchall()',而不是'$ stmtLastVIEW->取()因爲'fetch()'只給出1個結果,'fetchAll()'給出一個包含所有結果的數組。 – Mario

+0

現在給我'致命錯誤:調用未定義的方法mysqli_stmt :: fetchAll()' – Alcatraz007

回答

2

你的邏輯有一些錯誤。您可以使用此代碼嘗試:

if (isset($_GET['id'])) { 

    // Connect to the MySQL database 
    include "storescripts/connect_to_mysql.php"; 
    $id = preg_replace('#[^0-9]#i', '', $_GET['id']); 

    //1. Store only differents ids in the session by creating an associative array 
    $_SESSION['lastViewProducts'][$id] = $id; 

    //3. Don't show the $_GET['id'] in the list 
    $ids = array_filter($_SESSION['lastViewProducts'], function($currentID) use($id) { 
     return $id != $currentId; 
    }); 


    //Keep only 5 distincts articles 
    $ids = array_slice(array_unique($ids), 0, 5); 

    //2. declare your string OUTSIDE the foreach 
    $lastVIEWproduct = ''; 
    foreach($ids as $valueLASTview) { 
     $stmtLastVIEW = $con->prepare('SELECT id, product_name, price, details, category, subcategory, size, date_added, image, brand_name, product_color, vizualizari FROM products WHERE id=?'); 
     $stmtLastVIEW->bind_param('i', $valueLASTview); 
     $stmtLastVIEW->execute(); 
     $stmtLastVIEW->bind_result($idSelectDetalii, $produsNumeDetalii, $priceDetalii, $produsDetalii, $produsCategory, $produsSubcategory, $produsSize, $produsDate_added, $imageLocationDetalii, $brandProdusSelectat, $produsColor, $produsVizualizari); 
     while ($stmtLastVIEW->fetch()) { 
      $lastVIEWproduct .='titlu: '.$idSelectDetalii.' <img src="'.$imageLocationDetalii.'" class="img-responsive">'; 
     } 

     $stmtLastVIEW->free_result(); 
    } 
} 
+0

給我2錯誤a。 '警告:array_filter()期望參數1是數組,第20行給出的字符串是this(}); )'b。 'arning:爲第28行的foreach()提供的無效參數this(foreach($ ids as $ valueLASTview){)' – Alcatraz007

+0

同樣的錯誤...它也給出了一個新的錯誤'注意:Undefined variable:ids'line this $ ids = array_filter($ ids,function($ currentID)use($ id){)' – Alcatraz007

+0

對不起,你可以嘗試它現在應該工作! –

相關問題