PHP Session data gets lost after page refresh - php

I am working on this product page. Basically it shows a product by ID and gives you the option to add it to your cart. My problem is that the data stored in $_SESSION gets lost every time I refresh the page.
<?php
include 'scripts\init.php'; // contains session_start(); and the functions
if(!IsProductIdSafeAndExisting())
{
session_write_close();
header("Location: shop.php");
die();
}
if(isset($_POST['quantity'])) // adds current item to cart but gets lost after refresh
AddItemToCart($_GET["id"],$_POST['quantity']);
$id = $_GET["id"];
$name = GetProductField($id,"name");
$image = GetProductField($id,"image");
$price = GetProductField($id,"price");
$stock = GetProductField($id,"stock");
$details = GetProductField($id,"details");
$total_products = GetTotalProducts();
$total_price = GetTotalProductsPrice();
LoadHeaderByTitle($name." | Magazin Vinuri");
?>
<body>
<div id="page">
<?php LoadNavigationBy(""); ?>
<div id="body">
<div class="header">
<div>
<h1><?php echo $name; ?></h1>
</div>
</div>
<div class="singlepost">
<div class="featured">
<img src="images/<?php echo $image; ?>" alt="">
<h1><?php echo $name.' - '.$price.' lei'; ?></h1>
<span>Mai sunt <?php echo '<strong>'.$stock.'</strong>'; ?> bucati ramase.</span>
<p><?php echo $details; ?></p>
<div class="additem">
<center>
<form method="POST" action="product.php?id=<?php echo $id; ?>">
<input type="text" name="quantity" value="Cantitate" onblur="this.value=!this.value?'Cantitate':this.value;" onfocus="this.select()" onclick="this.value='';">
<input type="submit" value="Adauga" id="submit">
</form>
</center>
</div>
</div>
<div class="sidebar">
<h1>Cosul tau</h1>
<img src="images/cart.png" alt="">
<h2><?php echo $total_price; ?> lei</h2><br>
<p>Momentan aveti <strong><?php echo $total_products; ?></strong> produse in cos. Pentru a edita lista de produse dati click pe butonul de mai jos.</p>
vezi cumparaturi
</div>
</div>
</div>
<?php include 'scripts\overall\foot.php' ?>
init.php
<?php
session_start();
require 'database\connect.php';
require 'functions\general.php';
require 'functions\engine.php';
?>

the problem was that I stored product ids like this $_SESSION[10]=something; which was wrong because it the same as $_SESSION['10']=something; so i changed it to $_SESSION['id_10']=something; and now it works

Related

Trouble closing if statement and while loop in PHP

I'm trying to display some html inside a php "if statement" and during a "While" Loop.
I keep getting unexpected end of file error and I know there are two braces missing but I can't see where to close the while loop properly.
I'm still new to coding so please be gentle... :-)
tried php syntax checker and other similar posts here
<?php include 'includes/header.php';?>
<!-- Navigation -->
<?php include 'includes/navbar.php';?>
<?php
if (isset($_GET['post'])) {
$post = $_GET['post'];
if (!is_numeric($post)) {
header("location:index.php");
}
}
else {
header('location: index.php');
}
$query = "SELECT * FROM posts WHERE id=$post";
$run_query = mysqli_query($conn, $query) or die(mysqli_error($conn)) ;
if (mysqli_num_rows($run_query) > 0 ) {
while ($row = mysqli_fetch_array($run_query)) {
$post_title = $row['title'];
$post_id = $row['id'];
$post_author = $row['author'];
$post_date = $row['postdate'];
$post_image = $row['image'];
$post_content = $row['content'];
$post_tags = $row['tag'];
$post_status = $row['status'];
$img_ext = explode(".", $post_image);
?>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Post Content Column -->
<div class="col-lg-8">
<!--First Post -->
<hr>
<p><h2><?php echo $post_title; ?></h2></p>
<p><h3>by <?php echo $post_author; ?></h3></p>
<p><span class="glyphicon glyphicon-time"></span>Posted on <?php echo $post_date; ?></p>
<hr>
<?php if ($img_ext=="mp4"): ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php else : ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>" alt="900 * 300">
<hr>
<p><?php echo $post_content; ?></p>
</div>
<!-- Blog Sidebar Widgets Column -->
<div class="col-md-4">
<?php include 'includes/sidebar.php'; ?>
</div>
</div>
<!-- /.row -->
<hr>
<!-- Footer -->
<?php include 'includes/footer.php';?>
</div>
<!-- /.container -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Try below:
<?php include 'includes/header.php'; ?>
<!-- Navigation -->
<?php include 'includes/navbar.php'; ?>
<?php
if (isset($_GET['post'])) {
$post = $_GET['post'];
if (!is_numeric($post)) {
header("location:index.php");
}
} else {
header('location: index.php');
}
$query = "SELECT * FROM posts WHERE id=$post";
$run_query = mysqli_query($conn, $query) or die(mysqli_error($conn));
if (mysqli_num_rows($run_query) > 0) {
while ($row = mysqli_fetch_array($run_query)) {
$post_title = $row['title'];
$post_id = $row['id'];
$post_author = $row['author'];
$post_date = $row['postdate'];
$post_image = $row['image'];
$post_content = $row['content'];
$post_tags = $row['tag'];
$post_status = $row['status'];
$img_ext = explode(".", $post_image);
?>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Post Content Column -->
<div class="col-lg-8">
<!--First Post -->
<hr>
<p>
<h2><?php echo $post_title; ?></h2></p>
<p>
<h3>by <?php echo $post_author; ?></h3></p>
<p><span class="glyphicon glyphicon-time"></span>Posted on <?php echo $post_date; ?></p>
<hr>
<?php if ($img_ext == "mp4"): ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php else : ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>"
alt="900 * 300">
<?php endif; ?>
<hr>
<p><?php echo $post_content; ?></p>
</div>
<!-- Blog Sidebar Widgets Column -->
<div class="col-md-4">
<?php include 'includes/sidebar.php'; ?>
</div>
</div>
<!-- /.row -->
<hr>
<!-- Footer -->
<?php include 'includes/footer.php'; ?>
</div>
<!-- /.container -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<?php
} // End while
} // End if
Note: your code was missing <?php endif; ?> and two braces at the end.
PHP is syntatically more similar to C than to Python or VisualBasic. Indentation means nothing here. Code blocks need to be explicitly open and closed.
There are 2 types to quote the if-else blocks but you did neither of those.
You can either:
use brackets if { ... } else { ... }
Example:
<?php if ($img_ext=="mp4") { ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php } else { ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>" alt="900 * 300">
<hr>
<p><?php echo $post_content; ?></p>
<?php } ?>
use colon and keyworkds if, endif
Example,
<?php if ($img_ext=="mp4"): ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php else: ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>" alt="900 * 300">
<hr>
<p><?php echo $post_content; ?></p>
<?php endif ?>
Just chose which one to go for.
Similarly, you have to close your while-loop with either bracket } or endwhile, depends of your syntax of choice.
you opened the if statement and forgot to close it also while is not closed closed it after the container gets closed

The right way to store data into database in a wordpress plugin

I created this plugin for a Wordpress site, in the admin page i've insert a modal that can change a specific #color in css only if it's pressed the specific color button.
With the code below, i need to reload two times the admin page for see the change, so i think it's not the correct way to insert - update values displayed. This code it's placed on the same file where modal it's declare.
Please, can you show me the wordpress right way to update or insert data into database then show the change in admin page?
Thanks.
function recensility_system_action_color_fest($id , $title, $footer )
{
global $wpdb;
$colors = $wpdb->get_results('SELECT * FROM '.$wpdb->prefix.'recensility_color_fest', ARRAY_A);
?>
<!-- The Modal -->
<div id="<?php echo $id; ?>" class="recensility-modal">
<!-- Modal content -->
<div id="<?php echo $id.'-insert'; ?>" class="recensility-modal-content">
<div class="recensility-modal-header">
<span id="<?php echo $id.'-close'; ?>" class="recensility-modal-close" onclick="close_modal('<?php echo $id; ?>')">×</span>
<h2 class="recensility-modal-title"><i class="fas fa-swatchbook"></i><?php echo $title; ?></h2>
</div>
<div class="recensility-modal-body">
<center>
<h2>Clicca sul colore da impostare</h2>
<div class="recensility-color-fest-chooser">
<form id="recensility-color-fest-form" method="post">
<div class="recensility-color-fest-chooser-body">
<div class="recensility-color-fest-chooser-items">
<?php
foreach ($colors as $color){
?>
<div class="dot-container <?php echo ($color['is_active']=='true') ? 'dot-active' : 'dot-inactive'; ?>">
<?php
echo
($color['is_active']=='true')
?
'<p class="dot-active-text">Attivo</p>'
:
'<button id="recensility-color-fest-form-submit" name="recensility-color-fest-form-submit" class="recensility-color-fest-form-button" value="'. $color['value'].'" type="submit" form="recensility-color-fest-form">';
?>
<span class="dot" style="background-color:<?php echo $color['value'] ?>"></span>
<?php echo ($color['is_active']=='false') ? '</button>' : ''; ?>
<p><?php echo $color['name'] ?></p>
</div>
<?php
}
?>
</div>
</div>
</form>
</div>
</center>
</div>
<div class="recensility-modal-footer">
<h3 class="recensility-modal-footer-content"><?php echo $footer; ?></h3>
</div>
<?php
print_r($_POST);
if (!empty($_POST['recensility-color-fest-form-submit'])){
$activeColor = ($wpdb->get_results('SELECT `value` FROM '.$wpdb->prefix.'recensility_color_fest'.' WHERE `is_active` = "true"', ARRAY_A)[0])['value'];
$wpdb->update
(
$wpdb->prefix.'recensility_color_fest',
array('is_active' => 'false'),
array('is_active' => 'true')
);
recensility_color_fest_apply($activeColor, $_POST['recensility-color-fest-form-submit']);
$wpdb->update
(
$wpdb->prefix.'recensility_color_fest',
array('is_active' => 'true'),
array('value' => $_POST['recensility-color-fest-form-submit'])
);
}
?>
</div>
</div>
<?php
}

Magento - related product color swatches

I'm working with Magento EE v1.14 and i'm looking for a solution for when a user is viewing a product page to then drop swatches of related product colors if they are out of stock.
Screenshot: Highlighted out of stock related product color
Screenshot of HTML
PHP + HTML code:
<?php
$_base_product = $this->getProduct();
$base_product = Mage::getModel('catalog/product')->load($_base_product->getId());
$base_product_id = $base_product->getId();
$base_name = $base_product->getName();
$base_url = Mage::getBaseUrl();
$product_colors = Mage::getModel('catalog/product')->getCollection();
$product_colors->addAttributeToFilter('status',1); // 1 or 2
$product_colors->addAttributeToFilter('visibility',4); // 1.2.3.4
$product_colors->addAttributeToFilter('name', array('eq' => $base_name));
$product_colors->addAttributeToFilter('sku', array('neq' => $base_product->getSku()));
$product_colors_ids = $product_colors->getAllIds(); // get all products from the category
sort($product_colors_ids);
?>
<?php if(count($product_colors_ids) > 0) : ?>
<div id="product-color-options-wrapper">
<div id="product-color-options-container">
<label><?php echo $this->__('Color') ?> / <span style="font-weight: normal;"><?php echo $base_product->getAttributeText('color'); ?></span></label>
<div id="color-options-wrapper">
<?php $_swatch_img = $base_product->getMediaGalleryImages(false)->getItemByColumnValue('label', 'swatch') ?>
<?php if($_swatch_img) : ?>
<div class="current-product-wash-wrapper wash-wrapper">
<div class="current-product-wash-container wash-container">
<img src="<?php echo $this->helper('catalog/image')->init($base_product, 'small_image', $_swatch_img->getFile())->resize(33,30) ?>" alt="" title="<?php echo $base_product->getAttributeText('color') ?>" />
</div>
</div>
<?php else : ?>
<!-- <span><?php echo $base_product->getColor() ?></span> -->
<?php endif ?>
<?php foreach($product_colors_ids as $prod_id) : ?>
<?php $_sister_product = Mage::getModel('catalog/product')->load($prod_id) ?>
<?php
$_sister_prod_imgs = $_sister_product->getMediaGallery('images');
foreach($_sister_prod_imgs as $_sister_prod_img):
if($_sister_prod_img['label'] == 'swatch'):
$_swatch_img = $_sister_prod_img['file'];
endif;
endforeach;
?>
<?php if($_swatch_img): ?>
<div class="sister-product-wrapper wash-wrapper">
<div class="sister-product-container wash-container">
<a href="<?php echo $base_url ?><?php echo $_sister_product->getUrlKey() ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_sister_product, 'small_image', $_swatch_img)->resize(33,30); ?>" alt="" title="<?php echo $_sister_product->getAttributeText('color') ?>">
</a>
</div>
</div>
<?php endif; ?>
<?php endforeach ?>
<div class="clear"></div>
</div>
</div>
</div>
<?php endif ?>
Any help would be appreciated! :D
**Solution:**Added an if statement to check for stock availability using the isAvailable() function, shown in screenshot.
Link to screenshot: https://gyazo.com/abf07ba0373877836571858ee129cc22

PHP: link a Block

quick question, i want to link the BLOCK1 to another page. Is it possible? do anyone got a solution? i have tried abit and my brain is going mad T_T. there are 5 other blocks that i which to link to other pages aswell.
this is the code below:
<div class="midrow_block axn_block1">
<div class="mid_block_content">
<!--BLOCK1 IMAGE-->
<?php if(!empty($optimizer['block1_image']['url']) && empty($optimizer['block1_img_bg'])){ ?>
<div class="block_img"><img src="<?php echo $optimizer['block1_image']['url']; ?>" width="<?php echo $optimizer['block1_image']['width']; ?>" height="<?php echo $optimizer['block1_image']['height']; ?>" /></div>
<?php } ?>
<div class="block_content">
<h3>
<?php echo do_shortcode( $optimizer['block1_text_id']); ?>
</h3>
<?php echo do_shortcode($optimizer['block1_textarea_id']); ?>
</div>
</div>
</div>
</div>
<?php } ?>
<!--BLOCK1 END-->
Just put a <a>-Tag arount the block you want to link:
<a href="'your link here'"> <!-- start link here -->
<div class="midrow_block axn_block1">
<div class="mid_block_content">
<!--BLOCK1 IMAGE-->
<?php if(!empty($optimizer['block1_image']['url']) && empty($optimizer['block1_img_bg'])){ ?>
<div class="block_img"><img src="<?php echo $optimizer['block1_image']['url']; ?>" width="<?php echo $optimizer['block1_image']['width']; ?>" height="<?php echo $optimizer['block1_image']['height']; ?>" /></div>
<?php } ?>
<div class="block_content">
<h3>
<?php echo do_shortcode( $optimizer['block1_text_id']); ?>
</h3>
<?php echo do_shortcode($optimizer['block1_textarea_id']); ?>
</div>
</div>
</div>
</a> <!-- end link here -->
Or put it around any other part you want to have the link on. It's just basic HTML and has nothing to do with PHP.
here is the fullcode of the block. i missed to copy the top of it:
<?php if ((!empty ($optimizer['block1_text_id'])) || (!empty ($optimizer['block1_textarea_id'])) ) { ?>
<div class="midrow_block axn_block1">
<div class="mid_block_content">
<!--BLOCK1 IMAGE-->
<?php if(!empty($optimizer['block1_image']['url']) && empty($optimizer['block1_img_bg'])){ ?>
<div class="block_img"><img src="<?php echo $optimizer['block1_image']['url']; ?>" width="<?php echo $optimizer['block1_image']['width']; ?>" height="<?php echo $optimizer['block1_image']['height']; ?>" /></div>
<?php } ?>
<div class="block_content">
<h3>
<?php echo do_shortcode( $optimizer['block1_text_id']); ?>
</h3>
<?php echo do_shortcode($optimizer['block1_textarea_id']); ?>
</div>
</div>
</div>
You can use with this JS
<div class="midrow_block axn_block1" onclick="location.href='url'">content</div>
jQuery:
$("div").click(function(){
window.location=$(this).find("a").attr("href"); return false;
});

Using previous page array values in php into another page

I am creating a online webstore. But i am stuck in calling out the selected value to be used on the next page.
My Product summary page code,
<?php
include 'productData.php';
if (isset($_GET['cat']) && isset($productArr[$_GET['cat']])) {
$selected = $productArr[$_GET['cat']];
}
foreach ($selected as $productName => $productDescriptionArr):
?>
<div>
<div>
<a class="thumbnail" href="productDetailPage.php?cat=<?= $selected; ?>&code=<?= $productName; ?>">
<img src="<?php echo "img/" . $productDescriptionArr['image'] ?>" class="img-rounded" width="250" height="220">
<div><h3><?php echo $productName; ?></h3></div>
</a>
</div>
</div>
<?php endforeach; ?>
</body>
</html>
My Product Detail Page,
<html>
<body>
<?php
include 'productData.php';
if (isset($_GET['cat']) && isset($productArr[$_GET['cat']])) {
$selected = $productArr[$_GET['cat']];
if (isset($_GET['code']) && isset($productName[$_GET['code']])){
$name = $productName[$_GET['code']];
}
}
foreach ($selected as $name => $productDescriptionArr):
foreach ($productDescriptionArr as $key => $value) :
?>
<div>
<div>
<img src="<?php echo "img/" . $productDescriptionArr['image'] ?>" class="img-rounded" width="250" height="220">
<div class="title"><h3><?php echo $name; ?></h3></div>
</a>
</div>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
</body>
</html>
in sort, select "PT", moves to page with all "PT" product, and end at the specific selected "PT" product in the product detail page.
I have edited your code for product detail page.. As you've not defined $productName variable anywhere in page. I've customized your code and hope it'll be good for you..
include 'productData.php';
<?php
if (isset($_GET['cat']) && isset($productArr[$_GET['cat']])) {
$productNames = $productArr[$_GET['cat']]; //Added $productNames here to asign your current product category
}
foreach($productNames as $name=>$details):
if (isset($_GET['code']) && $name==$_GET['code']){
//$name is product name.
//print_r($details);
// now you can use $details['images'], $details['dimension'], $details['price'] as you want...
?>
<div class="category">
<div class="col-md-3">
<a class="thumbnail">
<img src="<?php echo "img/" . $details['image'] ?>" class="img-rounded" width="250" height="220">
<div class="title"><h3><?php echo $name; ?></h3></div>
</a>
</div>
</div>
<?php } ?>
<?php endforeach; ?>

Categories