Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Php Newbie, bit of help needed - undefined notices

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Php Newbie, bit of help needed - undefined notices

Postby thefandango » Fri Jun 28, 2013 11:13 am

Hi

Very new and struggling my way through a book. The exercise is around a simple shopping cart page.

I have updated the code with some isset() to clear a few undefined notices, but there are a couple I am totally stuck with.

The code is:

Code: Select all
<?php
//start session
session_start();

//initialise the session shopping cart
if (!isset($_SESSION['cart']))
{
   $_SESSION['cart'] = array();
}
   
//find the catalogue
$catalogFile = "catalogue.dat";

//file is available extract data from it
//place into an array called $CATALOG with sku as the key
if (file_exists($catalogFile))
{
   $data = file($catalogFile);
   foreach ($data as $line)
      {
         $linearray = explode (':', $line);
         $sku = trim ($linearray[0]);
         $CATALOG[$sku] ['desc'] = trim ($linearray[1]);
         $CATALOG [$sku] ['price'] = trim ($linearray[2]);
      }
}

else

{
         die ("could not find file");
}

//check to see if the form has been submitted and which submit button was clicked
//if this is an add operation add to existing quantities in the cart
if (isset($_POST['add']))
{
   foreach ($_POST['a_qty'] as $k => $v)
   {
   //if the value is zero do not bother changing the cart
   if ($v > 0)
      {
      $_SESSION['cart'][$k] = $_SESSION['cart'][$k] + $v;
      }
   }
}

//if this is an update to the cart operation replace quantities
//in the cart with values entered
else if (isset($_POST['update']))
{   
   foreach ($_POST['u_qty'] as $k => $v)
   {
   //if value is empty negative or zero dont update the cart
      if ($v != "" && $v >= 0)
      {
         $_SESSION['cart'][$k] = $v;
      }
   }
}

//if this is a clear operation reset the cart and destroy session
else if (isset($_POST['clear']))
{
      $_SESSION = array();
      session_destroy();
}
?>      
   
<html>
<head></head>
<body>

<h1>Catalogue</h1>

<form action="" method="post">   
<table border="0" cellspacing="10">
<?php
//print items for selection in a form
foreach ($CATALOG as $k => $v)
{
echo "<tr><td colspan=2>";
echo "<b>" . $v['desc'] . "</b>";
echo "</td></tr>\n";
echo "<tr><td>";
echo "Price per unit: " . $CATALOG[$k]['price'];
echo "</td><td>Quantity: ";
echo "<input size=4 type=text name=\"a_qty[" . $k . "]\">";
echo "</td></tr>\n";
}
?>
<tr>
<td colspan="2">
<input type="submit" name="add" value="add to cart">
</td>
</tr>
</table>   

<hr />
<hr />

<h2>Cart</h2>
<table width="500" border="1" cellspacing="10">
<?php
//initialise a variable to hold total cost
$total = 0;

//checl the cart and if it contains values look up the sku in the $CATALOG array
//get the cost and calculate sutotal and total
if (is_array($_SESSION['cart']))
{
   foreach ($_SESSION['cart'] as $k => $v)
   {
   //only display items thyat have been selected, ie, qunatitiees greater then zero
   if ($v > 0)
   {
      $subtotal = $v * $CATALOG[$k]['price'];
      $total += $subtotal;
      echo "<tr><td>";
      echo "<b>$v unit(s) of " . $CATALOG[$k]['desc'] . "</b>";
      echo "</td><td>";
      echo "New qty: <input size=4 type=text name=\"u_qty[" . $k . "]\">";
      echo "</td><td>";
      echo "Price per unit; " . $CATALOG[$k]['price'];
      echo "</td><td>";
      echo "Sub total: " . sprintf("%0.2f", $subtotal);
      echo "</td></tr>\n";
   }
   }
}
?>
<tr>
<td><b>TOTAL</b></td>
<td><b><?=sprintf("%0.2F", $total)?></b></td>
</tr>
<tr>
<td><input type="submit" name="update" value="update cart""></td>
<td><input type="submit" name=" clear" value="clear cart"></td>
</tr>
</table>
</form>
</body>
</html>   


The messages I cannot figure out how to solve are:

Notice: Undefined offset: 101 in C:\xampp\htdocs\stuff\cart.php on line 43

Notice: Undefined index: cart in C:\xampp\htdocs\stuff\cart.php on line 110


If someone could explain what those noties mean and how they are fixed that would help me learn thank you.


Also, the book I am using is a few years old. Is the php above up to date and well written or should I be using a newer teaching source?
thefandango
New php-forum User
New php-forum User
 
Posts: 2
Joined: Fri Jun 28, 2013 11:06 am

Re: Php Newbie, bit of help needed - undefined notices

Postby johnj » Fri Jun 28, 2013 8:45 pm

To explain this error, you need to understand about arrays in php.

Let see an example: Suppose I want to store the quatity of fruits in a shop in an array. For this I will
create an array like this:
Code: Select all
$fruits = array();
$fruits['apple'] = 120;
$fruits['orange'] = 60;
$fruits['pear'] = 220;


To access the count of these fruits you will do:
Code: Select all
echo " Count of orange is ".$fruits['orange'];
echo " Count of apple is ".$fruits['apple'];
echo " Count of pear is ".$fruits['pear'];


Let us try to access the count of "lemon";
Code: Select all
echo " Count of Lemon is ".$fruits['lemon'];


But lemon is not there in our array and php will throw the "undefined" notice.
Similarly php will again throw undefined notice if you try to display $fruits['99'];
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Php Newbie, bit of help needed - undefined notices

Postby johnj » Fri Jun 28, 2013 8:46 pm

johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Php Newbie, bit of help needed - undefined notices

Postby thefandango » Sat Jun 29, 2013 12:32 am

Hi

Thanks for the reply.

I did think it was to do with arrays, and sort of guessed what you describe is the case. Of course now changing the code to et rid of them is a new challenge!

So as this book was written by an "expert" that sort of issue should not be there?? Are there any new books that are recommended?
thefandango
New php-forum User
New php-forum User
 
Posts: 2
Joined: Fri Jun 28, 2013 11:06 am

Re: Php Newbie, bit of help needed - undefined notices

Postby johnj » Sat Jun 29, 2013 6:00 am

There isn't anything like "the best book". The ideal one is the manual on php.net.

There are plenty of "good" books. One of them is here
http://phillipnb.wordpress.com/2012/09/ ... beginners/

Beginning PHP by Doyle is another good one

Another one is Programming PHP by Kevin Tatroe, Peter MacIntyre and Rasmus Lerdorf

Another one Beginning PHP and MySQL: From Novice to Pr...by W. Jason Gilmore
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Php Newbie, bit of help needed - undefined notices

Postby xiaomingzc » Mon Jul 01, 2013 11:26 pm

item amounts depend. In buy diablo 3 Gold common, items has two machines of quality. Natural, red, and violet is just one range, even although raw item stage could be another range. like a conventional Cheap WOW Gold concept, items is normally much better using a greater item stage. Purple items could be probably the most effective within of similar level of Runescape Gold item stage, even although eco-friendly could be the most severe. When in question, you usually wish to go using the biggest item stage possible.
xiaomingzc
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Jul 01, 2013 11:22 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron