Avatar billede snigeren Nybegynder
02. januar 2006 - 21:01 Der er 1 kommentar og
1 løsning

id i indkøbskurv

Den indkøbskurv jeg har tilpasset mit site har et (tror jeg nok) Hver gang man tilføjer et nyt produkt laver den et nyt id i "cart" databasen.

Det er vel ikke meningen at hver gang en kunde tilføjer et nyt produkt til indkøbskurven så laver den nyt id. dvs. at den samme kunne i "cart" tabellen i DB´en får flere id?

Hold på hat og briller her kommer scriptet:

DELETE FROM `cart` WHERE `cartId` = '11' LIMIT 1;

<?php

    include("db.php");
       
    switch($_GET["action"])
    {
        case "add_item":
        {
            AddItem($_GET["id"], $_GET["qty"]);
            ShowCart();
            break;
        }
        case "update_item":
        {
            UpdateItem($_GET["id"], $_GET["qty"]);
            ShowCart();
            break;
        }
        case "remove_item":
        {
            RemoveItem($_GET["id"]);
            ShowCart();
            break;
        }
        default:
        {
            ShowCart();
        }
    }

    function AddItem($itemId, $qty)
    {
        // Will check whether or not this item
        // already exists in the cart table.
        // If it does, the UpdateItem function
        // will be called instead
       
        global $dbServer, $dbUser, $dbPass, $dbName;

        // Get a connection to the database
        $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
       
        // Check if this item already exists in the users cart table
        $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
        $row = mysql_fetch_row($result);
        $numRows = $row[0];
       
        if($numRows == 0)
        {
            // This item doesn't exist in the users cart,
            // we will add it with an insert query

            @mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");
        }
        else
        {
            // This item already exists in the users cart,
            // we will update it instead
           
            UpdateItem($itemId, $qty);
        }
    }
   
    function UpdateItem($itemId, $qty)
    {
        // Updates the quantity of an item in the users cart.
        // If the qutnaity is zero, then RemoveItem will be
        // called instead

        global $dbServer, $dbUser, $dbPass, $dbName;

        // Get a connection to the database
        $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
       
        if($qty == 0)
        {
            // Remove the item from the users cart
            RemoveItem($itemId);
        }
        else
        {
            mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
        }
    }
   
    function RemoveItem($itemId)
    {
        // Uses an SQL delete statement to remove an item from
        // the users cart

        global $dbServer, $dbUser, $dbPass, $dbName;

        // Get a connection to the database
        $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
       
        mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
    }
   
    function ShowCart()
    {
        // Gets each item from the cart table and display them in
        // a tabulated format, as well as a final total for the cart
       
        global $dbServer, $dbUser, $dbPass, $dbName;

        // Get a connection to the database
        $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
       
        $totalCost = 0;
        $result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");
        ?>
<html>
<head>
<title>Jernbanemaerker.dk - Indk&oslash;bskurv</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/styles.css" rel="stylesheet" type="text/css">
<script language="JavaScript">

    function UpdateQty(item)
    {
    itemId = item.name;
    newQty = item.options[item.selectedIndex].text;
       
    document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
    }
       
</script>
<style type="text/css">
<!--
.style2 {color: #89744B}
.style5 {font-size: 11px}
-->
</style>
<script language="javascript" type="text/javascript">
    window.onload = function(){update();setInterval('update()',60000)}
    function update(){
      var d = new Date();
      document.getElementById('minSpan').innerHTML = d.format("hh:uu | \\d. d. mmmm");
    }
  </script>
  <script language="javascript" type="text/javascript" src="roenvingDate.js"></script>
</head>

<body bgcolor="#D8C5A3">
<table width="900" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr align="right" valign="top">
    <td width="900" height="143" class="top"><table width="555" height="138" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td align="left" valign="bottom" class="style2"><p class="style2 style5"><?php include("menu.php"); ?></td>
  </tr>
</table></td>
  </tr>
  <tr>
    <td height="251" valign="top" class="body" bgcolor="#F2E3C8"><p>&nbsp;</p>
    <form name="frmCart" method="get">
        <table width="100%" cellspacing="0" cellpadding="0" border="0">
            <tr class="text">
                <td height="12">&nbsp;</td>
                <td width="37%" height="12">PRODUKT</td>
                <td width="16%">VARETYPE</td>
                <td height="12">ANTAL</td>
                <td height="12" colspan="2" align="right">PRIS PR. STK.</td>
                <td height="12">&nbsp;</td>
                <td height="12">SLET VAREN?</td>
          </tr>
            <tr class="text">
              <td height="13" colspan="8"><img src="gfx/prik.jpg" width="100%" height="1"></td>
          </tr>
            <?php
           
            while($row = mysql_fetch_array($result))
            {
                // Increment the total cost of all items
                $totalCost += ($row["qty"] * $row["itemPrice"]);
                ?>
                    <tr class="text">
                        <td width="2%" height="25">&nbsp;</td>
                        <td class="text"><?php echo $row["itemName"]; ?></td>
                        <td class="text"><?php echo str_replace("_", " ", $row['itemType']); ?></td>
                        <td width="14%" height="25" class="text">
                           
                            <select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
                              <?php
                               
                                    //for($i = 1; $i <= 20; $i++)
                                    for($i = 1; $i <= $row["itemQty"]; $i++)
                                    {
                                        echo "<option ";
                                        if($row["qty"] == $i)
                                        {
                                            echo " SELECTED ";
                                        }
                                        echo ">" . $i . "</option>";
                                    }
                                ?>
                            </select>
                        </td>
                        <td width="4%" height="25" align="right">
                              KR.
                        </td>
                        <td width="7%" align="right"><?php echo " ".number_format($row["itemPrice"], 2, ",", ","); ?></td>
                        <td width="8%">&nbsp;</td>
                        <td width="12%" height="25">
                              <a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>" class="style2">SLET VAREN</a>
                        </td>
                    </tr>
                <?php
            }
           
            // Display the total
            ?>
                    <tr class="text">
                        <td colspan="8"><img src="gfx/prik.jpg" width="100%" height="1"> </td>
                    </tr>
                    <tr class="text">
                        <td>&nbsp;</td>
                        <td colspan="2">&nbsp;</td>
                       
            <td align="right">TOTAL:&nbsp; </td>
                        <td align="right"> KR.</td>
                        <td align="right"><b><?php echo " ".number_format($totalCost, 2, ",", "."); ?></b></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr class="text">
                      <td>&nbsp;</td>
                      <td colspan="2">&nbsp;</td>
                      <td align="right">&nbsp;</td>
                      <td colspan="4">&nbsp;</td>
          </tr>
                    <tr class="text">
                      <td>&nbsp;</td>
                      <td colspan="2">+ Porto i henhold til Post Danmark </td>
                      <td align="right">&nbsp;</td>
                      <td align="right">KR.</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
          </tr>
                    <tr class="text">
                      <td>&nbsp;</td>
                      <td colspan="2">&nbsp;</td>
                      <td align="right">&nbsp;</td>
                      <td colspan="4">&nbsp;</td>
          </tr>
                    <tr class="text">
                      <td>&nbsp;</td>
                      <td colspan="2"><a href="java script:history.go(-1);" class="style2">&lt;&lt; Keep Shopping</a></td>
                      <td align="right">&nbsp;</td>
                     
            <td colspan="4"><a href="bestil.php">Send bestilling &gt;&gt;</a>
            </td>
          </tr>
        </table>
      </form>
    </p></td>
  </tr>
  <tr>
    <td height="252" align="right" valign="bottom" bgcolor="#F2E3C8" class="body"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="3" height="2" bgcolor="#D8C5A3"></td>
      </tr>
      <tr>
        <td width="16%" align="right" id="dato" title="Dato" class="style2"><p><p class="style2 style5"><br>
              <span id="minSpan"></span></p></td>
        <td width="83%" align="right" class="style2"><p class="style2 style5"><br>
        Design og programmering af Klaus Nissen, <a href="http://www.triodimenzionale.dk" target="_blank">www.Triodimenzionale.dk</a></p></td>
        <td width="1%" align="right">&nbsp;</td>
      </tr>
      <tr>
        <td align="right" valign="middle" id="dato" title="Dato" class="style2">&nbsp;</td>
        <td align="right" class="style2">&nbsp;</td>
        <td align="right">&nbsp;</td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>
            <?php
    }

?>
Avatar billede snigeren Nybegynder
02. januar 2006 - 21:20 #1
ahhh det er vist ok at den laver flere forskellige entries i cart tabellen,.. de får alle samme cookieId. Det eneste problem er nu hvordan jeg sletter alle entries med cookieId = 098jk32o4iuo2iu523

cart.php sender produkt infos viddere til bestil.php som sender produkt og bruger infos viddere til post.php som så sørger for at der bliver sendt bestilling og bekræftelse til køberen.

Hvordan sletter jeg så cookieId fra post.php siden?
Avatar billede snigeren Nybegynder
03. januar 2006 - 15:06 #2
lukker
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester