2013-05-20 39 views
1

我需要幫助傳遞值到收藏夾,以顯示購物車信息。傳遞值到收藏夾

目前,我有一個簡單的設置只顯示一個靜態的句子。

JavaScript代碼:

function displayHideBox(boxNumber) 
{ 
    if(document.getElementById("LightBox"+boxNumber).style.display=="none") { 
     document.getElementById("LightBox"+boxNumber).style.display="block"; 
     document.getElementById("grayBG").style.display="block"; 
    } else { 
     document.getElementById("LightBox"+boxNumber).style.display="none"; 
     document.getElementById("grayBG").style.display="none"; 
    } 
} 

HTML代碼:

<a href="<?$_SERVER['PHP_SELF']?>?action=add&id=<?=$id;?>" onclick="displayHideBox('1'); return false;">Open Box</a> 

<div id="grayBG" class="grayBox" style="display:none;"></div> 
<div id="LightBox1" class="box_content" style="display:none;"> 
<table cellpadding="3" cellspacing="0" border="0"> 
    <tr align="left"> 
    <td colspan="2" bgcolor="#FFFFFF" style="padding:10px;"><div onclick="displayHideBox('1'); return false;" style="cursor:pointer;" align="right">X</div><p><!-- Box content -->Text of the box!!!</p></td> 
    </tr> 
</table> 
</div> 

我已經建立了一個簡單的購物車,目前當你添加一個產品它進入購物車頁面在那裏創建一個會話,並在購物車中顯示您的物品並提供刪除或繼續購物的選項。但是,當你將項目添加到您的購物車,我需要它來顯示燈箱展示的項目信息,並在購物車等物品。

這裏是代碼當前的「添加到購物車」功能:

<table border="1"> 

    <?php 
     //Select all of the relevant part details 
     $prod_query = 'SELECT * FROM *****.*****'; 
     $prod_details = db_query_into_array_enhanced($mysql_connection, $prod_query);      
     $count = count($prod_details); 
     for($i = 0; $i < $count; $i++) 
      {?>   
      <tr> 
       <? $id = $prod_details[$i]['catID'];?> 
       <td><?=$prod_details[$i]['catID'];?></td> 
       <td><?=$prod_details[$i]['shortDescription'];?></td> 
       <td><?=$prod_details[$i]['rrp'];?></td> 
       <td><a href="cart.php?action=add&id=<?=$id;?>">Add To Cart</a></td> 

      </tr> 
     <?}?> 
</table> 


<a href="cart.php">View Cart</a> 

所以我真的覺得我的問題是如何獲得這個動態數據到我的收藏夾?

我希望我已經解釋了這不夠好,並且有人能幫助我...在此先感謝

回答

0

一種可能的選擇是使用Javascript中一個全局對象來存儲您的購物車條目。你可以通過創建一個變量,如'var cartArr = [];'在你的javascript中,然後當一個項目被添加到購物車時,寫一個函數來更新cartArr變量。然後在lightbox中引用它。

**請注意,這是未經測試,並且僅是對一個可能的方法的例子從收藏夾訪問。另外請注意,您將需要添加的功能驗證,如果該項目已經在購物車,然後增加,以及從車中移除。

希望它有幫助。

<?php 
      //Select all of the relevant part details 
      $prod_query = 'SELECT * FROM *****.*****'; 
      $prod_details = db_query_into_array_enhanced($mysql_connection, $prod_query);      
      $count = count($prod_details); 
      for($i = 0; $i < $count; $i++) 
       {?>   
       <tr> 
        <? $id = $prod_details[$i]['catID'];?> 
        <td><?=$prod_details[$i]['catID'];?></td> 
        <td><?=$prod_details[$i]['shortDescription'];?></td> 
        <td><?=$prod_details[$i]['rrp'];?></td> 
        <td><a href="cart.php?action=add&id=<?=$id;?>">Add To Cart</a></td> 
       echo " 
       <script type=\"text/javascript\"> 
       var cartitem = {id:$prod_details[$i]['catID'], desc:$prod_details[$i]['shortDescription'], rrp:$prod_details[$i]['rrp']}; 
       addToCartJs(cartitem); 
       </script> 
      "; 
       </tr> 
      <?}?> 

==== JS

var cartArr = []; 

function addToCartJs(item){ 
    cartArr.push(item); 
} 

===附加

function setCartDisplay(boxNumber){ 
     var itemdisplay = ''; 
     for(var i=0; i<cartArr.length;i++){ 
      var itemdisplay = '<span class="cartdisplay">ItemNo:'+cartArr[i].id+'</span>'; 
      itemdisplay += '<span class="cartdisplay">Description:'+cartArr[i].desc+'</span>'; 
     } 
     document.getElementById("LightBox"+boxNumber).innerHTML = itemdisplay; 
    } 

===測試頁面=== 使用此測試和顯示信息以及它正在做...這是一個很好的學習機會。請記住,最好學習並理解代碼,以便將來添加和維護。 「授人以魚,你可以喂他一天。教人以漁,你喂他一輩子」

<div id="LightBox1" class="box_content" style="display:none;"> 
<script type="text/javascript"> 
var cartArr = []; 

function testCartItem(){ 
    var cartitem = {id:1001, desc:'description here', rrp:1002 }; 
    addToCartJs(cartitem); 
} 

function addToCartJs(item){ 
    cartArr.push(item); 
} 

function setCartDisplay(boxNumber){ 
    var itemdisplay = ''; 
    for(var i=0; i<cartArr.length;i++){ 
     var itemdisplay = '<span class="cartdisplay">ItemNo:'+cartArr[i].id+'</span><br>'; 
     itemdisplay += '<span class="cartdisplay">Description:'+cartArr[i].desc+'</span>'; 
    } 
    document.getElementById("LightBox"+boxNumber).innerHTML = itemdisplay; 
    document.getElementById("LightBox"+boxNumber).style.display="block"; 
} 

testCartItem(); 
console.log(cartArr); 
setCartDisplay(1); 
</script> 
+0

您好,感謝您的回答大多是有道理的我,但你的代碼有有點困惑我。我知道你從數據庫中獲取數據並將其推送到一個javascript數組中。那麼我該如何接收這個數組並將其顯示在我的lightbox中?如目前它只是顯示一個靜態句子 – Kevlar

+0

我已經所附的燈箱內訪問cartArr的possilbe方式的示例。由於對象是javascript,我們將需要使用javascript(或jQuery)進行顯示。爲燈箱/每個燈箱實例添加一個div或span,然後類似於上面的附加示例進行填充。希望有助於 – bphillips

+0

再次感謝。說實話,我不知道該怎麼處理你的代碼,因爲沒有足夠的經驗。我已經把所有的JavaScript放到了一個單獨的文件中,我希望這是好的。但我真的不知道如何使用你給我的代碼 – Kevlar