ballouta Posted November 12, 2008 Share Posted November 12, 2008 Hi, I have show~books.php page that loops over a specific category (of a book store) and print it out. during this loop, the code says that if ($_session['authen']), the code will echo a cart photo with the link cart.php In this scope, the $_session['item'] is set to $row[isbn] the big bad problem I just noticed that the $session['isbn'] is always the last book in this category, because in every row i am setting this session value to $row[isbn] which is logically wrong. my code: <?php if($_SESSION['authen']) {// if the user is logged in echo "</td> <td valign=\"top\" align=\"center\"> <p align=\"center\"><font face=\"Tahoma\" size=\"2\"> <a href=\"../shopping/cart.php\"><img border=\"0\" src=\"../images/cart.jpg\" width=\"69\" height=\"132\" alt=\"اضف هذا الكتاب للطلبية\"></a> "; } else { echo "</td> <td valign=\"top\" align=\"center\"> <p align=\"center\"><font face=\"Tahoma\" size=\"2\"> <img border=\"0\" src=\"../images/spacer.gif\" width=\"69\" height=\"132\">"; } ?> Can this be fixed because all my cart.php depends on this isbn value from this $session. thank you Quote Link to comment https://forums.phpfreaks.com/topic/132488-session-problem/ Share on other sites More sharing options...
trq Posted November 12, 2008 Share Posted November 12, 2008 We need to see the relevent code. Quote Link to comment https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-688890 Share on other sites More sharing options...
ballouta Posted November 12, 2008 Author Share Posted November 12, 2008 do you mean you want to see all my code? do i have to paste it here or you like to see it live? Thank You Quote Link to comment https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-688904 Share on other sites More sharing options...
trq Posted November 12, 2008 Share Posted November 12, 2008 No we don't want to see all your code, just what is relevent to your problem. And no, seeing your output will not help us, we need to see code. Quote Link to comment https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-688945 Share on other sites More sharing options...
ballouta Posted November 13, 2008 Author Share Posted November 13, 2008 Hi, thanks my html code is alittle bit complicated. I am using mootools to show the details for each book anyway, here's my code, note the part that says //if the user is logged in.... <?php $query = "SELECT * FROM `books` WHERE `cat` = '$cat' AND `show` = 'Y' AND `author` = 'ballouta"; $result = mysql_query($query); while( $row = mysql_fetch_array($result) ) { echo " <DIV> <DIV id=\"sResultsRow\" title=\"$row[bname]\"> <TABLE cellSpacing=0 cellPadding=0 width=\"100%\" border=0 dir=\"rtl\"> <TBODY> <TR> <TD id=sResultsImgPreview vAlign=top width=\"99\"> <p align=\"center\" style=\"margin-top: 4px\"> <a href=\"../images/store/books/$cat/$row[ncode]-big.jpg\" rel=\"lightbox\" title=\"$row[bname]\"> <img border=\"0\" src=\"../images/store/books/$cat/$row[ncode].gif\" height=\"130\"></a></TD> <td onmouseover=\"this.style.backgroundColor='FFFFCC';this.style.cursor='pointer';\" onmouseout=\"this.style.backgroundColor='';this.style.cursor='default';\" onclick=\"self.location='http://www.dmcpublisher.com/Store/book~card.php?id=$row[ncode]'\" valign=\"top\" width=\"901\"> <table border=\"0\" width=\"100%\" id=\"table3\"> <tr> <td width=\"779\"> <p style=\"line-height: 150%; text-indent: 10px; margin-top: 10px; margin-bottom: 0\" align=\"right\"> <font face=\"Tahoma\" size=\"2\">booktitle: <b>$row[bname] </b></font></p> <p style=\"line-height: 150%; text-indent: 10px; margin-top: 0; margin-bottom: 0\" align=\"right\"> <font face=\"Tahoma\" size=\"2\">Author: $row[author]</font></p> <p style=\"line-height: 150%; text-indent: 10px; margin-top: 0; margin-bottom: 0\" align=\"right\"> <font face=\"Tahoma\" size=\"2\">price: $row[pricelist] $</font></p> <p style=\"line-height: 150%; text-indent: 10px; margin-top: 0; margin-bottom: 0\" align=\"right\"> <font face=\"Tahoma\" size=\"2\">"; if ($row[stock] == 'Y') { echo "<img border=\"0\" src=\"../images/check.gif\" width=\"11\" height=\"11\"> available in stock</font> <p style=\"line-height: 150%; text-indent: 10px; margin-top: 0; margin-bottom: 0\" align=\"right\">"; } else { echo "<img border=\"0\" src=\"../images/unav.gif\" width=\"11\" height=\"11\"> out of stock</font> <p style=\"line-height: 150%; text-indent: 10px; margin-top: 0; margin-bottom: 0\" align=\"right\">"; } if ($row[versio] == '1' && $row[yea] == $tyear) { echo "<font face=\"Tahoma\" size=\"2\"> <img border=\"0\" src=\"../images/exclmark.gif\" width=\"11\" height=\"11\"> This book is new!</font> <span lang=\"en-us\"><font face=\"Tahoma\" size=\"2\"></font>"; } //if the user is logged in, display Add to Cart Button if($_SESSION['authen']) { $_SESSION['item'] = $row[isbn]; echo "</td> <td valign=\"top\" align=\"center\"> <p align=\"center\"><font face=\"Tahoma\" size=\"2\"> <a href=\"../shopping/cart.php\"><img border=\"0\" src=\"../images/cart.jpg\" width=\"69\" height=\"132\" alt=\"اضف هذا الكتاب للطلبية\"></a> "; } else { echo "</td> <td valign=\"top\" align=\"center\"> <p align=\"center\"><font face=\"Tahoma\" size=\"2\"> <img border=\"0\" src=\"../images/spacer.gif\" width=\"69\" height=\"132\">"; } echo "</font></td> </tr> </table> <span lang=\"en-us\"><font face=\"Tahoma\" size=\"2\"><div class=\"slider\"> <p align=\"center\" style=\"margin-top: -20px; margin-bottom: 0; text-indent:250px\"> <a href=\"#\" title=\"in\" class=\"s_in\">more about this book</a> </p> <div> <div align=\"center\"> <table border=\"0\" width=\"100%\" id=\"table2\" dir=\"rtl\"> <tr> <td align=\"right\"> <p style=\"text-indent: 15px; margin-top: 10px\"> <span lang=\"en-us\"><font face=\"Tahoma\" size=\"2\"><b>category:</b> $thiscat </font></span></td> <td align=\"right\"> <p style=\"margin-top: 10px\"> <font face=\"Tahoma\" size=\"2\"> <span lang=\"en-us\"> <b>pages no:</b> </span></font></span> <font face=\"Tahoma\" size=\"2\">$row[pages]</font></td> </tr> <span lang=\"en-us\"><span lang=\"en-us\"> <span lang=\"en-us\"><span lang=\"en-us\"> <tr> <td align=\"right\"> <p style=\"text-indent: 15px; line-height: 150%\"> <span lang=\"en-us\"> <font face=\"Tahoma\" size=\"2\"><b>Cover type:</b> $row[cover] </font></span></td> <td> <p dir=\"ltr\" align=\"right\" style=\"line-height: 150%\" > <span lang=\"en-us\"> <font size=\"2\">$row[size]</font></span><b><font size=\"2\">Size </font></b></td> </tr> <span lang=\"en-us\"><span lang=\"en-us\"> <span lang=\"en-us\"><span lang=\"en-us\"> <tr> <td align=\"right\"> <p style=\"text-indent: 15px; line-height: 150%\"> <span lang=\"en-us\"> <font face=\"Tahoma\" size=\"2\"><b>paper type:</b> $row[paper] </font></span></td> <td align=\"right\"> <p style=\"line-height: 150%\"> <font face=\"Tahoma\" size=\"2\"> <span lang=\"en-us\"> <b>الوزن:</b> </span></font></span></span> <font face=\"Tahoma\" size=\"2\">$row[weight] غ</font></td> </tr> <span lang=\"en-us\"><span lang=\"en-us\"> <tr> <td align=\"right\"> <p style=\"text-indent: 15px; line-height: 150%\"> <font face=\"Tahoma\" size=\"2\"> <span lang=\"en-us\"> <b>Year:</b> </span></font></span></span> <font face=\"Tahoma\" size=\"2\">$row[yea]</font></td> <td align=\"right\"> <p style=\"line-height: 150%\"> <font face=\"Tahoma\" size=\"2\"> <span lang=\"en-us\"> <b>ISBN:</b> </span>$row[isbn]</font></td> </tr> </table> </div> </div> </div> </font><span lang=\"en-us\"><span lang=\"en-us\"><span lang=\"en-us\"><span lang=\"en-us\"><span lang=\"en-us\"><span lang=\"en-us\"></TD> </TR></TBODY></TABLE></DIV></DIV>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689115 Share on other sites More sharing options...
trq Posted November 13, 2008 Share Posted November 13, 2008 Maybe, instead of overiding $_SESSION['item'] each iteration, store the isbn within an array within the $_SESSION array. eg; $_SESSION['item'][] = $row['isbn']; ps: On a side note, all associative array indexes need to be surrounded in quotes. eg; $row[isbn] should be... $row['isbn'] Quote Link to comment https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689224 Share on other sites More sharing options...
ballouta Posted November 13, 2008 Author Share Posted November 13, 2008 Hi, I replaced the $_SESSION['item'] with the session array as follow: <?php if($_SESSION['authen']) { $_SESSION['item'][] = $row['isbn']; echo $_SESSION['item'][]; // I want to print this just for checking echo "</td> <td valign=\"top\" align=\"center\"> <p align=\"center\"><font face=\"Tahoma\" size=\"2\"> <a href=\"../shopping/cart.php\"><img border=\"0\" src=\"../images/cart.jpg\" width=\"69\" height=\"132\" alt=\"add to cart\"></a> "; } else { echo "</td> <td valign=\"top\" align=\"center\"> <p align=\"center\"><font face=\"Tahoma\" size=\"2\"> <img border=\"0\" src=\"../images/spacer.gif\" width=\"69\" height=\"132\">"; } ?> why i can't print the session value in each iteration inorder to check it? I got this error: Fatal error: Cannot use [] for reading in /home/dcompany/public_html/Store/show~books.php on line 168 Quote Link to comment https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689267 Share on other sites More sharing options...
Mchl Posted November 13, 2008 Share Posted November 13, 2008 $_SESSION['item'][]; can aonly be used when adding to array ( [] means add as new item in array). To echo last value in array, you must know array's number of items (use count ) Quote Link to comment https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689277 Share on other sites More sharing options...
ballouta Posted November 13, 2008 Author Share Posted November 13, 2008 ok, great, thanks for the explanation. now the page show~books.php is finished, I will contine my question on the cart.php page. How do i know which book has the user clicked in the show~books page? in the old code, I used to write this line at the begining of my process code: if ($_SESSION['item']) { etc... } In this way, i used to know that there's a value in this session. and i make some queries. Now, after using the array, how do i detect the session value posted? Thank You all Quote Link to comment https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689281 Share on other sites More sharing options...
trq Posted November 13, 2008 Share Posted November 13, 2008 As an example, this will show all items. if ($_SESSION['item']) { foreach ($_SESSION['item'] as $item) { echo "$item\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689287 Share on other sites More sharing options...
premiso Posted November 13, 2008 Share Posted November 13, 2008 This may be stupid, but I fail to see session_start <?php session_start(); // needs to be at the top of any page you want to use sessions in if($_SESSION['authen']) { $_SESSION['item'][] = $row['isbn']; echo $_SESSION['item'][]; // I want to print this just for checking echo "</td> <td valign=\"top\" align=\"center\"> <p align=\"center\"><font face=\"Tahoma\" size=\"2\"> <a href=\"../shopping/cart.php\"><img border=\"0\" src=\"../images/cart.jpg\" width=\"69\" height=\"132\" alt=\"add to cart\"></a> "; } else { echo "</td> <td valign=\"top\" align=\"center\"> <p align=\"center\"><font face=\"Tahoma\" size=\"2\"> <img border=\"0\" src=\"../images/spacer.gif\" width=\"69\" height=\"132\">"; } ?> Again I may just be missing something but yea. Quote Link to comment https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689303 Share on other sites More sharing options...
ballouta Posted November 13, 2008 Author Share Posted November 13, 2008 thanks premiso yes i do always start my session at the most top of these pages. now the page show~books.php is finished, I will contine my question on the cart.php page. How do i know which book has the user clicked in the show~books page? in the old code, I used to write this line at the begining of my process code: if ($_SESSION['item']) { etc... } In this way, i used to know that there's a value in this session. and i make some queries. Now, after using the array, how do i detect the session value posted? Thank You all Quote Link to comment https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689374 Share on other sites More sharing options...
ballouta Posted November 17, 2008 Author Share Posted November 17, 2008 Hi after assigning each row a session array value, how I will know which $_SESSION['item'] did the user choose? thank you Quote Link to comment https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-691970 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.