Jump to content

Using php sessions with while loop to display specific content in another page.


dangar
Go to solution Solved by requinix,

Recommended Posts

I have a products page with product images fetched from database using sql and php. I wanted to use php sessions, so when users click on a particular product image, they are directed to the poduct description page for the particular product. With the code below, I can view the products listed in the product page. However, if I click on any product image for their description it opens up the last product's image every time.! Any help would be appreciated. Thanks.

<?php
include ("connection.php");
session_start();
				
				$query = mysqli_query($conn,"select * from products");
				$row = mysqli_fetch_array($query);
				while($row = mysqli_fetch_array($query))
				
				{
				$_SESSION['image'] = $row['PRODUCT_IMAGE'];
				$_SESSION['price'] = $row['PRODUCT_PRICE'];
				$_SESSION['name'] = $row['PRODUCT_NAME'];
				$_SESSION['desc'] = $row['PRODUCT_DESCRIPTION'];
				$_SESSION['outcome'] = $row['PRODUCT_OUTCOME'];
				$_SESSION['id'] = $row['PRODUCT_ID'];
				
				
				
			?>
			
			<div class = "product-image-wrapper">
					<a href = "Productdetails.php">
					<?php echo '<img src = "data:image/jpeg;base64,'.base64_encode($_SESSION['image']).'" alt = "" width = "250px" height ="300px"/>'; ?>
					</a>
					<div class = "product-price">$ <?php echo $_SESSION['price']; ?> </div>
					<div class = "product-name"> <?php echo $_SESSION['name']; ?> </div>	
					<div class = "product-add">
						<form  class = "form1" method = "post" action = ""> <br>	
									QTY: <input class = "qty" type = "text" name = "qty" value = "1" />
									<input type = "hidden" name = "item_number" value = "<?php echo $row["PRODUCT_ID"];?>" />
									<input type = "hidden" name = "price" value = "<?php echo $row["PRODUCT_PRICE"]; ?>" />
									<input type = "hidden" name = "title" value = "<?php echo $row["PRODUCT_NAME"]; ?>" />
									<button class = "btnprimary" type = "submit"> Add To Cart </button>
						</form>
					</div>
					
			</div>
			<?php
				}
			?>



//In Productdetails.php page, this is how I have tried to use session.

<?php 
session_start();
echo '<img src = "data:image/jpeg;base64,'.base64_encode($_SESSION['image']).'" alt = "" width = "250px" height ="300px"/>'; ?>
			
Link to comment
Share on other sites

  • Solution

Don't use a session for this. Sessions are for storing data that lasts while a user visits your website and are not appropriate for passing temporary bits of data between a couple pages.

 

Just use normal variables for displaying the product information, like the $row you already have. Use a query string to pass the product ID to productdetails.php.

  • Like 1
Link to comment
Share on other sites

If this is your first project and you can't envision the code to accomplish it, perhaps you need to back up and start 'small'.  

 

Think about NOT passing the data itself but rather pass the id (ie, key) of the record that has that data.  Let the receiving script then do a query with that passed id/key, grabbing the data fields needed and displaying them in your desired format.

 

See how simple it can be when you think it out?

  • Like 1
Link to comment
Share on other sites

Basically, you would have something like this. Note that I changed the SESSION variables to use $row.

<?php
echo '<a href = "Productdetails.php?id=' . $row['PRODUCT_ID'] . '"><img src = "data:image/jpeg;base64,'.base64_encode($row['PRODUCT_IMAGE']).'" alt = "" width = "250px" height ="300px"/></a>';
?>
 
You would then be able to pull the image record using $_GET['PRODUCT_ID'] in Productdetails.php.
Edited by cyberRobot
fixed [code][/code] tag
  • Like 2
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.