Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I was wondering what will be the best way to show the number of items and the total price for those item which are already in cart(shopping basket).
The header.php page is on all my pages. so whatever page the user goes to they should be able to still see what the number of item in page and total price.
For an example if I had about 7 items my cart and let's say those item cost about $600.78. How cab I count those item up and show the number and total price in the div below.
example
Let's say u go on Nike website and buy 3 pairs of Nike shoes. If u look on top of the page it tells u how many items u have in your basket and the total price for all those items.
My header.php
<div class="cart">
<div class="shbag">
<a href ="cart.php">
</div>
<ul>
<li class="item"><a>
Item
<span id="items">0</span>
</a></li>
<li class="cart_price"><a href=""
title="Cart">
Total £
<span id="cart_total">0.00</span></a></li>
<!--cart page -->
<li><a href="cart.php">
<span class="check_bdr"
title="Checkout">View bag</span></a></li>
</ul>
</div>
Thanks in Advance
Here is my approach:
header.php
<?php require_once('minicart.php'); ?>
<div class="cart">
<div class="shbag">
<a href ="cart.php">
</div>
<ul>
<li class="item">
Item <span id="items"><?php echo cart_item_count() ?></span>
</li>
<li class="cart_price">
<a href="" title="Cart">Total £
<span id="cart_total"><?php echo cart_total() ?></span>
</a>
</li>
<!--cart page -->
<li>
<a href="cart.php">
<span class="check_bdr" title="Checkout">View bag</span>
</a>
</li>
</ul>
</div>
minicart.php
<?php
function cart_item_count()
{
return count($_SESSION['cart_array'])
}
function cart_total()
{
// calculate your cart total here
return $cart_total;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I have got multiple php variables that I want to display in html <ul list. The variables being displayed but not how I want it to be. It seems like one <li is inside of <ul correctly but, the other one is outside of <ul in it's own block and it's own <li even when I am displaying all of the variables inside of single <ul. Also the styling is only being applied to only the first <li element and not to the others.
Here is my html code
<div>
<ul class="product-info-list">
<li> <?php echo $dimensions; ?> </li>
<li> <?php echo $materials; ?> </li>
<p> <?php echo $description; ?> </p>
</ul>
</div>
The source code from page source is
<div class="product-info-details">
<div>
<ul class="product-info-list">
<li> 18" x 10" x 6".</li>
</ul></li>
<li> Burlap,Nylon,Polyester </li>
<p> <p>With the Fusion Backpack strapped on, every trek is an adventure - even a bus ride to work </p>
</ul>
</div>
and the output
The html structure that is getting created after doing what I am doing is,
The page element cant be where you have it. The <p> can not be between the </li> and </ul>. I can not find a suitable authoritative reference right now, but I have made the same mistake myself.
So I have shifted it inside the <li>..</li>
Try this.
<div>
<ul class="product-info-list">
<li> <?php echo $dimensions; ?> </li>
<li> <?php echo $materials; ?>
<p> <?php echo $description; ?> </p></li>
</ul>
</div>
Or maybe you intended this. I have changed the <p> to a list element
<div>
<ul class="product-info-list">
<li> <?php echo $dimensions; ?> </li>
<li> <?php echo $materials; ?> </li>
<li> <?php echo $description; ?> </li>
</ul>
</div>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am creating a blog page. I want to create a small highlights section on the home page.
I created the highlight part from the admin panel, when I make changes, it appears from the database, but I cannot pull the information on the front side. Only the name of the blog will appear in the highlights. Where is the problem?
<div class="card text-center my-5">
<div class="card-header baslik-blog">
En Çok Okunan Blog Yazıları
</div>
<ul class="list-group list-group-flush">
<?php
$blogsor=$db->prepare("SELECT * FROM blog where blog_onecikar=:blog_onecikar");
$blogsor->execute(array(
'blog_onecikar' =>1,
));
while($blogcek=$blogsor->fetch(PDO::FETCH_ASSOC)){
?>
<a href="blog-page" class="blog-a">
<li class="list-group-item"><i class="fas fa-angle-double-right float-start"></i>
<?php $blogcek['blog_ad'] ?>
</li>
</a>
<?php } ?>
Tüm Blog Yazıları
</ul>
</div>
change
<?php $blogcek['blog_ad'] ?>
to
<?php echo $blogcek['blog_ad'] ?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am starting a cart session on my cart page but cart dropdown is not updating with a count until I refresh the page. I just want to update the div without reloading.
I am using foreach but it doesn't work. What should I do now.
<?php
session_start();
require_once("product.php");
?>
<li class="header-cart dropdown default-dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" aria-expanded="true">
<div class="header-btns-icon">
<i class="fa fa-shopping-cart"></i>
<?php
if(isset($_SESSION['cart'])){
?>
<span class="qty"><?php echo count($_SESSION['cart'])?></span>
<?php
}else{
?>
<span class="qty">0</span>
<?php
}
?>
</div>
<strong class="text-uppercase">My Cart:</strong>
<br>
<span>Shop Now</span>
</a>
<div class="custom-menu">
<div id="shopping-cart">
<div class="shopping-cart-list">
<?php
#$cart = unserialize(serialize($_SESSION['cart']));
$total = 0;
if($cart){
foreach($cart as $k=>$v)
{
$total += $v->price * $v->quantity;
?>
<div class="product product-widget">
<div class="product-thumb">
<img src="<?php echo $v->img;?>" height="50" width="60" alt="">
</div>
<div class="product-body">
<h3 class="product-price"><?php echo $v->price;?> <span class="qty">x1</span></h3>
<h2 class="product-name"><?php echo $v->name;?></h2>
</div>
<button class="cancel-btn" style="margin-right:8px;"><i class="fa fa-trash"></i></button>
<?php }
?>
<div class="shopping-cart-btns">
<button class="main-btn">View Cart</button>
<button class="primary-btn" style="margin-left:15px;">Checkout <i class="fa fa-arrow-circle-right"></i></button>
</div>
<?php
}
else{
echo '<p style="color: red;">No Product is added in your cart</p>';
}
?>
</div>
</div>
</li>
I want to update the cart div when I click on Add to cart.
As you have not provided enough code to work with, I will post the concept and hope you will be able to do it, also comment if you have any more doubts I'm always open to help
So firstly you need to write a function that asynchronously fetches
the data - Which means it fetches the data without having the page to
reload.
So you need to write the PHP code in a PHP file instead of in the HTML page, for example,
cart.php or where ever you have written your query to fetch the count
of the cart from database.
Once you have done that then you need to write an AJAX call to the
button of Add to Cart, So when you click on add to cart, it triggers
the ajax request and fetches the data from the backend which is
passed from your cart.php
for example , write this code inside your add to cart button
fetch("cart.php")
.then(res=>res.json)
.then(res=>{
//You will get the count here and just append it inside the tag where your count is displayed
//For example :
count.innerHTML = res
})
Don't forget to json_encode($count); in the PHP file so that you can receive the data in JSON format
This might seem easy enough for the lovely experts here at SO :) but I can't find a decent answer/question about this on SO or google. Pls help. :)
How do i display a current value of a php variable in realtime via AJAX/js/jQuery
Basically im trying to do an E-commerce site and I am storing the shopping cart contents using a session and displaying it with an echo statement counting how many contents is currently in the basket, however when they are on a certain product and tried adding it in the cart, the value of the cart doesn't echo the added item until there is a page refresh, so i need something like an AJAX to display the updated value of the session when an item is added without having a page refresh.
Thank you so much.
Here is my code snippet for the cart as a sample:
// Assign shopping cart ($_SESSION['cart']) into variable
$basket = count($_SESSION['cart']);
And for displaying:
<!-- Display contents of the shopping cart -->
<ul id="basket" class="clearfix">
<li class="top">
<a href="checkout.php" class="top_link">
<span>Items: <? echo $basket ?></span>
</a>
</li>
</ul>
Thank you so much.. kindly provide a sample code on how to achieve this (either js or jQuery) and would help as well for best practices in dealing with this type of scenario.
First solution (based on your request - BAD IDEA)
get-count-items.php
<?php
session_start();
die(count($_SESSION['cart']));
?>
Javascript
<script>
setInterval(function(){
$.post('get-count-items.php', {}, function (total_items){
$('#total-items').html(total_items);
});
}, '1000');
</script>
HTML
<ul id="basket" class="clearfix">
<li class="top">
<a href="checkout.php" class="top_link">
<span>Items: <span id="total-items"><? echo $basket ?></span></span>
</a>
</li>
</ul>
This javascript will make a request to server at each 1 second.
Second solution (event on button "Add to cart")
add-to-cart.php
<?php
session_start();
// code for add to cart...
die(count($_SESSION['cart']));
?>
Javascript
<script>
$('.add-to-cart').click(function(){
$.post('add-to-cart.php', {}, function (total_items){
$('#total-items').html(items);
});
return false;
});
</script>
HTML
<ul id="basket" class="clearfix">
<li class="top">
<a href="checkout.php" class="top_link">
<span>Items: <span id="total-items"><? echo $basket ?></span></span>
</a>
</li>
</ul>
<a href="" class="add-to-cart">Add to cart</>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Excerpt changing according to post title. For removing the blank space.
I want to adjust (increase or decrease) the excerpt depending on the title of the post. For example (http://www.webdesignerdepot.com/) here in this site the excerpt is adjusted according to the post title. When the post title is large, the excerpt is decreased and vice versa. This excerpt is adjusted according to the letters in the post title.
Suppose there is no excerpt added. I want every thing from start. I found a post (http://www.rlmseo.com/blog/changing-excerpt-size-dynamically/) which is a correct answer to my question, but the code is not working - it is old, not updated, and was written in 2008.
My index.php snippet
<div class="mcr-post-area mcr-handheld">
<div class="mcr-item-warp">
<a class="mcr-title2-link" href="<?php the_permalink() ?>"><h2 class="mcr-title2-header"><?php the_title(); ?></h2></a>
<div class="mcr-below-title">
<span class="mcr-author">
<i class="icon-user"></i><span> <?php the_author(); ?> </span>
</span>
<span class="mcr-date">
<i class="icon-calendar-empty"></i> <?php the_time('M j, Y ') ?>
</span>
<span class="mcr-comment">
<i class="icon-comments"></i>
<?php comments_number( 'no responses', 'one response', '% responses' ); ?>
</span>
<div style="clear:both;"></div>
</div>
</div><!--mcr-item-warp-->
<div class="mcr-post-detial" style="height: auto;">
<div style="margin: 0px; padding: 0px; border: 0px;">
<div class="entry-home">
<?php the_excerpt(strlen(the_title())); ?>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php
// Dynamically resize excerpt according to title length
$rem_len = ""; //clear variable
$title_len = strlen(get_the_title()); //get length of title
if($title_len <= 35){
$rem_len=188; //calc space remaining for excerpt
}elseif($title_len <= 70){
$rem_len=146;
}elseif($title_len <= 105){
$rem_len=104;
}elseif($title_len <= 140){
$rem_len=62;
}
$trunc_ex = substr(get_the_excerpt(), 0, $rem_len); //truncate excerpt to fit remaining space
if(strlen($trunc_ex) < strlen($post->post_excerpt)) $trunc_ex = $trunc_ex . " [...]";
echo "<p>" . $trunc_ex . "</p>"; //display excerpt
?>