Reputation:
I have a {$obj->product_stock} which contains a numeric value - the stock of the product.
How can I convert that {$obj->product_stock} to a SESSION? Here is what I tried...
$_SESSION['product_stock'] = {$obj->product_stock};
Why does it error and how can I get round this?
<?php
session_start();
include_once("config.php");
// Current URL of the Page. cart_update.php redirects back to this URL
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="keywords" content="">
<title>Product Page</title>
<?php include("includes/header-includes.php"); ?>
</head>
<body>
<?php include("includes/navigation.php"); ?>
<?php include("includes/basket-warning.php"); ?>
<!-- Products List Start -->
<?php
$ProductID = intval($_REQUEST['ProductID']);
$results = $mysqli->query("SELECT * FROM products where ProductID = '$ProductID';");
if($results) {
$_SESSION['product_stock'] = $obj->product_stock;
echo "STOCK: ". $_SESSION['product_stock'] ."";
$products_item = '<ul class="products">';
// Fetch results, set as object and output HTML
while($obj = $results->fetch_object()) {
$products_item .= <<< EOT
<li class="product">
<form method="post" action="cart_update.php">
<div class="product-content">
<h3>{$obj->ProductName} <strong>(STOCK: {$obj->product_stock}</strong>)</h3>
<div class="product-image" style="background: url({$obj->TypeImage}) no-repeat; background-size: 100%; min-height: 551.328px;">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
<div class="item active"><img src={$obj->TypeImage} class="img-responsive"></div>
<div class="item"><img src={$obj->AlternativeImagesOne} class="img-responsive"></div>
<div class="item"><img src={$obj->AlternativeImagesTwo} class="img-responsive"></div>
<div class="item"><img src={$obj->AlternativeImagesThree} class="img-responsive"></div>
</div>
</div>
</div>
<div class="product-content-cont">
<h4>{$obj->ProductName} - Overview</h4>
<p>{$obj->ProductDescription}</p>
</div>
</div>
<div class="like-what-you-see">
<fieldset>
<label>
<span>Price</span>
{$currency}{$obj->Price}
</label>
<label>
<input type="" size="1" maxlength="2" name="product_qty" value="1" class="form-control" />
</label>
</fieldset>
<input type="hidden" name="ProductCode" value="{$obj->ProductCode}" />
<input type="hidden" name="type" value="add" />
<input type="hidden" name="return_url" value="{$current_url}" />
<button type="submit" class="btn btn-warning">Add</button>
</div>
</form>
</li>
EOT;
}
$products_item .= '</ul>';
echo $products_item;
}
?>
<!-- Products List End -->
<?php include("includes/footer.php"); ?>
</body>
</html>
Upvotes: 1
Views: 71
Reputation: 3157
Your code:
$_SESSION['product_stock'] = $obj->product_stock;
echo "STOCK: ". $_SESSION['product_stock'] ."";
is outside the while loop. Therefore $obj
is not yet defined.
Move the code above into the while loop like this
$products_item = '<ul class="products">';
// Fetch results, set as object and output HTML
while($obj = $results->fetch_object()) {
$_SESSION['product_stock'] = $obj->product_stock;
echo "STOCK: ". $_SESSION['product_stock'] ."";
....
Upvotes: 1