slobodnium Posted February 1, 2010 Share Posted February 1, 2010 I have a problem with sessions. I have two pages. One is poruc.php and the second one is pocesor.php in poruc.php i have a table with products listed from the database and in the cell of the each product i have the id of it and a text field for quantity and submit button the form action is second page procesor.php at that page i take the variable of the ID (so i can now which is the product) and the quantity and i store that in the session and then reload the poruc.php but when i reload the poruc.php in the forms textfield for that product (id) i still get a zero and not a quantity i entered here is the procesor.php page ----------------------------------------------- <?php session_start(); $idi=$_GET['idi']; $izbroj=$_GET['izbroj']; $_SESSION[$idi]=$izbroj; header ("Location: poruc.php"); ?> -------------------------------------------- Code: begining of the poruc.php ------------------------------------------------ <?php include ("putanja..../sifre.inc"); session_start(); $gdesi = curPageURL(); ?> .... rest of the poruc.php Code: <table class="price" border=0 cellpadding=0 cellspacing=0> <tr> <td width="7"><IMG SRC="img/kutija-11.gif"></td><td class="rowm">Slika</td><td class="rowm">Šifra</td><td class="rowm">Naziv</td><td class="rowm">Kategorija</td><td class="rowm">Opis</td><td width="7"><IMG SRC="img/kutija-12.gif"></td></tr> <?php $db = mysql_connect($server, $proiz,$proizpass); mysql_select_db($baza,$db); mysql_query("SET NAMES utf8"); mysql_query("SET CHARACTER SET utf8"); mysql_query("SET COLLATION_CONNECTION='utf8_general_ci'"); $query="SELECT * FROM proizvodi WHERE kategorija = $vrsta"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i < $num) { $id=mysql_result($result,$i,"id"); $slika=mysql_result($result,$i,"Slika"); $sifra=mysql_result($result,$i,"Sifra"); $naziv=mysql_result($result,$i,"Naziv"); $kategorija=mysql_result($result,$i,"Kategorija"); switch ($kategorija) { // here is the list of categories not important for this part case "1": $kategorija = "kategorijeeee"; break; } $opis=mysql_result($result,$i,"Opis"); if(isset($_SESSION[$id])) //$kolicina is a quantity $kolicina=$_SESSION[$id]; else $kolicina=0; echo "<tr class=\"row1\"> <td width=\"7\" background=\"img/kutija-vertikalno1.gif\"></td><td class=\"naziv\"><A rel=\"ibox\" href=\"img/proizvodi/$slika\"><IMG SRC=\"img/proizvodi/$slika\" border=0 width=\"105\" height=\"105\"></a></td><td class=\"naziv\">$sifra</td><td class=\"naziv\">$naziv</td><td class=\"naziv\">$kategorija</td><td class=\"naziv\">$opis</td><td class=\"naziv\"><FORM action=\"procesor.php\" method=\"GET\"><input type=\"text\" name=\"izbroj\" id=\"izbroj\" size=\"2\" value=".$kolicina."><input TYPE=\"hidden\" VALUE=$id NAME=\"idi\"><input type=\"SUBMIT\" value=\"DODAJ\" /></FORM></td><td width=\"7\" background=\"img/kutija-vertikalno2.gif\"></td> </tr> <tr><td width=\"7\" height=\"10\"><IMG SRC=\"img/kutija-levo.gif\"></td><td background=\"img/kutija-sredina.gif\"></td><td background=\"img/kutija-sredina.gif\"></td><td background=\"img/kutija-sredina.gif\"></td><td background=\"img/kutija-sredina.gif\"></td><td background=\"img/kutija-sredina.gif\"></td><td width=\"7\" height=\"10\"><IMG SRC=\"img/kutija-desno.gif\"></td></tr> "; $i++; } ?> </table> any help Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/ Share on other sites More sharing options...
jl5501 Posted February 1, 2010 Share Posted February 1, 2010 You seem to be loading $_SESSION['idi'] and reading $_SESSION['id'] Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1004938 Share on other sites More sharing options...
slobodnium Posted February 1, 2010 Author Share Posted February 1, 2010 it is not that in the form i have a hidden field which set the value of $id to the idi and i get that value in the pocesor page and have $idi variable (which is the same as the $id) so i store that and for the session variables i have placed the $idi variable and not the 'sometext' so in my logic when i have product id = 30 and quantity 100 he will store $_SESSION[$idi]=$izbroj; and that is $_SESSION[30]=100; am i right ? it work ok at procesor.php page when i enter echo $_SESSION[30] i get value 100 ... but i don't know why then at the poruc.php in the cell where the id is 30 why doesn't he loads session value 100 and prints it out in the textfild kolicina/quantity Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1004945 Share on other sites More sharing options...
jl5501 Posted February 1, 2010 Share Posted February 1, 2010 You have not posted any code where $_SESSION['id'] gets loaded, yet that is what you are reading Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1004947 Share on other sites More sharing options...
slobodnium Posted February 1, 2010 Author Share Posted February 1, 2010 yes i have this is the part of the code that loads just before printing out the cell of the table if(isset($_SESSION[$id])) //$kolicina is a quantity $kolicina=$_SESSION[$id]; else $kolicina=0; it is in my first post too it checks if the session for that id (example 30) is set then $kolicina (which is printed out in the cell form textfield) is equal to that value from session if not set it to 0 (this i am doing so the products id that are not set in the session have the value of 0 - not store all them in session) Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1004948 Share on other sites More sharing options...
slobodnium Posted February 2, 2010 Author Share Posted February 2, 2010 anyone ? so the general problem is to transfer a variable from a form to another page and on that another page to store that variable value in the session and go back to first page and in that form field to be written new value i have an variable $id i don't know it because it is inside the loop and read it from the db and there is a $quantity (kolicina) for that id so i want the value of the variable $id (for example 30) and the variable $quantity (100) to be stored in the session and next thing when the original page loads that inside that table when the cell for that id comes true it prints a quantity of 100 and not 0 Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1005367 Share on other sites More sharing options...
ochi Posted February 2, 2010 Share Posted February 2, 2010 Hello, I just looked this: if(isset($_SESSION[$id])) //$kolicina is a quantity $kolicina=$_SESSION[$id]; else $kolicina=0; but, the condition isset is always false, it's empty, you never insert anything! I hope it'll help you! Regards Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1005373 Share on other sites More sharing options...
slobodnium Posted February 2, 2010 Author Share Posted February 2, 2010 okay how to set the condition to check if the value of one variable is set to the session i have and $id which is 30 for example so i want to check if $_SESSION['30'] is set ? it is in the loop so i must use variable $id and not 30 because it reads all the products from the db and each product have a different unique id because i want to have $_SESSION['30']=100; which means for product 30 quantity is 100 so in the loop when i read all the products and display them in a cells i want to promty for the none set products quantity 0 and for the one that is set like the 30 one to display 100 and not 0 can you helo Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1005380 Share on other sites More sharing options...
PFMaBiSmAd Posted February 2, 2010 Share Posted February 2, 2010 Why not check what is in the $_SESSION array so that you know if poruc.php is receiving the data? Add the following lines of code intermediately after the session_start() statement - echo "<pre>"; print_r($_SESSION); echo "</pre>"; And if that shows an empty array, add the following two lines of code immediately after the first opening <?php tag so that php will display all the errors it detects - ini_set("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1005385 Share on other sites More sharing options...
ochi Posted February 2, 2010 Share Posted February 2, 2010 the problem is that you NEVER do: $_SESSION[$id] = something so, it's always empty! Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1005388 Share on other sites More sharing options...
PFMaBiSmAd Posted February 2, 2010 Share Posted February 2, 2010 Yes, $_SESSION[x] is being set, provided the $_GET variables in the code in procesor.php have expected values. Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1005392 Share on other sites More sharing options...
slobodnium Posted February 2, 2010 Author Share Posted February 2, 2010 well i added the part for the error reports and still nothing no error and empty array Array ( ) at the procesor.php when i doo an echo $idi and echo $izbroj i get the correct values i want why does then $_SESSION[$idi]=$izbroj; doesn't want to store them for example $idi 30 and $izbroj 100 why does then $_SESSION[$idi]=$izbroj; not equal/result $_SESSION['30']=100; if my values are $idi 30 and $izbroj 100 as the echo prompt right values at procesor.php Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1005424 Share on other sites More sharing options...
PFMaBiSmAd Posted February 2, 2010 Share Posted February 2, 2010 Add the ini_set("display_errors", "1"); error_reporting(E_ALL); code to procesor.php as well, right after the <?php tag (before the session_start()). Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1005427 Share on other sites More sharing options...
slobodnium Posted February 2, 2010 Author Share Posted February 2, 2010 i get an error on procesor.php when i enabled error reports Notice: Unknown: Skipping numeric key 30. in Unknown on line 0 Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1005453 Share on other sites More sharing options...
PFMaBiSmAd Posted February 2, 2010 Share Posted February 2, 2010 It tuns out that $_SESSION can only be an associative array. You need to use a name and make a 2-dimensional array - $_SESSION['qty'][$idi]=$izbroj; Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1005505 Share on other sites More sharing options...
slobodnium Posted February 2, 2010 Author Share Posted February 2, 2010 and how then to see if the $id is set in session for that value of the $id if(isset($_SESSION[$id])) { $quantity=$_SESSION[$id]; } else { $quantity=0;} how to doo this from 2dimensional array ? Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1005516 Share on other sites More sharing options...
slobodnium Posted February 2, 2010 Author Share Posted February 2, 2010 i managed by myslef thank you you have been very helpfull BESTttttttttttt Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1005519 Share on other sites More sharing options...
slobodnium Posted February 5, 2010 Author Share Posted February 5, 2010 okay next prob i have now in session $_session['2']=30; $_session['8']=40; $_session['12']=10; $_session['10']=5; $_session['22']=3; first are the id's of the products and the quantity how can i know how much of them is stored so i can print them in a table ? Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1007405 Share on other sites More sharing options...
slobodnium Posted February 5, 2010 Author Share Posted February 5, 2010 i have them in a $_session['products']['2']=30; $_session['products']['8']=40; $_session['products']['12']=10; $_session['products']['10']=5; $_session['products']['22']=3; how can i print them out like a order list so the visitor can see what have he added to the basket Quote Link to comment https://forums.phpfreaks.com/topic/190522-session-problem/#findComment-1007413 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.