PHP counter display

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

PHP counter display

Post by terrenuit »

Hi,
I want to create a quantity counter, but it doesn't work,
the error is in header.php line 3 ($res=$_POST['qty'];): Undefined index: qty in C:\wampserver32\www\sale\head.php on line 3
anyone can help me ?
index.php

Code: Select all

<?php      
 <?php require_once("header.php")  ?>
       $qty=0;
       $totalprice=0;
       foreach($listproduit as $produit) {  
	   $subqty=$produit['quantity'];
       $subtotal=$produit['quantity']*$produit['price'];
       $totalprice+=$subtotal;
	   $qty+=$subqty;	   
       ?> 
       <form action="header.php" method="post">  
        <input type="hidden" name="qty" value="$qty"/>  
        </form>
header.php

Code: Select all

<?php
session_start();	
$res=$_POST['qty'];
    echo "counter:$res"; 
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP counter display

Post by Celauran »

You're assuming a form has been posted. You want to check instead.

Code: Select all

if (isset($_POST['qty'])) {
	$res = $_POST['qty'];
	echo "counter:$res";
}
I also noticed the form on index.php has no submit button.

Finally, index.php seems to have the first two lines in reversed order. It should read

Code: Select all

<?php require_once 'foo'; ?>
<?php
Alternately, you can simply leave the tag open after the require line and remove the next opening tag entirely.
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

Re: PHP counter display

Post by terrenuit »

what is "foo" ?

if I use your script:
it is no good
if (isset($_POST['qty'])) {
$res = $_POST['qty'];
echo "counter:$res";
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP counter display

Post by Celauran »

I couldn't remember the name of the file you were requiring, so I just used some dummy text. How do you mean my script is no good? I posted a number of potential errors in the code you had originally posted. Have they all been addressed? What's happening with your script now? What errors are you encountering?
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

Re: PHP counter display

Post by terrenuit »

with your method, the error is:
Parse error: syntax error, unexpected '{' in C:\wampserver32\www\sale\header.php on line 3
which mean's : (isset($_POST['qty'])) {
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP counter display

Post by Celauran »

I don't see anything wrong with that line. Please also post the lines above it.
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

Re: PHP counter display

Post by terrenuit »

I changed my header.php script:
it is better, now, I have the counter display, but it is always in "0",
before, there was no counter output

<?php
session_start();
if ($_POST) {
$res=$_POST['qty'];
}
else { $res=0; }
echo "counter:$res";
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP counter display

Post by Celauran »

That's probably because there's currently no mechanism for submitting your form, so $_POST is going to be an empty array.
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

Re: PHP counter display

Post by terrenuit »

when I put value=3, the counter is always "0"

<form action="header.php" method="post">
<input type="hidden" name="qty" value="3"/>
</form>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP counter display

Post by Celauran »

Because you've created the form but never actually submitted it.
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

Re: PHP counter display

Post by terrenuit »

do you know any method that I can send the variable $qty to header.php without using submitted form ?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP counter display

Post by Celauran »

Where is the value coming from and what is the goal here? If it's stored in a database, for example, you could just query it on load.
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

Re: PHP counter display

Post by terrenuit »

the variable $qty is the client who want to buy the quantity( for each item),
i want to display this quantity,
now, I know I have to use the "public function count()"
I am Learning it,
do you have any idea ?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP counter display

Post by Celauran »

I'm not sure you'd need count(). That will return the number of elements in an array. You seem to already have handled that when you iterate over $listproduit. Looking at the code, $qty probably contains the correct value and the issue is solely one of display. Does that sound correct?
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

Re: PHP counter display

Post by terrenuit »

using submitted form is impossible to realise my program,
I saw a tutorial in "public function count()",
I am Learning it I wish I could arrive
thank you !
Post Reply