Pages

Sunday, October 25, 2015

Filtering products

Filtering products Another useful way to allow customers to better find the products they are looking for is with filtering.  Customers can filter down lists of products based on attributes, such as price ranges, manufacturer, weight, brands, and so on. Price range filtering should be simple enough. However, with attributes such as manufacturer or brands, we would need to extend the database and models representation of a product to maintain this additional information, and allow  us to filter down based on these attributes. There are a few different ways in which we can store filtered results: In the user's session: This will be lost when the user closes their browser. In a cookie: This information will stay when the user closes their browser. In the URL: This would allow the customer to filter results and send the link of those results to a friend. In POST data: The information will only be stored for the one instance the filter is used. Let's try using the URL to store filter data. If we format filter data within the URL as filter/attribute-type/attribute-value-ID, then we can simply iterate through the bits of the URL, find bits containing filter, and then take the next two parts of the URL to help build the filter. This way we can filter down products based on a number of attributes, for example filter/price/5/filter/weight/6. Of course, there is a limit to this, and that is the maximum length of a URL. 

Stock control

Stock control With product variations, stock control becomes an interesting issue. If we only had a single set of variations for each products (as we discussed under the Simple variants section, earlier in the chapter), we could simply disable an option if it was out of stock. However, with multiple variations we may have small blue t-shirts, but not large blue t-shirts. The logic for detecting if this is in stock obviously lies with the shopping basket itself: when we click on Add to basket, it will need to detect to  see if there are any in stock. This obviously isn't an ideal situation; however, an alternative would be to utilize AJAX to enable the view to alert the customer that a particular combination is out  of stock, by performing a lookup as and when the customer selects their variation of the product.

Product Variations and User Uploads

Having a store with products and categories is great, but we need to be able to offer greater flexibility with our products. We looked at how to extend the information stored about our products, and extending products that way; but certain types of product, like apparel, need to allow the user to customize the product, often by selecting a variation of the product, or uploading images or text as part of the  order. In this chapter, you will learn: How to create customizable products How to assign uploaded files to individual product orders How we will maintain these uploads How to assign custom user-submitted data with individual product orders One important point to note is that this chapter links in greatly with Chapter 6,  The Shopping Basket; so some aspects of this chapter may be preparation for that, and some aspects of that chapter may require some looking back at this one. This chapter will primarily focus on integrating support for these customizable products to our framework as it is at the moment. Giving users choice Many products in e-commerce stores require some sort of choice from the customer, be it the size, color, or even the material. At the moment, we only have very basic products, which can simply be viewed by our customers. We need to extend this to allow customers to see variations of products, and to be able to choose their own variation of the product, before purchasing it. 

.htaccess file

We have our index.php file set up to process the incoming request and send it to the relevant controller. However, URLs which have the format of index. php?page=some/page/on/our/site or index.php?page=products/view/someproduct are not as attractive or memorable as those with just some/page/on/our/ site or products/view/some-product. With the Apache module mod_rewrite we can get our site to rewrite the more friendly URLs into the less friendly ones for our framework to understand.
ErrorDocument 404 /index.php DirectoryIndex index.php <IfModule mod_rewrite.c>  RewriteEngine on  RewriteCond %{REQUEST_FILENAME} !-f  RewriteCond %{REQUEST_FILENAME} !-d  RewriteRule ^(.*)$ index.php?page=$1 [L,QSA] </IfModule> This .htaccess file instructs the web server (Apache) to use index.php as the index file within a directory. It also instructs Apache that if the mod_rewrite module is enabled, the requests that are not for valid files or directories should be rewritten to the main index.php file. However, anything that occurs after the directory containing the .htaccess file, should be appended to the page $_GET variable. For example, oursite.com/pagea would be rewritten to oursite.com/index. php?page=pagea.

Extending the database

Extending the database object So, how could we extend the database object, and the way we have designed it? Inheritance: We could have a database interface, which defines some base methods for any database object we create, making it easier to swap the type of database we use (for example, from MySQL to pgSQL, or MSSQL). Abstracting the logic to the queries: To further simplify the use of various database engines, we could abstract the logic from our queries into the database object itself. This way, instead of passing queries to the object, we pass the make up of the queries—for example, table, fields, fields to order by (to define how the results should be ordered), types of joins, and so on—and the object inserts every bit of SQL that is required. This is the only true way to have complete database abstraction within a framework. Debug information: We could add provisions for logging the performance of our queries, recording slow queries to allow us to debug, and optimize the queries we use.

Saturday, October 24, 2015

Model-View-Controller (MVC)

The Model-View-Controller architectural pattern provides a widely used solution to separate the user interface from the logic of an application. The user interface of the application (view) interacts with the data (model) using the controller, which contains the business rules needed to manipulate data sent to and from the model. To put this into an e-commerce perspective, consider a customer adding a product to their shopping basket clicks on an Add to basket button within the view/user interface. The controller processes this request and interacts with the model (basket) to add the product to the basket. Similarly, the data from within the basket is relayed back to the user interface through the controller, to display how many products are in the basket, and the value of the contents.
CONTROLLER
VIEW MODEL
Because we are creating a framework for use with websites and web applications, we can further extend the representation of the MVC pattern to reflect implementation in such a framework. As discussed earlier, the models represent data; this is primarily stored within the database. However, in our framework we will have a series of models, which take the data and store it within themselves in a more suitable format, or allowing the data to be manipulated more easily. So, we could in fact add our database to this diagram, to show the interaction with the models and the database. We are also viewing the end result of our website or web application in a web browser, which renders the views, and relays our interactions (for example mouse clicks or field submissions), back to the controller. So we could also add the
• 

Why use e-commerce?

Why use e-commerce? The popularity of online shopping has increased dramatically over the past few years. Not only does it provide the convenience of allowing customers to shop in the comfort of their own home, it also allows businesses to trade on a global marketplace, targeting even more potential customers. Because everything is done electronically, e-commerce stores can also help generate recurring revenue, by recommending new products to customers based on previous purchases, and  by keeping them up to date with the store's catalog. Rolling out your own framework Throughout the course of this book, we are going to build a framework of our own, using PHP, as opposed to making use of an existing product. Sometimes, it is more appropriate to use existing solutions; sometimes it is better to use your own solutions. As you are reading this book, hopefully you know why you want to create your own framework. However, let's look at why we are going to create ours. Why PHP? PHP is a very popular language, and because it isn't a framework in its own right, we can easily structure our framework out of it, however we wish. The main choice for a programming language is generally down to your own preference. Most modern web hosts support PHP and MySQL, and while languages like  Ruby on Rails are gaining popularity, at the moment hosting for them is not as common. This book assumes that you already have a reasonable understanding of PHP, so hopefully that will also be an important factor in why you want to use PHP; perhaps you need to develop something quickly, and don't want to use a language or platform that is out of your comfort zone. Why a framework? Instead of looking to create an e-commerce system, designed to perform all types of e-commerce tasks, we will create a framework. This will make it easy to extend the needs of any e-commerce project with minimal effort. Because we are creating our own framework, it is going to be something we will know and understand very  well, meaning that if we do need to extend it or use it, we can do so easily.

PHP e-commerce

Welcome to building a PHP e-commerce framework! During the course of this book we are going to build a flexible e-commerce framework using PHP, which can be extended and modified for the purposes of any e-commerce site. In this chapter, you will learn: The business logic behind e-commerce Why (and when) you should use your own system over an existing product The benefits of a "framework" About existing e-commerce sites and products e-commerce: Who, what, where, why? e-commerce, or electronic commerce, is the sale and purchase of goods or services through electronic means. In our case, this electronic means is the Internet. There are so many different applications of e-commerce on the Internet, including: Online shops selling products, such as Amazon, or the online counterparts to Brick 'N Mortar stores Online auctions, such as eBay Online services/web services such as BaseCamp, or subscription-based websites An overview of e-commerce e-commerce is an incredibly popular way of doing business, so let's look at who is using e-commerce and what they are using it for. • • • • • • •
PHP e-commerce

eBay According to eBay's website, there are approximately 84 million active users of eBay, with users trading more than $1,900 worth of goods each second. That means 84 million of us are using eBay to buy and sell goods, either as a business sustaining a regular turnover, or to try and make a little extra cash by selling unwanted or unneeded things knocking about the house. eBay is a social e-commerce site, operating as an online auction house, whereby they don't actually sell anything themselves, but instead allow their community of users to not only buy but also sell through their site. This not only illustrates the popularity of e-commerce, but also that there is money to be made in providing a stage for low (and high) volume online purchases. Amazon With revenue of over $19 billion in 2008, Amazon is one of the most popular  e-commerce sites on the Internet. Research in early 2009 indicated that it was  the favorite retailer for both video and music in the UK. Brick 'N Mortar stores Large, established Brick 'N Mortar stores such as Wal-Mart, Tesco, and Borders use online shops to sell the products they generally keep in store. With the likes of Wal-Mart and Tesco, customers often book a delivery timeslot for their groceries to be delivered. They also offer more than what is available in store, which they can easily bolt on for the convenience of their customers. With online retail, sellers are not confined to what they can stock on the shelves, but what they can store in their distribution warehouses. Smaller, niche-based Brick 'N Mortar stores use online selling as a way to  target their products to a wider audience, without the limitation of their  existing physical presence. Service-based companies Companies such as 37signals are setting up online applications (such as Project Management tool, BaseCamp) with monthly subscription models. Other examples of such sites include large file distribution websites (allowing you to "e-mail" large files using a third-party website) and premium features on certain websites, for example Get Satisfaction. 

Find all your apps and discover new ones

Google Play has over 1.3 million apps to choose from so you can find all your favorites and discover new ones. For apps where you’re a subscriber, like Netflix, you can simply transfer your service over to Android for free. Just download the same app from Google Play and sign in to your app.

Creating an Android Project

An Android project contains all the files that comprise the source code for your Android app.
This lesson shows how to create a new project either using Android Studio or using the SDK tools from a command line.
Note: You should already have the Android SDK installed, and if you're using Android Studio, you should also haveAndroid Studio installed. If you don't have these, follow the guide to Installing the Android SDK before you start this lesson.

Create a Project with Android Studio


  1. In Android Studio, create a new project:
    • If you don't have a project opened, in the Welcome screen, click New Project.
    • If you have a project opened, from the File menu, select New Project.
  2. Figure 1. Configuring a new project in Android Studio.
  3. Under Configure your new project, fill in the fields as shown in figure 1 and click Next.
    It will probably be easier to follow these lessons if you use the same values as shown.
    • Application Name is the app name that appears to users. For this project, use "My First App."
    • Company domain provides a qualifier that will be appended to the package name; Android Studio will remember this qualifier for each new project you create.
    • Package name is the fully qualified name for the project (following the same rules as those for naming packages in the Java programming language). Your package name must be unique across all packages installed on the Android system. You can Edit this value independently from the application name or the company domain.

Building Your First App

Welcome to Android application development!
This class teaches you how to build your first Android app. You’ll learn how to create an Android project and run a debuggable version of the app. You'll also learn some fundamentals of Android app design, including how to build a simple user interface and handle user input.

Set Up Your Environment


Before you start this class, be sure you have your development environment set up. You need to:
  1. Download Android Studio.
  2. Download the latest SDK tools and platforms using the SDK Manager.
Note: Although most of this training class expects that you're using Android Studio, some procedures include alternative instructions for using the SDK tools from the command line instead.
This class uses a tutorial format to create a small Android app that teaches you some fundamental concepts about Android development, so it's important that you follow each step.

History of Computers

The computer was born not for entertainment or email but out of a need to solve a serious number-crunching crisis. By 1880, the U.S. population had grown so large that it took more than seven years to tabulate the U.S. Census results. The government sought a faster way to get the job done, giving rise to punch-card based computers that took up entire rooms.
Today, we carry more computing power on our smartphones than was available in these early models. The following brief history of computing is a timeline of how computers evolved from their humble beginnings to the machines of today that surf the Internet, play games and stream multimedia in addition to crunching numbers.
1801: In France, Joseph Marie Jacquard invents a loom that uses punched wooden cards to automatically weave fabric designs. Early computers would use similar punch cards.

Advanced Unity Particles Tutorial


This tutorial covers the more advanced ways to use the Unity particle systems. It lasts for just under 30 minutes and demonstrates how to use particles as triggers, as bullets, as water, as gas, and as a flickering lamp flame. In addition, it covers how to control the forces and behaviors of particles using scripting. Now with improved sound quality, larger text size, and updated projects!
Includes:
  • Five Examples (Star Wind, Lamp Cave, Particle Cannon, Fluid Effects, Particle Trigger)
  • Seven Scripts
  • Nine Models
  • Ten Particle Effects
  • One Sound
  • Eight Textures

PHP - Predefined Variables

PHP provides a large number of predefined variables to any script which it runs. PHP provides an additional set of predefined arrays containing variables from the web server the environment, and user input. These new arrays are called superglobals −
All the following variables are automatically available in every scope.

PHP Superglobals

VariableDescription
$GLOBALSContains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables.
$_SERVERThis is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these. See next section for a complete list of all the SERVER variables.
$_GETAn associative array of variables passed to the current script via the HTTP GET method.
$_POSTAn associative array of variables passed to the current script via the HTTP POST method.

PHP & MySQL

PHP will work with virtually all database software, including Oracle and Sybase but most commonly used is freely available MySQL database.

What you should already have ?

  • You have gone through MySQL tutorial to understand MySQL Basics.
  • Downloaded and installed a latest version of MySQL.
  • Created database user guest with password guest123.
  • If you have not created a database then you would need root user and its password to create a database.
We have divided this chapter in the following sections −

PHP - Variable Types

The main way to store information in the middle of a PHP program is by using a variable.
Here are the most important things to know about variables in PHP.
  • All variables in PHP are denoted with a leading dollar sign ($).
  • The value of a variable is the value of its most recent assignment.
  • Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right.
  • Variables can, but do not need, to be declared before assignment.
  • Variables in PHP do not have intrinsic types - a variable does not know in advance whether it will be used to store a number or a string of characters.
  • Variables used before they are assigned have default values.
  • PHP does a good job of automatically converting types from one to another when necessary.
  • PHP variables are Perl-like.
PHP has a total of eight data types which we use to construct our variables −
  • Integers − are whole numbers, without a decimal point, like 4195.
  • Doubles − are floating-point numbers, like 3.14159 or 49.1.
  • Booleans − have only two possible values either true or false.
  • NULL − is a special type that only has one value: NULL.
  • Strings − are sequences of characters, like 'PHP supports string operations.'
  • Arrays − are named and indexed collections of other values.
  • Objects − are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
  • Resources − are special variables that hold references to resources external to PHP (such as database connections).
The first five are simple types, and the next two (arrays and objects) are compound - the compound types can package up other arbitrary values of arbitrary type, whereas the simple types cannot.
We will explain only simple data type in this chapters. Array and Objects will be explained separately.

PHP Tutorial

The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications. This tutorial helps you to build your base with PHP.

Audience

This tutorial is designed for PHP programmers who are completely unaware of PHP concepts but they have basic understanding on computer programming.

Prerequisites

Before proceeding with this tutorial you should have at least basic understanding of computer programming, Internet, Database, and MySQL etc is very helpful.

Execute PHP Online

For most of the examples given in this tutorial you will find Try it an option, so just make use of this option to execute your PHP programs at the spot and enjoy your learning.

Implementing animations

It's very simple to implement this solution. We will use .css() to change the background properties and a simple setInterval to change the current frame of  the animation. Therefore, let's say that we have a sprite sheet containing 4 frames  of a walk cycle where each frame measures 64 by 64 pixels. First, we simply have to create a div with the sprite sheet as its background. This div should measure 64 by 64 pixels, otherwise the next frame would leak onto the current one. In the following example, we add the sprite to a div with the ID mygame.
$("#mygame").append("<div id='sprite1'>"); $("#sprite1").css("backgroundImage","url('spritesheet1.png')"); As the background image is by default aligned with the upper-left corner of the div, we will only see the first frame of the walk-cycle sprite sheet. What we want is to be able to change what frame is visible. The following function changes the background position to the correct position based on the argument passed to it. Take a look at the following code for the exact meaning of the arguments:

Chaining animations

The .animate() function from jQuery allows you to make a property vary through time from the current value to a new one. A typical effect, for example, would be to move it left from 10 pixels, or change its height. From what you've seen earlier and experienced for other type of functions, you may expect the following code to make  a div (DOM division element) move diagonally to the position left = 200px and top = 200px.
$("#myElementId").animate({top: 200}).animate({left: 200}); However, it doesn't! What you will see instead is the div first moves to reach top = 200px and only then moves to left = 200px. This is called queuing; each call to animate will be queued to the previous ones and will only execute once they're all finished. If you want to have two movements executed at the same time, thereby generating a diagonal movement, you'll have to use only one call to .animate().
$("#myElementId").animate({top: 200,left: 200}); Another possibility is to explicitly tell the .animate() function not to queue  the animations:
$("#myElementId").animate({top: 200}).animate({left: 200},{queue: false}); Keep in mind that this also applies to other functions that are in fact wrappers around the .animate() function, such as the following: • fadeIn(), fadeOut(), and fadeTo() • hide() and show() • slideUp() and slideDown

jQuery for Games

Over the course of the last few years, jQuery has almost become the default framework for any JavaScript development. More than 55 percent of the top 10,000 most visited websites as well as an estimated total of 24 million websites on the Internet are using it (more at http://trends.builtwith.com/javascript/ JQuery). And this trend doesn't show any sign of stopping. This book expects you to have some prior experience of jQuery. If you feel that you don't meet this requirement, then you could first learn more about it in Learning jQuery, Jonathan Chaffer, Karl Swedberg, Packt Publishing. This chapter will quickly go through the peculiarities of jQuery and will then dive deeper into its most game-oriented functions. Even if you probably have already used most of them, you may not be familiar with the full extent of their capabilities. The following is a detailed list of the topics addressed in this chapter: • The peculiarities of jQuery • The function that will help you for moving elements around • Event handling • DOM manipulation