Sunday, May 30, 2010

Workshop 12 : PHP & MySQL Online Store Pt.3

In this workshop, we will further enhance the developed Online Store by adding a shopping cart functionality. To do this, we first need some new tables created in the lohky database to be able to implement the shopping cart functionality.

Mysql > use lohky 

mysql> create table store_shoppertrack(
     id int not null primary key auto_increment,
     session_id varchar (32),
     sel_item_id int,
     sel_item_qty smallint,
     sel_item_size varchar (25),
     sel_item_color varchar (25),
     date_added datetime
     );
Query OK, 0 rows affected (0.00 sec)

mysql> create table store_orders (
    -> id int not null primary key auto_increment,
    -> order_date datetime,
    -> order_name varchar (100),
    -> order_address varchar (255),
    -> order_city varchar (50),
    -> order_state char(50),
    -> order_zip varchar (10),
    -> order_tel varchar (25),
    -> order_email varchar (100),
    -> item_total float(6,2),
    -> shipping_total float (6,2),
    -> authorization varchar (50),
    -> status enum ('processed', 'pending')
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> create table store_orders_items(
    -> id int not null primary key auto_increment,
    -> order_id int,
    -> sel_item_id int,
    -> sel_item_qty smallint,
    -> sel_item_size varchar (25),
    -> sel_item_color varchar (25),
    -> sel_item_price float (6,2)
    -> )
    -> ;
Query OK, 0 rows affected (0.00 sec)


Then we move on to the PHP part of the shopping cart implementation, in which functions to manage (add/remove/view) the shopping cart is constructed:

addToCart.php:

<?php
session_start();

//connect to database
$conn = mysql_connect("localhost", "lohky", "welcome") or die(mysql_error());
mysql_select_db("lohky",$conn)  or die(mysql_error());

if ($_POST[sel_item_id] != "") {
   //validate item and get title and price
    $get_iteminfo = "select item_title from store_items where id = $_POST[sel_item_id]";
    $get_iteminfo_res = mysql_query($get_iteminfo) or die(mysql_error());

    if (mysql_num_rows($get_iteminfo_res) < 1) {
           //invalid id, send away
           header("Location: seestore.php");
           exit;
    } else {
           //get info
           $item_title =  mysql_result($get_iteminfo_res,0,'item_title');

           //add info to cart table
           $addtocart = "insert into store_shoppertrack values ('', '$PHPSESSID', '$_POST[sel_item_id]', '$_POST[sel_item_qty]', '$_POST[sel_item_size]', '$_POST[sel_item_color]', now())";
          mysql_query($addtocart);

           //redirect to showcart page
           header("Location: showcart.php");
          exit;
    }
} else {
    //send them somewhere else
    header("Location: seestore.php");
    exit;
}
?>


removeFromCart.php

<?php
session_start();
//connect to database
$conn = mysql_connect("localhost", "lohky", "welcome")  or die(mysql_error());
mysql_select_db("lohky",$conn)  or die(mysql_error());

if ($_GET[id] != "") {
    $delete_item = "delete from store_shoppertrack where id = $_GET[id] and session_id = '$PHPSESSID'";
    mysql_query($delete_item) or die(mysql_error());

    //redirect to showcart page
    header("Location: showcart.php");
    exit;

} else {
    //send them somewhere else
    header("Location: seestore.php");
    exit;
}
?>


showCart.php

<?php
session_start();

//connect to database
$conn = mysql_connect("localhost", "lohky", "welcome") or die(mysql_error());
mysql_select_db("lohky",$conn)  or die(mysql_error());

$display_block = "<h1>Your Shopping Cart</h1>";

//check for cart items based on user session id
$get_cart = "select st.id, si.item_title, si.item_price, st.sel_item_qty, st.sel_item_size, st.sel_item_color from store_shoppertrack as st  left join store_items as si on si.id = st.sel_item_id where  session_id = '$PHPSESSID'";
$get_cart_res = mysql_query($get_cart) or die(mysql_error());

if (mysql_num_rows($get_cart_res) < 1) {
    //print message
    $display_block .= "<P>You have no items in your cart.
    Please <a href=\"seestore.php\">continue to shop</a>!</p>";

} else {
    //get info and build cart display
    $display_block .= "
    <table celpadding=3 cellspacing=2 border=1 width=98%>
    <tr>
    <th>Title</th>
    <th>Size</th>
    <th>Color</th>
    <th>Price</th>
    <th>Qty</th>
    <th>Total Price</th>
    <th>Action</th>
    </tr>";

    while ($cart = mysql_fetch_array($get_cart_res)) {
           $id = $cart['id'];
           $item_title = stripslashes($cart['item_title']);
           $item_price = $cart['item_price'];
           $item_qty = $cart['item_qty'];
           $item_color = $cart['sel_item_color'];
           $item_size = $cart['sel_item_size'];
        $total_price = sprintf("%.02f", $item_price * $item_qty);

           $display_block .= "<tr>
           <td align=center>$item_title <br></td>
           <td align=center>$item_size <br></td>
           <td align=center>$item_color <br></td>
           <td align=center>\$ $item_price <br></td>
           <td align=center>$item_qty <br></td>
           <td align=center>\$ $total_price</td>
           <td align=center><a href=\"removefromcart.php?id=$id\">remove</a></td>
           </tr>";
    }

    $display_block .= "</table>";
}
?>
<HTML>
<HEAD>
<TITLE>My Store</TITLE>
</HEAD>
<BODY>
<? print $display_block; ?>
</BODY>
</HTML>

Workshop 11 : PHP & MySQL Online Store Pt.2

In this workshop, will continue our discussion on developing an Online store using the records we have created in the previous workshop with the help of some PHP programming.
First, we need to develop the PHP codes to make-up the actual Store.

<?php
//connect to database
$conn = mysql_connect("localhost", "lohky", "welcome") or die(mysql_error());
mysql_select_db("lohky",$conn)  or die(mysql_error());

$display_block = "<h1>My Categories</h1>
<P>Select a category to see its items.</p>";

//show categories first
$get_cats = "select id, cat_title, cat_desc from store_categories order by cat_title";
$get_cats_res = mysql_query($get_cats) or die(mysql_error());

if (mysql_num_rows($get_cats_res) < 1) {
   $display_block = "<P><em>Sorry, no categories to browse.</em></p>";
} else {
   while ($cats = mysql_fetch_array($get_cats_res)) {
        $cat_id  = $cats[id];
        $cat_title = strtoupper(stripslashes($cats[cat_title]));
        $cat_desc = stripslashes($cats[cat_desc]);

        $display_block .= "<p><strong><a href=\"$_SERVER[PHP_SELF]?cat_id=$cat_id\">$cat_title</a></strong><br>$cat_desc</p>";

        if ($_GET[cat_id] == $cat_id) {
           //get items
           $get_items = "select id, item_title, item_price from store_items where cat_id = $cat_id order by item_title";
           $get_items_res = mysql_query($get_items) or die(mysql_error());

           if (mysql_num_rows($get_items_res) < 1) {
                $display_block = "<P><em>Sorry, no items in this category.</em></p>";
           } else {
                $display_block .= "<ul>";

                while ($items = mysql_fetch_array($get_items_res)) {
                   $item_id  = $items[id];
                   $item_title = stripslashes($items[item_title]);
                   $item_price = $items[item_price];

                   $display_block .= "<li><a href=\"showitem.php?item_id=$item_id\">$item_title</a></strong> (\$$item_price)";
                }

                $display_block .= "</ul>";
           }
       }
   }
}
?>
<HTML>
<HEAD>
<TITLE>My Categories</TITLE>
</HEAD>
<BODY>
<? print $display_block; ?>
</BODY>
</HTML>


This code will show the products based on its categories. To view the items listed in the categories, we need these lines of codes:

<?php
//connect to database
$conn = mysql_connect("localhost", "lohky", "welcome") or die(mysql_error());
mysql_select_db("lohky",$conn)  or die(mysql_error());

$display_block = "<h1>My Store - Item Detail</h1>";

//validate item
$get_item = "select c.cat_title, si.item_title, si.item_price, si.item_desc, si.item_image from store_items as si left join store_categories as c on c.id = si.cat_id where si.id = $_GET[item_id]";
$get_item_res = mysql_query($get_item) or die (mysql_error());

if (mysql_num_rows($get_item_res) < 1) {
   //invalid item
   $display_block .= "<P><em>Invalid item selection.</em></p>";
} else {
   //valid item, get info
   $cat_title = strtoupper(stripslashes(mysql_result($get_item_res,0,'cat_title')));
   $item_title = stripslashes(mysql_result($get_item_res,0,'item_title'));
   $item_price = mysql_result($get_item_res,0,'item_price');
   $item_desc = stripslashes(mysql_result($get_item_res,0,'item_desc'));
   $item_image = mysql_result($get_item_res,0,'item_image');

   //make breadcrumb trail
   $display_block .= "<P><strong><em>You are viewing:</em><br><a href=\"seestore.php?cat_id=$cat_id\">$cat_title</a> &gt; $item_title</strong></p>
   <table cellpadding=3 cellspacing=3>
   <tr>
   <td valign=middle align=center><img src=\"$item_image\"></td>
   <td valign=middle><P><strong>Description:</strong><br>$item_desc</p>
   <P><strong>Price:</strong> \$$item_price</p>";

   //get colors
   $get_colors = "select item_color from store_item_color where item_id = $item_id order by item_color";
   $get_colors_res = mysql_query($get_colors) or die(mysql_error());

   if (mysql_num_rows($get_colors_res) > 0) {
        $display_block .= "<P><strong>Available Colors:</strong><br>";
        while ($colors = mysql_fetch_array($get_colors_res)) {
           $item_color = $colors['item_color'];
           $display_block .= "$item_color<br>";
       }
   }

   //get sizes
   $get_sizes = "select item_size from store_item_size where item_id = $item_id order by item_size";
   $get_sizes_res = mysql_query($get_sizes) or die(mysql_error());

   if (mysql_num_rows($get_sizes_res) > 0) {
       $display_block .= "<P><strong>Available Sizes:</strong><br>";

       while ($sizes = mysql_fetch_array($get_sizes_res)) {
          $item_size = $sizes['item_size'];
          $display_block .= "$item_size<br>";
       }
   }

   $display_block .= "
   </td>
   </tr>
   </table>";
}
?>
<HTML>
<HEAD>
<TITLE>My Store</TITLE>
</HEAD>
<BODY>
<? print $display_block; ?>
</BODY>
</HTML>


To view the online store, upload both .php files. name the first one seeStore.php and the second seeItems.php. After uploaded, you can visit the store by calling seeStore.php

Workshop 10 : PHP & MySQL Online Store Pt.1

In this workshop, we will discuss the steps to create a simple online store application with PHP and MySQL.

The first step is to login to the MySQL database and select/ create a database to be used. Instructions on how to do this can be found on the previous workshop. Let's use the lohky database created earlier and then create a table using t enter records to save some time. Here we go!
Mysql > use lohky 

mysql> create table store_categories (
    -> id int not null primary key auto_increment,
    -> cat_title varchar (50) unique,
    -> cat_desc text
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> create table store_items (
    -> id int not null primary key auto_increment,
    -> cat_id int not null,
    -> item_title varchar (75),
    -> item_price float (8,2),
    -> item_desc text,
    -> item_image varchar (50)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> create table store_item_size (
    -> item_id int not null,
    -> item_size varchar (25)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> create table store_item_color(
    -> item_id int not null,
    -> item_color varchar (25));
Query OK, 0 rows affected (0.00 sec)


After creating the table, we would then need to store some records inside the created tables

Insert some records onto the store_categories tables

mysql> insert into store_categories values
    -> ('1','hats','funky hats in all shapes and sizes!');
Query OK, 1 row affected (0.00 sec)

mysql> insert into store_categories values ('2', 'Shirts', 'All shapes and size
s.');
Query OK, 1 row affected (0.00 sec)

mysql> insert into store_categories values ('3', 'Books', 'Fiction and all.');
Query OK, 1 row affected (0.01 sec)


Insert some records on to the store_items table

mysql> insert into store_items values ('1', '1', 'Baseball Hat', '12.00', ' Fan
cy, low profile baseball hat.', 'baseballhat.gif');
Query OK, 1 row affected (0.07 sec)

mysql> insert into store_items values ('2', '1', 'cowboy hat', '52.00', '10 gal
lon variety', 'cowboyhat.gif');
Query OK, 1 row affected (0.00 sec)

mysql> insert into store_items values ('3', '1', 'Top Hat', '102.00', 'A classi
c', 'tophat.gif');
Query OK, 1 row affected (0.00 sec)

mysql> insert into store_items values ('4', '2', 'Short sleeve T Shirt', '12.00
', '100% cotton, pre shrunk.', 'sstshirt.gif');
Query OK, 1 row affected (0.00 sec)

mysql> insert into store_items values ('5', '2', 'Long sleeve T Shirt', '15.00
'> ', '100% cotton, pre shrunk.', 'lstshirt.gif');
Query OK, 1 row affected (0.00 sec)

mysql> insert into store_items values ('6', '2', 'Sweatshirt', '22.00
'> ', 'Ideal for chilly nights.', 'sweatshirt.gif');
Query OK, 1 row affected (0.00 sec)

mysql> insert into store_items values ('7', '3', 'Jane Self Help Book', '12.00
'> ', 'HELPING You.', 'book1.gif');
Query OK, 1 row affected (0.00 sec)

mysql> insert into store_items values ('8', '3', 'Client Server Applications',
'40.00
'> ', 'A good reference.', 'book2.gif');
Query OK, 1 row affected (0.00 sec)

mysql> insert into store_items values ('9', '3', 'Style Guru', '40.00
'> ', 'Fashionable tips.', 'book3.gif');
Query OK, 1 row affected (0.00 sec)

Insert records into the store_item_size table

mysql> Insert into store_item_size values (1, 'one size fits all');
Query OK, 1 row affected (0.00 sec)

mysql> Insert into store_item_size values (2, 'one size fits all');
Query OK, 1 row affected (0.00 sec)

mysql> Insert into store_item_size values (3, 'one size fits all');
Query OK, 1 row affected (0.00 sec)

mysql> Insert into store_item_size values (4, 'S');
Query OK, 1 row affected (0.01 sec)

mysql> Insert into store_item_size values (4, 'M');
Query OK, 1 row affected (0.00 sec)

mysql> Insert into store_item_size values (4, 'L');
Query OK, 1 row affected (0.00 sec)

mysql> Insert into store_item_size values (4, 'XL');
Query OK, 1 row affected (0.00 sec)


Insert records into the store_item_color table

mysql> Insert into store_item_color values (1, 'red');
Query OK, 1 row affected (0.00 sec)

mysql> Insert into store_item_color values (1, 'black');
Query OK, 1 row affected (0.00 sec)

mysql> Insert into store_item_color values (1, 'blue');
Query OK, 1 row affected (0.00 sec)


We now have our basic database, complete with tables and records to be used in conjuction with our PHP code which we will discuss on the next Workshop.

Exercise 15: M-Commerce: Innovation and Mobile Devices

1. What is meant by a location based service? Explain using the Web applications found on a late model mobile device.
A location-based service (LBS) is an information service, accessible with mobile devices through the mobile network and utilizing the ability to make use of the geographical position of the mobile device. An example of this is the Map/Navigation function or GoogleMaps found in many of today's mobile phones. GoogleMaps will retrieve information on the device's whereabouts and it's surroundings to provide users with a sense of direction by displaying the information on the device's screen as a map. The concept on determining the users position can be done by measuring the phone's distance from the cell-phone towers nearby or using GPS services provide an accurate location of the subject in relation to the services specified by the user. 

2. Describe the purpose of Open Mobile Alliance Initiative?
OMAI's main goal is to remove the barriers to global user adoption and to ensure seamless application interoperability while allowing businesses to compete through innovation and differentiation. This is done to grow the market of mobile industry to a new level of inter-operability.
Through OMAI, competition are encouraged through innovation and differentiation, while ensuring the interoperability of mobile service through the entire value chain.
3. What are the main components of a mobile Web services framework?According to The European Space Agency (2008), the components of a mobile web services framework are:

Mobile Server
The Mobile Server is a mobile remote computer, linked to the Internet via an Inmarsat Regional Broadband Global Area Network (RBGAN) User Terminal (UT).
Gatekeeper
The Gatekeeper is placed on the terrestrial Internet, and acts as the sole gateway to the Mobile Server.
RBGAN UT / Thuraya Satellite / RBGAN SAS
The physical connection between the remote web server and the Gatekeeper is established using a Satellite Access Station, a Telecommunications Satellite and a satellite modem.
GPS / Data Acquisition system / Web cam
The Mobile Server is connected to a set of peripherals, such as a GPS device, a web cam and/or a Data Acquisition system.
Application server / Client PC
The Gatekeeper handles requests from clients over the Internet.

4. Visit an airline Web site and search for information on WAP or SMS or 3G mobile application access to the booking airline system. The same services exist in banking. How do they compare?
Companies researched: 
Singapore Airlines and Commonwealth Bank

The difference in the services provided on the different business is that, on Banking systems, security is the priority, this can be seen clearly, when opening a connection to the site, which is done through a secure SSL connection. Upon exiting the site, secure information contained in the phone browser's cache are prompted to be removed as to its secure nature.  On the other hand, on airline web site, the only concern is having customers having to be able to connect to the system anywhere, so security measures are mainly only implemented on sensitive areas such as booking checkout.


References:
Location-Based Sevice.Wikimedia Foundation, Inc. From http://en.wikipedia.org/wiki/Location-based_service Retreived on 28/05/2010
Mobile Web Services Framework (2008).European Space Agency 2008, ESA Telecommunication. From
http://telecom.esa.int/telecom/www/object/index.cfm?fobjectid=12852 Retrieved on 28/05/2010  
http://www.singaporeair.com Retrieved on 28/05/2010 
http://www.commbank.com.au Retrieved on 28/05/2010 
http://en.wikipedia.org/wiki/Open_Mobile_Alliance Retrieved on 28/05/201

Exercise 14: Searching Mechanisms, virtual worlds and cyberagents


1.  What is a spider? What does it do?
A spider is a piece of software which  designed to go through or "crawl" through a specific medium.  These mediums could be as simple as a text file, a database table or as complex as  the internet. In the internet, web-crawlers /spiders are used in search for new websites/ pages.  A web-crawler visits crawls through the internet by visiting sites and links to and from the sites. on finding a new web page or website, the web-crawler indexed and cataloged their findings for future rapid retrieval. Search companies e.g. Google use spiders to keep being updated on these new sites and to find updates on the known sites to increase the accuracy of their search results

2. Differentiate the various types of software agents.
A software agent is a a software entity which functions continuously and autonomously
in a particular environment, often inhabited by other agents and processes (Shoham 1997).
Software agents differ from conventional software in that they are long-lived, semi-autonomous, proactive, and adaptive.
These include:
Intelligent agents  are agents that in particular exhibits some aspect of Artificial Intelligence, such as learning and reasoning.
Autonomous agents  - agents that are able to adapt, giving them the ability to modify their approaches in achieving their objectives.
Distributed agents  - agents that are being executed on physically distinct computers over distribution network.
Multi-agent systems  - distributed agents that do not have the capabilities to achieve an objective alone and thus must communicate with other agents.
Mobile agents  - agents that can relocate their execution onto different processors, hence: mobile.

3. Identify various activities in e-commerce where software agents are currently in use.

Buyer agents or shopping bots  are used in many eCommerce shops, for example  Amazon.com .The sopping bot provides a list of books recommendations based on your buying history.
User or personal agents example at jobdb.com , which sends email notifications about the current available jobs in the market according to your personal resume data.

Monitoring and Surveillance agents are used in NASA's Jet Propulsion Laboratory,  which monitors inventory, planning and scheduling equipment ordering to keep monitoring and surveillance costs down.

Data Mining agents are commonly used in credit card companies to provide them with information on consumer's spending habits and tendencies. These information gathered by the miners could be priceless to the businesses in forecasting the market behavior.


References:

Shoham, Y. 1997. An Overview of Agent-oriented Programming. In Software Agents, ed
J. M. Bradshaw. Menlo Park, Calif.: AAAI Press.

http://en.wikipedia.org/wiki/Web_crawler accessed on 28/05/2010
 

http://en.wikipedia.org/wiki/Software_agent accessed on 28/05/2010