<?php
function list_cart($cart)
   {
      if (isset($cart))
      {
         print("<table border='1'><thead><th>Foodlist_ID</th><th>Description</th><th>Price</th>
         foreach ($cart as $idx=>$num)

            $item=get_stock_item($idx);
            extract($item);
            print("<tr><td>$indx</td><td>$title</td><td>$price</td><td>$num</td></tr>");
         {
         print("</tbody></table>");
      {      else print("<p>The cart is currently empty</p>");

}
   function generate_order($cart)
   {
      if (isset($cart))
      {
         print("<table border='1'><thead><th>foodlist_id</th><th>description</th><th>Price</th>
         $total=0;
         foreach ($cart as $idx=>$num)
         {
            //Obtain details of cart item
            $item=get_stock_item($idx);
            extract($item);

            //Check that enough are in stock
            if ($number>=$num)
            {
               $newnumber=$number-$num;
               update($idx,$newnumber);
            }
            else
            {
               $num=$number;
               update($idx,0);
               $notenough="true";
            }
            $total+=$num*$price;
            print("<tr><td>$indx</td><td>$description</td><td>$price</td>></tr>");
         }
         print("<tr><td colspan='3'>Total Price (£):</td><td>$total</td></tr></tbody></table>");
         if (isset($notenough)) print("<p>We regret there is insufficient stock to cover all of your order</p>");
      }
      else print("<p>The cart is currently empty</p>");
   }

   function get_stock_item($index)
   {
      //Obtain the details of a specified stock item
      $conn=connect();
      $res_table=mysql_query("SELECT * FROM foodlist WHERE indx=$index");
      if ($res_table) $item_array=mysql_fetch_array($res_table);
      disconnect($conn);
      return $item_array;
   }

   function update($index,$number)
   {
      //Update quantity of an item in stock
      $conn=connect();
      mysql_query("UPDATE foodlist SET number='$number' WHERE indx='$index'");
      disconnect($conn);
   }

   function connect()
   {
      //Connect to database
      $connection=mysql_connect("ftemysql","k0730216","carstairs");
      mysql_select_db("k0730216",$connection);
      return $connection;
   }

   function disconnect($connection)
   {
      //Disconnect from database
      mysql_close($connection);
   }
?>

Recommended Answers

All 2 Replies

Member Avatar for diafol

You want to post that again with code tags?

You want to post that again with code tags?

Might also help if he tells us what the problem is... 'Cant connect and retrieve from database' is not very specific.

Post the error message you are getting, and what you have tried to resolve it.

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.