PHP 5/MySQL Programming- P65 docx

5 286 0
PHP 5/MySQL Programming- P65 docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

298 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r The first step is determining if an XML file has been sent through the $theXML parameter. If not, a default value of main.xml is defined. This, of course, presumes that a copy of main.xml is available and properly formatted. If the program is called with some other XML file as its parameter, that file is interpreted again. I then attempt to open the XML file. If the simplexml_load_file command is unsuccessful, it returns the value FALSE. The program reports this failure if it occurs. If it does not fail, the program creates a page based on the parameters indicated in this file. I expect a page with five parameters ( top, css, title, menu, and content), but I could easily modify the program to accept as many parame- ters as you wish. I ignored the title parameter in this particular program version because I have the page title already stored in top.html. The program includes all the files indicated in the XML code, incorporating them in CSS styles when appropriate. Summary Content management systems can help automate your Web site’s creation. CMS tools allow you to build powerful multipart Web documents, combining pages with a single style and layout. You learned how to install and customize the pop- ular PHP-Nuke CMS. You also learned how to build a very basic CMS of your own using GET parameters to customize your page. You learned about XML and how to use the simpleXML tools to parse any XML files you encounter. Finally, you learned how to combine your newfound XML skills with CMS to build an XML-aware CMS. CHALLENGES 1. Install and configure PHP-Nuke or another CMS on your system. 2. Create a custom theme by analyzing and modifying an existing theme. 3. Modify simpleCMS with your own layout, images, and banner files. 4. Create an editor that allows the user to build XML pages for XCMS. 5. Write an XCMS module that allows authorized users to add new content (a news or guest book, for example). W hen you begin programming in PHP, you start with very simple variables. Soon you learned how to do more interesting things with arrays and asso- ciative arrays. You added the power of files to gain tremendous new skills. Now you learn how relational databases can be used to manage data. In this chapter you discover how to build a basic database and how to hook it up to your PHP programs. Specifically, you learn: • How to start the MySQL executable • How to build basic databases • The essential data definition SQL statements • How to return a basic SQL query • How to use phpMyAdmin to manage your databases • How to incorporate databases into PHP programs U s i n g M y S Q L t o C r e a t e D a t a b a s e s 9 CHAPTER Introducing the Adventure Generator Program Databases are a serious tool but they can be fun, too. The program shown in Fig- ures 9.1 through 9.4 shows how a database can be used to fuel an adventure game generator. The adventure generator is a system that allows users to create and play simple multiple-choice adventures. This style of game consists of several nodes. Each node describes some sort of decision. In each case, the user can choose from up to three options. The user’s choice leads to a new decision. If the user makes a sequence of correct choices, he wins the game. This program is interesting as a game, but the really exciting part is how the user can modify this game. A user can use the same system to create and modify adventures. Figure 9.3 shows the data behind the Enigma game. Note that you can edit any node by clicking the appropriate button from this screen. If the user chooses to edit a segment, the page shown in Figure 9.4 appears. 300 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r FIGURE 9.1 The user can choose an option. I’m hopping onto that sub As you can see, the data structure is the most important element of this game. You already know some ways to work with data, but this chapter introduces the notion of relational database management systems (RDBMS). An RDBMS is a sys- tem that helps programmers work with data. The adventure generator program uses a database to store and manipulate all the data. 301 C h a p t e r 9U s i n g M y S Q L t o C r e a t e D a t a b a s e s FIGURE 9.2 Maybe the warehouse would have been a better choice after all. FIGURE 9.3 This page provides information about each segment in the game, including links to directly edit each segment. Using a Database Management System Data is such an important part of modern programming that entire program- ming languages are devoted to manipulating databases. The primary standard for database languages is Structured Query Language (SQL). SQL is a standardized language for creating databases, storing information in databases, and retrieving information from databases. Special applications and programming environ- ments specialize in interpreting SQL data and acting on it. Often a programmer begins by creating a data structure in SQL, and then writes a program in some other language (such as PHP) to allow access to that data. The PHP program can then formulate data requests or updates, which are passed on to the SQL interpreter. This approach has a couple of advantages: • Once you learn SQL, you can apply it easily to a new programming language. • You can easily add multiple interfaces to an existing data set because many programming languages have ways to access an SQL interpreter. Many relational database management systems are available, but the MySQL environment is especially well suited to working with PHP. 302 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r FIGURE 9.4 From this screen it is possible to change everything about a node. All the nodes that have been created so far are available as new locations. . SQL statements • How to return a basic SQL query • How to use phpMyAdmin to manage your databases • How to incorporate databases into PHP programs U s i n g M y S Q L t o C r e a t e D a t a b a s e s 9 CHAPTER Introducing. structure in SQL, and then writes a program in some other language (such as PHP) to allow access to that data. The PHP program can then formulate data requests or updates, which are passed on to. users to add new content (a news or guest book, for example). W hen you begin programming in PHP, you start with very simple variables. Soon you learned how to do more interesting things with

Ngày đăng: 07/07/2014, 02:20

Mục lục

  • PHP 5 / MySQL Programming for the Absolute Beginner

    • Cover

    • Chapter 1: Exploring the PHP Environment

    • Chapter 2: Using Variables and Input

    • Chapter 3: Controlling Your Code with Conditions and Functions

    • Chapter 4: Loops and Arrays

    • Chapter 5: Better Arrays and String Handling

    • Chapter 6: Working with Files

    • Chapter 7: Writing Programs with Objects

    • Chapter 8: XML and Content Management Systems

    • Chapter 9: Using MySQL to Create Databases

    • Chapter 10: Connecting to Databases within PHP

    • Chapter 12: Building a Three-Tiered Data Application

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan