Jump to content

Session Problem


ballouta

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/132488-session-problem/
Share on other sites

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>";
                    
                    }

?>

Link to comment
https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689115
Share on other sites

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']

Link to comment
https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689224
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689267
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689281
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689303
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/132488-session-problem/#findComment-689374
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.