Hi,

I am having a problem with undefined variable. But I am actually not sure what is the problem is.

<?php
require_once "dbconnect.php";
$link=dbconnect();
$query=("select *from  cart where cid='$cid'");
$result=mysql_query($query);
$count2=mysql_num_rows($result);

while($row= mysql_fetch_array($result))
 {

$count=1;

[COLOR="Red"]$totalprice+=$row['copies']*$row['price'];[/COLOR]
$count+=1;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome to Your Store </title>
<link href="style.css" rel="stylesheet" type="text/css" />

</head>
<div class="borderbox">
                <div class="leftcol_box">
                <div id="product_search">
                        <h2><span>Your Shopping Cart </span></h2>
                        <div class="content">
                        </br>
                        <center>
                          <p align="left"><?php Echo $name;?>     </p>
                          <br>
                          <p align="left">Total Products :   <?php Echo $count2;?>                       </p>
                          <p align="left">Total Amount   :   <?php Echo $totalprice;?></p>
                          <br>
                          <a href="vnc.php">View and Checkout
                          </a>
                        </center>
</div>
                </div>
                </div>
                </div>

this code giving me the output but also giving me a notice --

Notice: Undefined variable: totalprice in C:\wamp\www\Your Store\cart.php on line 13

I am not sure how to fix it. Anyone can help me pls?

    Try initializing totalPrice to 0 before the loop.

    $totalprice = 0 // add this to your code
    
    while($row= mysql_fetch_array($result))
     {
    
    $count=1;
    
    $totalprice+=$row['copies']*$row['price'];
    $count+=1;
    }
    
    

      Either that or you can alter your [man]error_reporting[/man] configuration to something less finicky.

        david8787 wrote:

        Try initializing totalPrice to 0 before the loop.

        And you'll want to shift your "$count = 1;" line to the same place or it will never get past 2 (not that you need it anyway, because because it will be equal to $count2+1 anyway).

        Actually, you don't need the loop at all; [font=monospace]SELECT SUM(price*copies) AS totalPrice FROM cart where cid='$cid'[/font] will give you one record with one field containing the total price.

          sneakyimp;10953515 wrote:

          Either that or you can alter your [man]error_reporting[/man] configuration to something less finicky.

          Nothing finicky about PHP complaining about the use of an undefined variable, really. PHP is just nice enough to assume that you wanted to initialize the variable to 0 before incrementing it.

          Try using an undefined variable in C++ and see how far you get with just ignoring the error messages. 😉

            You are correct brad, it's always a good idea to init a var (especially with register_globals lurking around on legacy systems). However, many popular FOSS software projects will complain relentlessly because the original coders assumed E_NOTICE errors are turned off due to the fact that the PHP default setting is E_ALL & ~E_NOTICE.

              Write a Reply...