bionic25 Posted September 5, 2009 Author Share Posted September 5, 2009 if you want the whole code heres the page that holds it. <?php // Include functions require_once('functions.php'); // Start the session session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="jq.js"></script> <script> var refreshId = setInterval(function() { $('#refresh')./*fadeOut("slow").*/load('getCart.php')/*.fadeIn("slow")*/; }, 1000); </script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <div id="refresh" style="float: right; width: 300px; background: #ccc;"> </div> <h1>products</h1> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get" name="qty"> <?php dbcon(); productDump(); addItem(); onDelete(); ?> </form> </body> </html> heres the functions page <?php //FUNCTIONS function dbcon(){ mysql_connect("db1", "**", "**") or die(mysql_error()); mysql_select_db("**") or die(mysql_error()); } function getCart(){ echo "<h2>Basket:</h2>"; dbcon(); /* //testing purposes session_start(); $_SESSION['cart']="123,456,123,123"; */ //eg entry ,123456*01 $items=$_SESSION['cart']; $item=explode(',',$items); foreach ($item as $x) { $pidBit = substr($x, 0, 6); echo "$pidBit"; $result = mysql_query("select * from products where id= $pidBit"); while($r=mysql_fetch_array($result)) { $title = $r['title']; $description = $r['description']; $price = $r['price']; echo "$title<br />$description<br />$price <a href=this.php?action=remove&pid=$pidBit>remove</a><hr />"; } } } function onDelete(){ if ($_GET['action']=='remove') { $delPid = $_GET['pid']; $items=$_SESSION['cart']; $item=explode(',',$items); foreach ($item as $x) { if ($pidBit == $x) { //$items - $x = (session-pid) $rep = ",".$pidBit; $newCart = str_replace($rep, "", $items); /* //handles first item if that is item being deleted $newerCart = str_replace($x, "", $newCart); */ $_SESSION['cart']=$newCart; } else { } } echo "new calculated string: $newCart"; $ses=$_SESSION['cart']; echo " new session output: $ses"; } else{} } function addItem(){ if ($_GET['action']=='add') { $newPid = $_GET['pid']; $qty = $_GET['qty']; $oldCart = $_SESSION['cart']; $newCart = $oldCart.','.$newPid.'*'.$qty; $_SESSION['cart']=$newCart; } else { } } function productDump(){ $result = mysql_query("select * from products"); while($r=mysql_fetch_array($result)) { $title = $r['title']; $description = $r['description']; $price = $r['price']; $pid = $r['id']; echo "$title<br />$description<br />$price <select name=qty id=qty> <option value='1'>1</option> <option value='2'>2</option> <option value='3'>3</option> <option value='4'>4</option> <option value='5'>5</option> </select> <input type=submit name=submit value=add /><hr />"; $qty = null;//ADD if (isset($_GET['submit'])) { $qty= (int)$_GET['qty'];//ADD the (int) $url="this.php?action=add&pid=$pid&qty=$qty"; echo "$qty"; //echo "<script>window.location = \"$url\"</script>"; /* echo "<script>window.location = \"$url\"</script>"; */ } else { } } } ?> please note the ocde is still very much under construction and done from scratch with limited knowhow if it looks clumsy. Quote Link to comment Share on other sites More sharing options...
bionic25 Posted September 5, 2009 Author Share Posted September 5, 2009 also bearing in mind that it does actually output the right choice in some way qty=4&submit=add&qty=1&qty=1&qty=1 couldnt i set it as like $qtyString or something and then maniuplate it to cut the dead bits and set the correct bit of string as $qty??? Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 Just figured it out. Just bear with me here :-D Your productdump() function loops through adding forms for each product id. So what you need to do is to be able to determine how they change each of the products... Since each product is using the same form, this means that same form gets repeated, so when you submit, you literally submit ALL forms. So we need to fix this by adding a unique identifier. function productDump(){ $result = mysql_query("select * from products"); while($r=mysql_fetch_array($result)) { $title = $r['title']; $description = $r['description']; $price = $r['price']; $pid = $r['id']; echo "<form action={$_SERVER['PHP_SELF']}' method='get' name='qty'>";//remove this line from the other page echo "$title<br />$description<br />$price <select name=qty id=qty> <option value='1'>1</option> <option value='2'>2</option> <option value='3'>3</option> <option value='4'>4</option> <option value='5'>5</option> </select> <input type=hidden name='pid' value=$pid'> <input type=submit name=submit value=add /></form><hr />"; $qty = null;//ADD if (isset($_GET['submit'])) { $qty= (int)$_GET['qty'];//ADD the (int) $url="this.php?action=add&pid=$pid&qty=$qty"; echo "$qty"; //echo "<script>window.location = \"$url\"</script>"; /* echo "<script>window.location = \"$url\"</script>"; */ } else { } } } Then, you'll be able to determine the product id that you're handling with $_POST['pid'] and the qty with $_POST['qty'] Quote Link to comment Share on other sites More sharing options...
bionic25 Posted September 5, 2009 Author Share Posted September 5, 2009 Brilliant works great now! i see the problem looks so obvious now but well done for spotting it! thanks to all the responses appreciate it just need to get good enough to help out some others now ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.