gongpex Posted September 29, 2012 Share Posted September 29, 2012 Hello everyone, I think all of you know about shopping cart, I had tried to create shopping cart on my site into localhost, using firefox It works, but when I open on other browser and open my localhost using it, cause my cart on firefox lost or deleted (I open two browser and test my site). Q : Is if I open my site on localhost using two different browser simultaneously it can cause session_destroy? Q : Should I upload my site to web hosting so that when I open using 2 browser simultaneously it cannot cause session_destroy? please answer Thanks Link to comment Share on other sites More sharing options...
kanchatchai Posted September 29, 2012 Share Posted September 29, 2012 http://w3schools.invisionzone.com/index.php?showtopic=44974&st=0&p=250448entry250448 Link to comment Share on other sites More sharing options...
birbal Posted September 29, 2012 Share Posted September 29, 2012 session will destroy when you close the browser. usually you cant use same session from two different browser. as by default session workked with session cookie and cookies are very much browser specific. Link to comment Share on other sites More sharing options...
gongpex Posted September 30, 2012 Author Share Posted September 30, 2012 (edited) http://w3schools.inv...48 Q : So, actually user buy first (without login) on shopping cart, it's without using database? Q : And all of the product and basket (everything) are using session? Please tell me Thanks Edited September 30, 2012 by gong Link to comment Share on other sites More sharing options...
birbal Posted September 30, 2012 Share Posted September 30, 2012 products are fetched from database. shoping cart use session to store the information of the selected product. they holds reference to the product objects. when you buy the products it fetches the products again from the shoping cart ad take certain action like buying,billing etc. Link to comment Share on other sites More sharing options...
gongpex Posted October 1, 2012 Author Share Posted October 1, 2012 Ok, thanks for answer, but if I am stuck I will post here again. Link to comment Share on other sites More sharing options...
gongpex Posted October 2, 2012 Author Share Posted October 2, 2012 (edited) Hello everyone, if I using this session code it cannot add product when user buy other product : Code I : <form method="post" action="buy.php"> <input type="text" value="<?$id1 = $product_id?>" style="display:none" name="test" /></div> <div class="pr" style="height:15px"><b style="font-size:14px"><?echo"$product_name";?></b></div> <div class="pr"><input type="submit" value="BUY NOW" /></div></form> //this is product 1 <form method="post" action="buy.php"> <input type="text" value="<?$id2 = $product_id2;?>" style="display:none" name="test" /></div> <div class="pr" style="height:15px"><b style="font-size:14px"><?echo"$product_name2";?></b></div> <div class="pr"><input type="submit" value="BUY NOW" /></div></form> //this is product 2 Code II (buy.php) <?require("database.php");$s = $_POST['test'];session_start();$_SESSION['$s']= $s;header("location: index.php?p=shop");?> Code III (this is page where the cart is shown content_shop.php) : <?if($_SESSION['$s']){$product_db = mysql_query("select * from product where p_id='".$_SESSION['$s']."'");$product_list = mysql_fetch_array($product_db);$product_title = $product_list['p_title'];$product_price = $product_list['p_price'];$product_img = $product_list['p_image'];echo"$product_title<br />$product_price<br />$product_img<br />".$_SESSION['total']."<br />";}else{echo"fail";}?> When I click "BUY NOW" it can added to basket cart, but when I buy other product, it changes to last product (product that last to be clicked) Q : How to display using session, all product that I had bought (on shopping cart)? please help me Thanks : Edited October 2, 2012 by gong Link to comment Share on other sites More sharing options...
justsomeguy Posted October 2, 2012 Share Posted October 2, 2012 You keep overwriting $_SESSION['$s'] with the new product. You should make an array in the session and keep adding products to the array instead of replacing the existing one. It might be better to use a more meaningful name than '$s' also. Link to comment Share on other sites More sharing options...
kanchatchai Posted October 3, 2012 Share Posted October 3, 2012 code Iload product from databasemove form in to loop and make it only one form . Link to comment Share on other sites More sharing options...
gongpex Posted October 3, 2012 Author Share Posted October 3, 2012 Ok, thanks everyone, I'll try this , but if I stuck I will post here again please help me Once again , Thanks Link to comment Share on other sites More sharing options...
gongpex Posted October 4, 2012 Author Share Posted October 4, 2012 Hello, You keep overwriting $_SESSION['$s'] with the new product. You should make an array in the session and keep adding products to the array instead of replacing the existing one. Q : Sorry, I rather don't understand can you explain me more? This my new code : Code II (buy.php) : <?require("database.php");$s = $_POST['test'];$product_db = mysql_query("select * from product where p_id='$s'");$p_list = mysql_fetch_array($product_db);$p_title = $p_list['p_title'];$p_price = $p_list['p_price'];$p_img = $p_list['p_image']; session_start();$_SESSION['pid'] = $s;$_SESSION['buy'][0] = $p_title;$_SESSION['buy'][1] = $p_price;$_SESSION['buy'][2] = $p_img;header("location: index.php?p=shop");?> This is code III (content_shop.php) : <?if($_SESSION['pid']){$x=0;while($x<2){$product['title'][$x] = $_SESSION['buy'][0];$product['price'][$x] = $_SESSION['buy'][1];$product['img'][$x] = $_SESSION['buy'][2];echo"".$product['title'][$x]."<br />".$product['price'][$x]."<br />".$product['img'][$x]."<br /><br />";$x++;}}else{echo"fail";}?> This is result from my code : And this is my expectation (this result from image modification using photoshop tool): Q : What's mistake on my code? please help me Thanks Link to comment Share on other sites More sharing options...
Ingolme Posted October 4, 2012 Share Posted October 4, 2012 If you're using a location header you should save the session data using session_write_close(). Did you forget to initialize $_SESSION['buy'] as an array?$_SESSION['buy'] = array(); session_start();$_SESSION['pid'] = $s;$_SESSION['buy'][0] = $p_title;$_SESSION['buy'][1] = $p_price;$_SESSION['buy'][2] = $p_img;session_write_close()header("location: index.php?p=shop"); Link to comment Share on other sites More sharing options...
gongpex Posted October 4, 2012 Author Share Posted October 4, 2012 Did you forget to initialize $_SESSION['buy'] as an array?$_SESSION['buy'] = array(); I think to initialize session as an array no need to add "array()" on session Q : So, it's must be written before I made another array? please help me Thanks Link to comment Share on other sites More sharing options...
Ingolme Posted October 4, 2012 Share Posted October 4, 2012 $_SESSION is an array, but unless you tell it to be, $_SESSION['buy'] is not an array. The root of your problem is in this loop. You're only taking the data from one single product and printing it twice. $_SESSION['buy'] doesn't change its value at any point. while($x<2){$product['title'][$x] = $_SESSION['buy'][0];$product['price'][$x] = $_SESSION['buy'][1];$product['img'][$x] = $_SESSION['buy'][2];echo"".$product['title'][$x]."<br />".$product['price'][$x]."<br />".$product['img'][$x]."<br /><br />";$x++;}}else{echo"fail";} Perhaps you need to add an extra level in your array. $data = array();$data[0] = $p_title;$data[1] = $p_price;$data[2] = $p_img;$_SESSION['buy'][] = $data; -----while($x<2){ $product['title'][$x] = $_SESSION['buy'][$x][0]; $product['price'][$x] = $_SESSION['buy'][$x][1]; $product['img'][$x] = $_SESSION['buy'][$x][2]; Link to comment Share on other sites More sharing options...
gongpex Posted October 5, 2012 Author Share Posted October 5, 2012 (edited) Hi, You right, thanks for answer. But I didn't found tutorial about : $_SESSION['buy'][] = $data; Q : What the meaning of this code? on : http://w3schools.com.../php_arrays.asp On this tutorial it's not written : array should be initialized first , then you create arrays. (please see on numeric arrays example) and this the code <?php$cars[0]="Saab";$cars[1]="Volvo";$cars[2]="BMW";$cars[3]="Toyota";echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";?> This is made me thought , if not necessary to initialize array ($data=array(); then , $data[0] =... and etc) please tell me Thanks Edited October 5, 2012 by gong Link to comment Share on other sites More sharing options...
gongpex Posted October 5, 2012 Author Share Posted October 5, 2012 Hi everyone, please see this code : while($x<2){$product['title'][$x] = $_SESSION['buy'][0];$product['price'][$x] = $_SESSION['buy'][1];$product['img'][$x] = $_SESSION['buy'][2];} it's now result like this : it's can be shown because : (while($x<2)), Q : if product more than 2, of course I cannot using $x<2 it should be : $x<$count or $etc, how to create so that $count can be shown automatically based on ordered product ? please help me Thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted October 5, 2012 Share Posted October 5, 2012 Putting the empty brackets at the end of the array is shorthand for pushing an element onto the end of the array: http://www.php.net/manual/en/function.array-push.php You should always initialize an array, the tutorial is not correct to leave that out. If you have maximum error reporting on then you'll see a notice otherwise. The count function returns the number of elements in an array for you to loop through. http://www.php.net/manual/en/function.count.php Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now