gazza52 Posted March 1, 2009 Share Posted March 1, 2009 Hi, I have developed a search which connects to a mysql database. From these results I have added them to a table which has an option to add the items to a shopping basket. When you go to the shopping basket the added items are shown. I have added an option to empty the basket. What I am having problems with is deleting single items from the table. My code for the shopping basket is: <? session_start(); if (!isset($_SESSION["loggedIn"])) { include ("login.php"); exit; } if (!isset($_SESSION["basketcount"])) { $items=0; } else { $items= $_SESSION["basketcount"]; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <TITLE> Shrewsbury Town FC </TITLE> <? IF (ISSET($_COOKIE["selectedStyle"])) // if style has been set { $style=$_COOKIE["selectedStyle"]; } ELSE // if style not yet set, default to 0 { $style="Master"; } ?> <LINK rel="stylesheet" href="style<?= $style; ?>.css"> </HEAD> <BODY> <DIV id="header"> <IMG src="mlogo.jpg"alt="Shrewsbury Town Unofficial Logo"> </DIV> <DIV id="date"> <? PRINT date("l M dS, Y",time()); PRINT "<br>"; PRINT date ("H:i",time()); ?> </DIV> <DIV id="basket"> <? ECHO "You have $items<br>"; ECHO "items in your basket<br>"; ECHO"<a href=\"basket.php\">View Basket</a>"; ?> </DIV> <H1>Shopping Basket</H1> <DIV id="navigation"> <UL> <LI> <A href="index.php">Home</A></LI> <LI> <A href="history.php">Club History</A></LI> <LI> <A href="clubshop.php">Club Shop</A></LI> <LI> <A href="F&R.php">Fixtures & Results</A></LI> <LI> <A href="prostar.php">Prostar Stadium</A></LI> <LI> <A href="account.php">My Account</A></LI> <LI> <A href="feedback.php">Feedback</A></LI> <LI> <A href="logout.php">Logout</A></LI> </UL> </DIV> <DIV id="resultstable"> <table align="center" border="1" cellpadding="5" cellspacing="0"> <tr> <td width="500"><b>Item Selected</b></td> <td width="100"><b>Remove Item</b></td> </tr> <? if (isset($_SESSION["basket"])) { foreach ($_SESSION["basket"] as $item) { print ("<tr>"); print ("<td>$item</td>"); print ("<td>Remove</a></td>"); print ("<tr>"); } } ?> </table> <A href="add.php?empty=true">Empty the basket</A> </DIV> </BODY> </HTML> The add.php is a script which allows me to add and remove items. The code for this is shown below: <? session_start(); IF (!ISSET($_SESSION["loggedIn"])) { INCLUDE ("login.php"); EXIT; } IF (!ISSET($_SESSION["basketcount"])) { $items=0; } ELSE { $items= $_SESSION["basketcount"]; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <TITLE> Shrewsbury Town FC </TITLE> <? IF (ISSET($_COOKIE["selectedStyle"])) // if style has been set { $style=$_COOKIE["selectedStyle"]; } ELSE // if style not yet set, default to 0 { $style="Master"; } ?> <LINK rel="stylesheet" href="style<?= $style; ?>.css"> </HEAD> <BODY> <DIV id="header"> <IMG src="mlogo.jpg"alt="Shrewsbury Town Unofficial Logo"> </DIV> <DIV id="date"> <? PRINT date("l M dS, Y",time()); PRINT "<br>"; PRINT date ("H:i",time()); ?> </DIV> <DIV id="basket"> <? ECHO "You have $items<br>"; ECHO "items in your basket<br>"; ECHO"<a href=\"basket.php\">View Basket</a>"; ?> </DIV> <H1>Online Club Shop</H1> <DIV id="navigation"> <UL> <LI> <A href="index.php">Home</A></LI> <LI> <A href="history.php">Club History</A></LI> <LI> <A href="clubshop.php">Club Shop</A></LI> <LI> <A href="F&R.php">Fixtures & Results</A></LI> <LI> <A href="prostar.php">Prostar Stadium</A></LI> <LI> <A href="account.php">My Account</A></LI> <LI> <A href="feedback.php">Feedback</A></LI> <LI> <A href="logout.php">Logout</A></LI> </UL> </DIV> <DIV id="resultstable"> <? if (isset($_GET["add"])) { $_SESSION["basket"][]=$_GET["add"]; if (isset($_GET["add"])) $_SESSION["basketcount"] = $items; $_SESSION["basketcount"] = $_SESSION["basketcount"] + 1; header ("location:http://mi-linux.wlv.ac.uk/~0525154/clubshop.php"); } if (isset($_GET["empty"])) { unset($_SESSION["basket"]); $_SESSION["basketcount"] = 0; header ("location:http://mi-linux.wlv.ac.uk/~0525154/basket.php"); } ?> <a href="basket.php">View Basket</a> <p> <a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01 Transitional" height="31" width="88"></a> </p> </div> </body> </html> Please advise on code which will allow me to delete single items from the basket. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/147482-deleting-single-rows/ Share on other sites More sharing options...
RussellReal Posted March 1, 2009 Share Posted March 1, 2009 ok I hope you understand what I'm about to say.. in `basket` you're going to need an auto_incrementing id field THEN in the basket php when you're showing the user what he has in his basket.. have the delete button send the item's ID to another php file like.. deleteItem.php?id=10 then in deleteItem.php you get the user's id from the cookie or session and do something like this <?php $id = (int) $_GET['id']; $sql = "DELETE FROM `basket` WHERE `id` = '{$id}' AND `user_id` = '{$_SESSION['user_id']}'"; mysql_query($sql); ?> Quote Link to comment https://forums.phpfreaks.com/topic/147482-deleting-single-rows/#findComment-774178 Share on other sites More sharing options...
gazza52 Posted March 1, 2009 Author Share Posted March 1, 2009 I understand where you are coming from but can you help me with the coding?. Thanks again, Quote Link to comment https://forums.phpfreaks.com/topic/147482-deleting-single-rows/#findComment-774190 Share on other sites More sharing options...
gazza52 Posted March 2, 2009 Author Share Posted March 2, 2009 Can anyone help me with this. I have now managed to add a radio button under a field called delte and a record number for each item that is added to the basket. The record number will start from 0 and go up. To test the record number I have created a button which deletes a certain record number row and this works. What I am struggling with is how to delete a certain row if its radio button is checked with one single delete button. ??? Quote Link to comment https://forums.phpfreaks.com/topic/147482-deleting-single-rows/#findComment-774584 Share on other sites More sharing options...
Yesideez Posted March 2, 2009 Share Posted March 2, 2009 I'd use a checkbox instead of a radio button. <input type="checkbox" name="delme" /> Delete That way you can simply check for an "on/off" state of "delme" like this: if ($_POST['delme']) { mysql_query("DELETE FROM basket WHERE id='".$id."' LIMIT 1"); } Something like that although you'd have to adapt it for your script. Quote Link to comment https://forums.phpfreaks.com/topic/147482-deleting-single-rows/#findComment-774614 Share on other sites More sharing options...
gazza52 Posted March 3, 2009 Author Share Posted March 3, 2009 I have now managed to get a checkbox for each item. I have put a single field in which is called Delete item. What I need to do now is say when I click delete item the checkbox which is ticked is deleted along with its row. The delete item should call the add.php script contains something like: if ($_POST['delme']) { unset (session [basket][$item]; --$item is the id field } What I do not understand is what I would send to add.php and if the code above is correct? Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/147482-deleting-single-rows/#findComment-775323 Share on other sites More sharing options...
gazza52 Posted March 3, 2009 Author Share Posted March 3, 2009 To make it easier I have put in a button instead which is called delete. What should I assign to the delete button to get it to pass the correct details to add.php to get it to delete the specific id field? Quote Link to comment https://forums.phpfreaks.com/topic/147482-deleting-single-rows/#findComment-775330 Share on other sites More sharing options...
gazza52 Posted March 3, 2009 Author Share Posted March 3, 2009 Please can anyone help me. ??? :'( Quote Link to comment https://forums.phpfreaks.com/topic/147482-deleting-single-rows/#findComment-775453 Share on other sites More sharing options...
Yesideez Posted March 3, 2009 Share Posted March 3, 2009 Have you got a submit button which is pointing to your script to handle it? The idea (I'm presuming) is to have a list of checkboxes the user can tick a few then submit the form to a script that'll see which ones are ticked and act accordingly. Now, let's presume your checkboxes are all marked something like this: <input type="checkbox" name="chk1" /> <input type="checkbox" name="chk2" /> <input type="checkbox" name="chk3" /> Now we can set up a loop to check them and see ifthey're ticked: for ($i=1;$i<$maxcheckboxes;++$i) { if ($_POST['chk'.$i]) { echo 'Checkbox '.$i.' is ticked<br />'; } } Something like that. Quote Link to comment https://forums.phpfreaks.com/topic/147482-deleting-single-rows/#findComment-775460 Share on other sites More sharing options...
sasa Posted March 3, 2009 Share Posted March 3, 2009 in 1st file use foreach ($_SESSION["basket"] as $k => $item) { print ("<tr>"); print ("<td>$item</td>"); print ("<td><a href='add.php?delete=$k'>Remove</a></td>"); print ("<tr>"); } and in add.php add if (isset($_GET["delete"])) { if(isset($_SESSION["basket"][$_GET["delete"]])) {unset($_SESSION["basket"][$_GET["delete"]]); $_SESSION["basketcount"]--;} header ("location:http://mi-linux.wlv.ac.uk/~0525154/basket.php"); } Quote Link to comment https://forums.phpfreaks.com/topic/147482-deleting-single-rows/#findComment-775466 Share on other sites More sharing options...
gazza52 Posted March 3, 2009 Author Share Posted March 3, 2009 Hi Sasa, Sounds good to me. Will let you know if it works later :-). Gary Quote Link to comment https://forums.phpfreaks.com/topic/147482-deleting-single-rows/#findComment-775473 Share on other sites More sharing options...
gazza52 Posted March 4, 2009 Author Share Posted March 4, 2009 Works a treat, thanks all. Quote Link to comment https://forums.phpfreaks.com/topic/147482-deleting-single-rows/#findComment-776164 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.