2017-03-06 30 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

对不起,你可以尝试它现在应该工作! –

相关问题