SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    ontario
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question check for duplicate items?

    HI,

    I have an ItemList that lists item in a cart (Javascript). What i need to do is check that there are not duplicate items in the cart.
    PHP Code:
    The following will print out each item in cart
    document
    .write(''+theitem+''
    How do I check that the same item hasn't been added twice?

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any chance of seing the actual code?
    ::: certified wild guess :::

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    ontario
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adios
    Any chance of seing the actual code?
    here:
    PHP Code:
    <script LANGUAGE="JavaScript">
        function showBasket() {
            index = document.cookie.indexOf("TheBasket");
            countbegin = (document.cookie.indexOf("=", index) + 1);
                countend = document.cookie.indexOf(";", index);
                if (countend == -1) {
                        countend = document.cookie.length;
                }
            fulllist = document.cookie.substring(countbegin, countend);
            subtotal = 0;
            document.writeln('<CENTER><FORM NAME="updateform"><center><font size="5" color="#000000" face="Verdana, Arial, Helvetica, sans-serif"><b><img src="images/viewcart.jpg"></B></FONT></center><HR WIDTH=75% size=1 COLOR="#000000">');
            document.writeln('<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=2>');

    document.writeln('<TR><TD BGCOLOR="#D2B48C"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $txt_qty ?></b></FONT></TD><TD BGCOLOR="#D2B48C"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $txt_code_no ?></b></FONT></TD><TD BGCOLOR="#D2B48C"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $txt_item ?></b></FONT></TD><TD BGCOLOR="#D2B48C"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $txt_option ?></b></FONT></TD><TD BGCOLOR="#D2B48C"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b>Points</b></FONT></TD><td BGCOLOR="#D2B48C"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b></b></FONT><TD BGCOLOR="#D2B48C"><b><FONT COLOR="#D2B48C">.</FONT></b></TD></TR>');
            itemlist = 0;
            for (var i = 0; i <= fulllist.length; i++) {
                if (fulllist.substring(i,i+1) == '[') {
                    itemstart = i+1;
                    thisitem = 1;
                } else if (fulllist.substring(i,i+1) == ']') {
                    itemend = i;
                    thequantity = fulllist.substring(itemstart, itemend);
                    itemtotal = 0;
                    itemtotal = (eval(theprice*thequantity));
                    temptotal = itemtotal;
                    subtotal = subtotal + itemtotal;
                    itemlist=itemlist+1;
                    document.write('<tr><td align=middle BGCOLOR="#FAFAD2"><?php echo "'+thequantity+'" ?></td><td BGCOLOR="#FAFAD2"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><a href=prod_show.php?code_no='+thenumber+'>'+thenumber+'</A></FONT></td>');

                    document.write('<td align=left BGCOLOR="#FAFAD2"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><a href=prod_show.php?code_no='+thenumber+'>'+theitem+'</A></FONT></td><td align=left BGCOLOR="#FAFAD2"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">'+theoption+'</FONT></td><td align=right BGCOLOR="#FAFAD2"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">'+theprice+'</FONT></td><td align=right BGCOLOR="#FAFAD2"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"></FONT></td><td WIDTH="38" align=right BGCOLOR="#FAFAD2"><a href="javascript:removeItem('+itemlist+')"><IMG SRC="images/remove.gif" WIDTH="13" HEIGHT="15" BORDER="0" ALT="<?php echo $txt_remove ?>"></a>&nbsp;</td></tr>');

                } else if (fulllist.substring(i,i+1) == '|') {
                    if (thisitem==1) theitem = fulllist.substring(itemstart, i);
                    if (thisitem==2) theprice = fulllist.substring(itemstart, i);
                    if (thisitem==3) thenumber = fulllist.substring(itemstart, i);
                    if (thisitem==4) theoption = fulllist.substring(itemstart, i);
                    thisitem++;
                    itemstart=i+1;
                }
            }
            document.writeln('<tr><td align=right BGCOLOR="#D2B48C" colspan=5><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>Total Points: '+subtotal+'</b></FONT></td><td BGCOLOR="#D2B48C">&nbsp</td></td><td BGCOLOR="#D2B48C">&nbsp</td></tr>');
            document.writeln('</TABLE><CENTER><CENTER><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><IMG SRC="images/remove.gif" WIDTH="13" HEIGHT="15" BORDER="0" ALT="<?php echo $txt_remove ?>"> = <?php echo $txt_remove ?></FONT></CENTER></CENTER>');
            document.writeln('</FORM>');

        }


        function updateItem(itemno, newquant) {
            newItemList = null;
            itemlist = 0;
            for (var i = 0; i <= fulllist.length; i++) {
                if (fulllist.substring(i,i+1) == '[') {
                    thisitem = 1;
                    itemstart = i+1;
                    fullstart = i+1;
                } else if (fulllist.substring(i,i+1) == ']') {
                    itemend = i;
                    itemlist=itemlist+1;
                    if (itemlist != itemno) {
                        newItemList = newItemList+'['+fulllist.substring(fullstart, itemend)+']';
                    } else {
        newItemList = newItemList + '['+theitem+'|'+theprice+'|'+theoption+'|'+thenumber+'|'+newquant+'|';
                    }
                } else if (fulllist.substring(i,i+1) == '|') {
                    if (thisitem==1) theitem = fulllist.substring(itemstart, i);
                    if (thisitem==2) theprice = fulllist.substring(itemstart, i);
                    if (thisitem==3) theoption = fulllist.substring(itemstart, i);
                    if (thisitem==4) thenumber = fulllist.substring(itemstart, i);
                    if (thisitem==5) theweight = fulllist.substring(itemstart, i);
                    thisitem++;
                    itemstart=i+1;
                }
            }
            index = document.cookie.indexOf("TheBasket");
            document.cookie="TheBasket="+newItemList;
            self.location = "basket.php";
                    top.frames[2].location = "middle_right.php";
        

        }

        function removeItem(itemno) {
            newItemList = null;
            itemlist = 0;
            for (var i = 0; i <= fulllist.length; i++) {
                if (fulllist.substring(i,i+1) == '[') {
                    itemstart = i+1;
                } else if (fulllist.substring(i,i+1) == ']') {
                    itemend = i;
                    theitem = fulllist.substring(itemstart, itemend);
                    itemlist=itemlist+1;
                    if (itemlist != itemno) {
                        newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']';
                    }
                }
            }
            index = document.cookie.indexOf("TheBasket");
            document.cookie="TheBasket="+newItemList;
            self.location = "basket.php";
                    top.frames[2].location = "middle_right.php";
        
        }

        function clearBasket() {
            if (confirm('<?php echo $txt_reset_shopping_cart ?>?')) {
                index = document.cookie.indexOf("TheBasket");
                document.cookie="TheBasket=.";
                self.location = "basket_empty.php";
                    top.frames[2].location = "middle_right.php";
            }
        }
        
    function totalCart() {
        if(subtotal < 20) {
            var cartitem = (eval(20 - subtotal));
             alert("Please add " + cartitem + "  more points.\nComplete Create - A - Pack = 20 points.")
             return false;
        }else if(subtotal > 20) {
          var carttotal = (eval(subtotal - 20));
             alert("Too many items.\n Please remove " + carttotal + " points from cart.")
             return false;
        }
        }

    </script>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •