Why is it saying "Notice: Undefined variable: cnt in C:\Wamp\www\librarie\cos.php on line 24". Here is the code:

<?php
	session_start();
	include("conectare.php");
	include("page_top.php");
	include("meniu.php");
	
	if (isset($_GET['actiune']) && ($_GET['actiune'] == "adauga"))
	{

if (!isset($_SESSION['cnt']) || !$_SESSION['cnt'])
        $_SESSION['cnt'] = '0';

$cnt =  $_SESSION['cnt']; $cnt++;
$_SESSION['cnt'] = $cnt;

		$_SESSION['nr_buc'][] = 1;
		$_SESSION['pret'][] = $_POST['pret'];
		$_SESSION['titlu'][] = $_POST['titlu'];
		$_SESSION['nume_autor'][] = $_POST['nume_autor'];
	}
	
	if (isset($_GET['actiune']) && ($_GET['actiune'] == "modifica"))
	{
		for ($i=0; $i < $cnt; $i++)
		{
			$_SESSION['nr_buc'][$i] = $_POST['noulNrBuc'][$i];
		}
	}
?>

<td valign="top">
<div align="center">
	<h1><img src="pictures/cumparaturi.png"></h1>
	<form action="cos.php?actiune=modifica" method="post">
		<table border="1" cellspacing="0" cellpadding="4">
			<tr bgcolor="#F9F1E7">
				<td><b>Nr. buc </b></td>
				<td><b>Carte</b></td>
				<td><b>Pret</b></td>
				<td><b>Total</b></td>
			</tr>
			<?php
            $cnt = $_SESSION['cnt'];
            if (!isset($cnt)) $cnt = '';

            $totalGeneral = 0;
			for ($i=0; $i < $cnt; $i++)
			{
				//afisez randul doar daca numarul de bucati este diferit de 0
				if ($_SESSION['nr_buc'][$i] != 0)
				{
					print '<tr>
								<td><input type="text" name="noulNrBuc['.$i.']" size="1" value="'.$_SESSION['nr_buc'][$i].'"></td>
								<td><b>'.$_SESSION['titlu'][$i].' de '.$_SESSION['nume_autor'][$i].'</td>
								<td align="right">'.($_SESSION['pret'][$i]*$_SESSION['nr_buc'][$i]).' lei</td>
							</tr>';
					$totalGeneral = $totalGeneral + $_SESSION['pret'][$i]*$_SESSION['nr_buc'][$i];
				}
			}
			print '<tr>
						<td align="right" colspan="3"><b>Total in cos</b></td>
						<td align="right"><b>'.$totalGeneral.'</b> lei</td>
					</tr>';
			?>
		</table>
		<br>
		<input type="submit" value="Modifica"><br><br>
		Introduceti <b>0</b> pentru cartile ce doriti sa le scoateti din cos!
<hr size="1" />
		<h1><img src="pictures/continuare.png"></h1>
		<table>
			<tr>
				<td width="200" align="center"> <img src="pictures/cos.JPG"><a href="index.php">Continua cumparaturile</a></td>
				<td width="200" align="center"><img src="pictures/casa.JPG"><a href="casa.php">Mergi la casa</a></td>
			</tr>
		</table>
	</form>
</td>

<?php
	include("page_bottom.php");			
?>

I tried using this 2 methods, but nothing:
1) error_reporting(E_ALL & E_NOTICE);
2) $cnt = $_SESSION;
if (!isset($cnt)) $cnt = '';

Recommended Answers

All 6 Replies

$cnt is set inside an if-statment:

if (isset($_GET['actiune']) && ($_GET['actiune'] == "adauga"))
             {

so if this is false $cnt don't exist

$cnt is set inside an if-statment:

if (isset($_GET['actiune']) && ($_GET['actiune'] == "adauga"))
             {

so if this is false $cnt don't exist

What do you suggest?

think you need to take the declaration of $cnt outside the if

if (!isset($_SESSION['cnt']) || !$_SESSION['cnt'])
            $_SESSION['cnt'] = '0';
 
$cnt = $_SESSION['cnt']; 
$cnt++;
$_SESSION['cnt'] = $cnt;

if (isset($_GET['actiune']) && ($_GET['actiune'] == "adauga"))
   {
   $_SESSION['nr_buc'][] = 1;
   $_SESSION['pret'][] = $_POST['pret'];
   $_SESSION['titlu'][] = $_POST['titlu'];
   $_SESSION['nume_autor'][] = $_POST['nume_autor'];
   }

think you need to take the declaration of $cnt outside the if

if (!isset($_SESSION['cnt']) || !$_SESSION['cnt'])
            $_SESSION['cnt'] = '0';
 
$cnt = $_SESSION['cnt']; 
$cnt++;
$_SESSION['cnt'] = $cnt;

if (isset($_GET['actiune']) && ($_GET['actiune'] == "adauga"))
   {
   $_SESSION['nr_buc'][] = 1;
   $_SESSION['pret'][] = $_POST['pret'];
   $_SESSION['titlu'][] = $_POST['titlu'];
   $_SESSION['nume_autor'][] = $_POST['nume_autor'];
   }

If i do that i get error every time i refresh the page:
Notice: Undefined offset: 2 in C:\Wamp\www\librarie\cos.php on line 49
Notice: Undefined offset: 3 in C:\Wamp\www\librarie\cos.php on line 49
Notice: Undefined offset: 4 in C:\Wamp\www\librarie\cos.php on line 49
...so on

Set default value for '$cnt'. If the 'if' statement wrong, fallback to deafult value. For example:

$cnt = 0; // fallback to use this default value when if statement wrong
if (!isset($_SESSION['cnt']) || !$_SESSION['cnt']){
$_SESSION['cnt'] = '0';
$cnt = $_SESSION['cnt']; 
$cnt++;
}

My code is this so far:

<?php
	session_start();
	include("conectare.php");
	include("page_top.php");
	include("meniu.php");
	
//	if (isset($_GET['actiune']) && ($_GET['actiune'] == "adauga"))
//	{
$cnt = 0;
if (!isset($_SESSION['cnt']) || !$_SESSION['cnt'])
    {
    $_SESSION['cnt'] = '0';
    $cnt =  $_SESSION['cnt']; 
    $cnt++;
    $_SESSION['cnt'] = $cnt;
  }  
    if (isset($_GET['actiune']) && ($_GET['actiune'] == "adauga"))
    {
		$_SESSION['nr_buc'][] = 1;
		$_SESSION['pret'][] = $_POST['pret'];
		$_SESSION['titlu'][] = $_POST['titlu'];
		$_SESSION['nume_autor'][] = $_POST['nume_autor'];
	}
	
	if (isset($_GET['actiune']) && ($_GET['actiune'] == "modifica"))
	{
		for ($i=0; $i < $cnt; $i++)
		{
			$_SESSION['nr_buc'][$i] = $_POST['noulNrBuc'][$i];
		}
	}
?>

But, although i have 2 items in my basket, it tells me i have 0 items in my basket as in picture.
http://uploadimage.ro/images/03458514878679674493.jpg

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.