Hi, Right i am working on a project where i need a POST value to add to a cookie which is fine as i have just done: setcookie("product", $_POST[product]); setcookie("product_select",$_POST[product_select]); Code (markup): This works fine, but what i need to do is when this goes to the next section the user can either buy or add another product i need them to be able to add another product to this cookie so on the diplay page it shows all the products they have. Any idea how i am going to do this? Cheers, Adam
I would store the data in serialized arrays, just keep setting the cookie with the new serial data everytime someone posts or gets to the page.......
I am afraid you have lost me a bit there. Please can you explain a bit more on how i should go about this.
You can put an array in the cookie. So you can save many things there. I think you can better use a database order by ip or user_id
no you can't store arrays in cookies. I've not so much time today, but I wrote this for you, hopefully you'll see where I'm going with it, I didn't bother to serialize the data it would just be too confusing for you .... <?php error_reporting( E_ALL ); class cookie { function cookie( $name, $expires = null, $path = null, $domain = null, $secure = null ) { $this->name = $name; $this->expires = $expires; $this->path = $path; $this->domain = $domain; $this->secure = $secure; if(! $_COOKIE[ $this->name ] ) setcookie( $this->name, "null" ); } function addrecord( $data ) { return setcookie( $this->name, $this->getcookie( ) . ":" . $data, $this->expires, $this->path, $this->domain, $this->secure ); } function delrecord( $data ) { return setcookie( $this->name, preg_replace( "#:$data#si", '', $this->getcookie( ) ), $this->expires, $this->path, $this->domain, $this->secure ); } function getcookie( ) { return $_COOKIE[ $this->name ] ? implode(":", array_values( split(":", $_COOKIE[ $this->name ] ) )) : "null"; } function delcookie( ) { return setcookie( $this->name, false ); } } $cookie = new cookie( 'products' ); if( $_POST ) { $cookie->addrecord( $_POST['prodid'] ); header("location: " . $_SERVER['PHP_SELF']); exit; } if( $_GET['delcookie'] ) { $cookie->delcookie( ); header("location: " . $_SERVER['PHP_SELF']); exit; } if( $_GET['delprod'] ) { $cookie->delrecord( $_GET['delprod'] ); header("location: " . $_SERVER['PHP_SELF']); exit; } ?> <html> <head><title>Cookies and stuff</title> <body> <h1>Delete from cookie item list</h1> <?php foreach( split(":", $cookie->getcookie( ) ) as $product ) { if( $product != 'null' and $product != "" ) printf( "<a href=\"?delprod=%s\">Del %s</a><br/>", $product, $product ); } ?> <h1>Add to cookie item list</h1> <form action="" method="post" name="theForm"> <select name="prodid" onchange="document.theForm.submit( document.theForm.prodid.value )"> <option value="null">Select Product To Add to cookie</option> <option value="001">Product 1</option> <option value="002">Product 2</option> <option value="003">Product 3</option> <option value="004">Product 4</option> <option value="005">Product 5</option> <option value="006">Product 6</option> </select> </form> <h1>Raw cookie</h1> <?php print_r( $cookie->getcookie( ) ); ?> PHP: Maybe you'll be able to use that for inspiration .....
If that is what you knock up when you don't have a lot of time then i would of loved to of seen what you knock up if you do have time. I will have to have a look through this and make sense of it all as i am not an expert on PHP so will have to get to grips with your code. Thanks a lot for reply.
What erros are you getting? I am getting several errors myself: Notice: Undefined index: products in This on multiple lines. Is this what you are getting? Cheers, Adam
<?php class cookie { function cookie( $name, $expires = null, $path = null, $domain = null, $secure = null ) { $this->name = $name; $this->expires = $expires; $this->path = $path; $this->domain = $domain; $this->secure = $secure; if( !@$_COOKIE[ $this->name ] ) setcookie( $this->name, "null" ); } function addrecord( $data ) { return setcookie( $this->name, $this->getcookie( ) . ":" . $data, $this->expires, $this->path, $this->domain, $this->secure ); } function delrecord( $data ) { return setcookie( $this->name, preg_replace( "#:$data#si", '', $this->getcookie( ) ), $this->expires, $this->path, $this->domain, $this->secure ); } function getcookie( ) { return @$_COOKIE[ $this->name ] ? implode(":", array_values( split(":", $_COOKIE[ $this->name ] ) )) : "null"; } function delcookie( ) { return setcookie( $this->name, false ); } } $cookie = new cookie( 'products' ); if( $_POST ) { $cookie->addrecord( $_POST['prodid'] ); header("location: " . $_SERVER['PHP_SELF']); exit; } if( $_GET['delcookie'] ) { $cookie->delcookie( ); header("location: " . $_SERVER['PHP_SELF']); exit; } if( $_GET['delprod'] ) { $cookie->delrecord( $_GET['delprod'] ); header("location: " . $_SERVER['PHP_SELF']); exit; } ?> <html> <head><title>Cookies and stuff</title> <body> <h1>Delete from cookie item list</h1> <?php foreach( split(":", $cookie->getcookie( ) ) as $product ) { if( $product != 'null' and $product != "" ) printf( "<a href=\"?delprod=%s\">Del %s</a><br/>", $product, $product ); } ?> <h1>Add to cookie item list</h1> <form action="" method="post" name="theForm"> <select name="prodid" onchange="document.theForm.submit( document.theForm.prodid.value )"> <option value="null">Select Product To Add to cookie</option> <option value="001">Product 1</option> <option value="002">Product 2</option> <option value="003">Product 3</option> <option value="004">Product 4</option> <option value="005">Product 5</option> <option value="006">Product 6</option> </select> </form> <h1>Raw cookie</h1> <?php print_r( $cookie->getcookie( ) ); ?> PHP: Sorry I was being half assed and left error_reporting on which will stop headers for cookies being sent if there are errors which there will be when no cookie has been set before, try that ......
Sorry for the late follow up i just have so many projects on at the moment i don't know where my head is at. You script works great and have implemented this into my site i just have one question: I am going to have 3 fields which i need to add. At present it is just: $cookie->addrecord( $_POST['prodid'] ); I need to add 'image' and also 'product select' I am not to sure how this is done, as the addrecord function does not cater for this at the moment. Again thanks for your help. Cheers, Adam
assuming the stuff is posted just call addrecord again for each thing you need to add to the cookie, or if you wanted to keep it seperate you could just create a new cookie object for each thing you want to track, like $products_c = new cookie( 'products' ); $image_c = new cookie( 'image' ); $product_select_c = new cookie( 'productselect' ); those are awful names for objects, just an example...... If I missed the point then say so and I'll try and answer again .....
Thank you for you reply, if i understand correctly would i be doing something like this: if( $_POST ) { $products_c = new cookie( $_POST['prodid'] ); $image_c = new cookie( $_POST['image'] ); $product_select_c = new cookie( $_POST['image'] ); header("location: " . $_SERVER['PHP_SELF']); exit; } Code (markup): Then how would i display the items that i have added? Cheers, Adam
Ok, right, classes ...... This syntax $blob = new class() ; that creates a new instance of that class, not connected to anything you made before, the functions() you see inside the class are refered to as methods, so when you do $cookie = new cookie( 'whatever' ); you create a new cookie object not attached to any others and so when you call functions() [ methods ] on that object they only affect that object, so the same as getting the cookie data from the first cookie you just call getcookie() on each cookie [ object ] you want the data from .... Clearer ???
Sorry to bring this one back up after so long i have had several problems with the server for this job. I am almost there now, i have got it to add the image to a seperate cookie now like this $cookieimage = new cookie( 'image' ); $cookie->addrecord( $_GET['prodid'] ); $cookieimage->addrecord( $_GET['image'] ); Code (markup): but i just don't know how to loop both the prodid cookie and the image cookie. Also i need to know how delete the image cookie when i am deleting the proid. This just deletes the product but i need it to include image also. <?php if( $product != 'null' and $product != "" ) printf( "<a href=\"?delprod=%s\">Del %s</a><br/>", $product, $product ); ?> Code (markup): My other problem is including the image into the foreach loop here: foreach( split(":", $cookie->getcookie( ) ) as $product){ Code (markup): Thank you in advance.
I need more code than that, I can't guess what your code is, post the code you want to change and try to be clearer about how you want to change it ( although I'm sure the above will make sense once I have seen your code )
Cheers for your reply Joe, sorry for not explaining my requirements correctly. Basically what i am trying to achive with this is to have a form where you can submit an image and i size. That is then added to the cart and from there you can add another image/size to the cart as another item in your basket. The form that is being submitted is the following: <form action="image-process.php" method="post" enctype='multipart/form-data'> <table width="100%" border="0" cellspacing="0" cellpadding="3"> <tr> <td><span class="style1">1. Select The Format </span></td> </tr> <tr> <td><table width="180" border="0" cellspacing="0" cellpadding="0"> <tr> <td><input type="radio" name="prodid" id="product186" value="186" /></td> <td><span class="darkgrey_pattern" style="color:#CC0000;"> <label for="product186"><strong>90 x 60 cm</strong></label> </span> </td> <td align="right"><span class="darkgrey_pattern" style="color:#CC0000;"> <label for="product186"><strong>£16.99</strong></label> </span></td> </tr> <tr> <td><input type="radio" name="prodid" id="product181" value="181" /></td> <td><span class="darkgrey_pattern"> <label for="product181"><strong>30 x 20 cm</strong></label> </span> </td> <td align="right"><span class="darkgrey_pattern"> <label for="product181"><strong>£1.99</strong></label> </span></td> </tr> <tr> <td><input type="radio" name="prodid" id="product182" value="182" /></td> <td><span class="darkgrey_pattern"> <label for="product182"><strong>40 x 30 cm</strong></label> </span> </td> <td align="right"><span class="darkgrey_pattern"> <label for="product182"><strong>£3.99</strong></label> </span></td> </tr> <tr> <td width="20"><input type="radio" name="prodid" id="product183" value="183" /></td> <td width="107"><span class="darkgrey_pattern"> <label for="product183"><strong>60 x 40 cm</strong></label> </span> </td> <td align="right" width="50"><span class="darkgrey_pattern"> <label for="product183"><strong>£8.99</strong></label> </span></td> </tr> <tr> <td><input type="radio" name="prodid" id="product213" value="213" /></td> <td><span class="darkgrey_pattern"> <label for="product213"><strong>70 x 50 cm</strong></label> </span> </td> <td align="right"><span class="darkgrey_pattern"> <label for="product213"><strong>£12.99</strong></label> </span></td> </tr> <tr> <td><input type="radio" name="prodid" id="product185" value="185" /></td> <td><span class="darkgrey_pattern"> <label for="product185"><strong>80 x 60 cm</strong></label> </span> </td> <td align="right"><span class="darkgrey_pattern"> <label for="product185"><strong>£16.99</strong></label> </span></td> </tr> <tr> <td><input type="radio" name="prodid" id="product214" value="214" /></td> <td><span class="darkgrey_pattern"> <label for="product214"><strong>100 x 70 cm</strong></label> </span> </td> <td align="right"><span class="darkgrey_pattern"> <label for="product214"><strong>£24.99</strong></label> </span></td> </tr> <tr> <td><input type="radio" name="prodid" id="product187" value="187" /></td> <td><span class="darkgrey_pattern"> <label for="product187"><strong>120 x 80 cm</strong></label> </span> </td> <td align="right"><span class="darkgrey_pattern"> <label for="product187"><strong>£39.99</strong></label> </span></td> </tr> <tr> <td><input type="radio" name="prodid" id="product224" value="224" /></td> <td><span class="darkgrey_pattern"> <label for="product224"><strong>160 x 120 cm</strong></label> </span> </td> <td align="right"><span class="darkgrey_pattern"> <label for="product224"><strong>£76.99</strong></label> </span></td> </tr> <tr> <td><input type="radio" name="prodid" id="product190" value="190" /></td> <td><span class="darkgrey_pattern"> <label for="product190"><strong>200 x 100 cm</strong></label> </span> </td> <td align="right"><span class="darkgrey_pattern"> <label for="product190"><strong>£79.99</strong></label> </span></td> </tr> </table> <table width="180" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="15"><input type="radio" name="prodid" value="0" id="dropdown_products" /></td> <td width="165"><select name="product_dropdown" size="1" onchange="pre_select_radio();" style="color:#666666;"> <option value="0" style="color:#000000;">=== More formats ===</option> <option value="0" style="font-weight:bold;color:#000000;">--- 3:2 formats</option> <option value="181" >30 x 20 cm - £1.99</option> <option value="221" >45 x 30 cm - £4.99</option> <option value="183" >60 x 40 cm - £8.99</option> <option value="222" >75 x 50 cm - £12.99</option> <option value="186" >90 x 60 cm - £16.99</option> <option value="187" >120 x 80 cm - £39.99</option> <option value="189" >150 x 100 cm - £59.99</option> <option value="225" >180 x 120 cm - £86.99</option> <option value="217" >225 x 150 cm - £139.99</option> <option value="0" style="font-weight:bold;color:#000000;">--- 4:3 formats</option> <option value="227" >28 x 21 cm - £1.99</option> <option value="182" >40 x 30 cm - £3.99</option> <option value="184" >60 x 45 cm - £9.99</option> <option value="185" >80 x 60 cm - £16.99</option> <option value="188" >120 x 90 cm - £42.99</option> <option value="223" >140 x 105 cm - £58.99</option> <option value="224" >160 x 120 cm - £76.99</option> <option value="226" >180 x 135 cm - £99.99</option> <option value="218" >200 x 150 cm - £129.99</option> <option value="0" style="font-weight:bold;color:#000000;">--- DIN formats</option> <option value="195" >A4 (29,7x21 cm) - £1.99</option> <option value="196" >A3 (42x29,7 cm) - £3.99</option> <option value="194" >A2 (59,4x42 cm) - £8.99</option> <option value="193" >A1 (84,1x59,4 cm) - £17.99</option> <option value="192" >A0 (118,9x84,1 cm) - £39.99</option> <option value="0" style="font-weight:bold;color:#000000;">--- Panorama formats</option> <option value="197" >60 x 20 cm - £3.99</option> <option value="198" >80 x 20 cm - £5.99</option> <option value="199" >90 x 30 cm - £9.99</option> <option value="200" >120 x 30 cm - £12.99</option> <option value="201" >120 x 40 cm - £16.99</option> <option value="202" >160 x 40 cm - £22.99</option> <option value="203" >150 x 50 cm - £29.99</option> <option value="204" >200 x 50 cm - £39.99</option> <option value="190" >200 x 100 cm - £79.99</option> <option value="191" >300 x 100 cm - £129.99</option> <option value="219" >300 x 150 cm - £179.99</option> <option value="220" >450 x 150 cm - £269.99</option> <option value="0" style="font-weight:bold;color:#000000;">--- Poster formats</option> <option value="213" >70 x 50 cm - £12.99</option> <option value="214" >100 x 70 cm - £24.99</option> <option value="0" style="font-weight:bold;color:#000000;">--- Square formats</option> <option value="205" >30 x 30 cm - £2.99</option> <option value="206" >40 x 40 cm - £5.99</option> <option value="207" >50 x 50 cm - £8.99</option> <option value="208" >60 x 60 cm - £12.99</option> <option value="209" >70 x 70 cm - £17.99</option> <option value="210" >80 x 80 cm - £22.99</option> <option value="211" >90 x 90 cm - £29.99</option> <option value="212" >100 x 100 cm - £39.99</option> <option value="215" >120 x 120 cm - £57.99</option> <option value="216" >150 x 150 cm - £89.99</option> </select> </td> </tr> </table></td> </tr> <tr> <td> </td> </tr> <tr> <td><span class="style1">2. Select The Image </span></td> </tr> <tr> <td><label> <input name="image" type="file" id="image" /> </label></td> </tr> <tr> <td> </td> </tr> <tr> <td><span class="style1">3. Upload </span></td> </tr> <tr> <td><label> <input type="submit" name="Submit" value="Start" /> </label></td> </tr> </table> </form> Code (markup): This submits to image-process.php which is: <?php //path where to store images $path_thumbs = "upload/thumbs"; $path_big = "upload/big"; //the new width of the resized image. $img_thumb_width = 160; // in pixcel $extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no) //allowed Extensions $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp"); //check if folders are Writable or not //please CHOMD them 777 if (!is_writeable($path_thumbs)){ die ("Error: The directory <b>($path_thumbs)</b> is NOT writable"); } if (!is_writeable($path_big)){ die ("Error: The directory <b>($path_big)</b> is NOT writable"); } foreach ($_FILES as $image) { if (strlen($image[name])>0) { $file_type = $image['type']; $file_name = $image['name']; $file_size = $image['size']; $file_tmp = $image['tmp_name']; //check file extension $ext = strrchr($file_name,'.'); $ext = strtolower($ext); if (($extlimit == "yes") && (!in_array($ext,$limitedext)) ) { echo "Wrong file extension1. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>"; exit(); } //get the file extension. $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; //create a random file name $rand_name = md5(time()); $rand_name= rand(0,999999999); //get the new width variable. $ThumbWidth = $img_thumb_width; //keep image type if($file_size){ if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){ $new_img = imagecreatefromjpeg($file_tmp); }elseif($file_type == "image/x-png" || $file_type == "image/png"){ $new_img = imagecreatefrompng($file_tmp); }elseif($file_type == "image/gif"){ $new_img = imagecreatefromgif($file_tmp); } //list width and height and keep height ratio. list($width, $height) = getimagesize($file_tmp); $imgratio=$width/$height; if ($imgratio>1){ $newwidth = $ThumbWidth; $newheight = $ThumbWidth/$imgratio; }else{ $newheight = $ThumbWidth; $newwidth = $ThumbWidth*$imgratio; } //function for resize image. if (function_exists(imagecreatetruecolor)){ $resized_img = imagecreatetruecolor($newwidth,$newheight); }else{ die("Error: Please make sure you have GD library ver 2+"); } imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //save image ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext"); ImageDestroy ($resized_img); ImageDestroy ($new_img); //print message } //upload the big image move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext"); header("Location: product-select.php?prodid=$_POST[prodid]&image=$rand_name.$file_ext"); } } ?> Code (markup): When this comes back to the page with the form it then add the cookie as i have put the following code at the top of the page with the form: <?php include ("db.config.php"); $cookie = new cookie( 'products' ); $cookieimage = new cookie( 'image' ); $cookie->addrecord( $_GET['prodid'] ); $cookieimage->addrecord( $_GET['image'] ); if( $_GET['delcookie'] ) { $cookie->delcookie( ); header("location: " . $_SERVER['PHP_SELF']); exit; } if( $_GET['delprod'] ) { $cookie->delrecord( $_GET['delprod'] ); $cookieimage->delrecord( $_GET['delimage'] ); header("location: " . $_SERVER['PHP_SELF']); exit; } ?> Code (markup): The code which is out putting the cookies is: <?php if( $image != 'null' and $image != "" ) printf( "<a href=\"?delprod=%s\">Del %s</a><br/>", $image, $image ); ?> <br /> <?php if( $product != 'null' and $product != "" ) printf( "<a href=\"?delprod=%s\">Del %s</a><br/>", $product, $product ); ?> </td> <td width="63%" valign="top"> </td> </tr> </table> <p> <?php } } } ?> Code (markup): Now for some reason this is getting a lot of problems but is adding. When i try to output the data i am getting duplicate items and also i need to add a delete button next to the product to remove from the cart but don't know how to do that so it deletes both the image cookie and the product size cookie. Thanks for your help. Adam