Creating a Shopping Cart Class

For the second real-world use of object-oriented programming, I want to implement as a class the shopping cart system created in Chapter 5, E-Commerce Techniques. If you look back at cart.php Script 5.7 , you'll see that the one page does several things Updates the quantities in the cart Removes items from the cart when the user enters a quantity of 0 Displays the shopping cart in an HTML form In an object-oriented structure, this can be implemented in many ways. You could start with an...

My Security Anecdote

In the summer of 2006, I revamped my company's Web site www.DMCInsights.com , using a modularized layout, to a small extent. Literally the first night the new version of the site went live, someone tried hacking the server by changing URLs like about.php i phpmysql2 into about.php i http somesite.com file.txt. The file.txt script on that other server contained PHP code that would reveal every file on my server. Had it run, my site's security would have been compromised. The attempt did not work...

Sorting multidimensional arrays

Sorting arrays is easy using PHP, thanks to the sort , ksort , and related functions. You can sort a one-dimensional array by key, by value, in reverse order, etc. But these functions will not work on multidimensional arrays not as you'd probably like, at least . Say you have an array defined like so a array array 'keyl' gt 940, 'key2' gt 'blah' , array 'keyl' gt 23, 'key2' gt 'this' , array 'keyl' gt 894, 'key2' gt 'that' This is a simple two-dimensional array an array whose elements are also...

Creating an RSS Feed

RSS, which stands for Really Simple Syndication it used to mean Rich Site Summary or RDF Site Summary , is a way for Web sites to provide listings of the site's content. Normally this list contains at least the titles of articles, plus their descriptions and by article, think of any type of content that a site might offer . Users access these feeds using an RSS client many Web browsers support RSS, as well . If they want to read more of an article, there's a link to click, which takes them to...

To create the configuration file:

1. Begin a new PHP script in your text editor or IDE Script 2.1 . lt php Script 2.1 - config.inc.php Script 2.1 The configuration file is the key back-end script. It defines site-wide constants and dictates how errors are handled. 1 lt php Script 2.1 - config.inc.php 5 Created by Larry E. Ullman of DMC Insights, Inc. 6 Contact LarryUllman DMCInsights.com, http www.dmcinsights.com 7 Last modified November 8, 2006 9 Configuration file does the following things 10 - Has site settings in one...

Php-gtk

GTK , which stands for GIMP Tool Kit GIMP being the GNU Image Manipulation Program , is an open-source toolkit that works on multiple platforms. This resource makes it easy for programmers to generate graphical elements windows, buttons, and so on for their applications. The GTK home page, www.gtk.org, describes the system's origins and usage but from a C programming perspective GTK, like PHP, is written in C . The PHP-GTK Web site, http gtk.php.net Figure 10.10 , discusses how GTK can be used...

To use cURL:

1. Create a new PHP script in your text editor or IDE, beginning with the HTML Script 9.4 . lt DOCTYPE html PUBLIC - W3C DTD lt html xmlns http www.w3.org 1999 xhtml xml lang en lang en gt lt meta http-equiv content-type content text html charset iso-8859-1 gt lt title gt Using cURL lt title gt lt head gt lt body gt url 'http localhost login.php' curl curl_init url You don't have to assign the URL to use to a variable prior to the curl_init line, of course. But do make sure you change this URL...

Modularizing a Web Site

In my experience, the arc of a programmer's development starts with writing one-page applications that do just a single thing. These turn into two-page tools, and eventually into multipage sites, using templates, sessions, and or cookies to tie them all together. For many programmers, though, the arc is actually a bell curve. After more and more experience, the seasoned PHP developer starts doing the same amount of work in fewer pages, like having the same script both display and handle a form....

To create the database: 2

1. Connect to your server via a command-line interface and connect to MySQL. If you'd rather, or are forced to, use phpMyAdmin or whatever other tool instead, that's fine. 2. Create the database. CREATE DATABASE ecommerce 3. Choose the database Figure 5.1 . USE ecommerce 4. Create the customers table Figure 5.2 . CREATE TABLE customers customer_id INT UNSIGNED NOT NULL AUTO_INCREMENT, email VARCHAR 40 NOT NULL, pass CHAR 40 NOT NULL, first_name VARCHAR 20 NOT NULL, last_name VARCHAR 30 NOT...

Testing the application

That's it You've now created an entire Ajax application from scratch. You have created four files These should all be in the same folder in your Web directory. Now, to test the applica-tion but first If yours does not work as it should as it does in the following figures , you'll need to use some good old-fashioned debugging work to solve the problem. I discuss how to debug Ajax applications at the end of this chapter. Figure 13.16 The result without reloading the Web page if a username is...

Filtering and validating form data

In the quickform.php example Script 12.3 , I show how to start using QuickForm to create an HTML form. Those steps generate the HTML required to make the elements. But that s just a drop in the bucket as for what QuickForm can do. Commonly you ll want to apply some sort of filter to your form data. The applyFilter method does this, taking the name of the element to which this should be applied and the function to apply as its arguments form- gt applyFilter element_name , function_name To have...

Parsing XML with Expat

Using Expat with PHP is a four-step process 2. Identify the functions to use for handling events. 4. Free up the resources used by the parser. The first step is accomplished using The second step is the most important. Because Expat is an event-handler parser, it makes use of callback functions when encountering events. The primary events that occur are reading You need to tell PHP what user-defined functions should be called when each of these events occurs. For the opening and closing tags,...