Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Get AMPed over Uniform Server and Build a Data-driven Web Site in Nothing Flat : Page 3

Web development gets easier all the time, but Uniform Server, an open source project that packs up the ease and power of the 'AMP' platform, takes simplicity to the edge of embarrassing. Go from zero to Web hero in minutes by using our sample blog project to teach yourself the ease and power of AMP.


advertisement
Auto-Generating the Entry Form
Listing 1 showed the entry form for your blog, which was just fine, except that it didn't have the facility to set the category. You might think that it is easy to just add another form field that contains a list of the categories and you can take it from there, but there would be a problem with this. You would need to hard-code the categories into the page, so if the database ever changed, then you would also need to go back and change your page. A far more efficient way of doing this is to have a PHP script generate the HTML for you. This is a lot easier than it sounds!

Figure 7. Look Mom: It's your first PHP page.

The first thing to do is to get an instance of your page and save it as a PHP file instead of an HTM file. (For this example I called it BlogForm.php.) You can then put it on your Web server by copying it to the 'www' directory. You can then browse to this form using the http://localhost/blogform.php URL. The result is shown in Figure 7. You've just written your first PHP page! Congratulations.

But hang on a second: You might think that all you did was write an HTML page and call it a PHP page. Well you would be right. This is because the PHP parser handles HTML, and in fact works best in these cases by taking the HTML and allowing you to mark it up with information generated on the fly.

Consider the example, where you want to add category information to this page. You would put a form menu object on the page between the blog entry and the pin (see Figure 7). The code for this object would look something like:

<select name="select"> <option value="1">Test1</option> <option value="2">Test2</option> <option value="3">Test3</option> </select>

In PHP we can generate this HTML using this code:

<select name="select"> <? for($lp=1;$lp<4;$lp++) { ?> <option value="<?php echo($lp); ?>">Test<?php echo($lp); ?></option> <? } ?> </select>

At first this may look like gibberish but if you note that the code between the <? And the ?> is considered to be server code and the rest to be client side HTML, you will see it take shape.

First you output the <select> tag, which is identical to the category object code above. Then you go through a three-iteration loop, in which you output the <option> tag, with the value set to the loop variable, and the content to the string 'Test,' prefixing the loop variable. When you run the page in your browser you will see that the results are the same!

The next step is to read the values from the database and to loop through them, outputting the menu selector:


<?php $dbconn = @mysql_connect('localhost', 'root', 'rootp'); if (!$dbconn) { die('Error connecting to DB!'); } if (! @mysql_select_db('blog') ) { die( '<p>Unable to locate the main ' . 'database at this time.</p>' ); } $query = "SELECT * from Categories"; $result = @mysql_query($query); ?> <select name="sCategory"> <? while ($row = mysql_fetch_array($result)) { ?> <option value="<?php echo($row['ID']); ?>"> <?php echo($row['Description']); ?></option> <? } ?> </select>


Figure 8. What to Say?: The finished HTML form is driven by PHP.
This snippet demonstrates using the @mysqlconnect, @mysql_select_db, @mysql_query, and @mysql_fetch_array commands to connect to a MySQL server, select a database on that server, run a query on that database, and load the resultset into an in-memory array, respectively. It then outputs the contents of that array to the selection box within a while loop.

This can then be added to the Form, to produce a data-driven form. This is available in the download as blogform.php (see Figure 8).

Processing the Results
When you submit the form, the browser sends the value of the contents of its form controls to the destination as POST variables. This can be seen on the very first line of Listing 1, where the form method is POST and the destination is blogentr.php.

The destination file receives these POST variables in the HTTP conversation, and as (in this case) it is PHP, it can retrieve them using the $_POST array. The code to handle these variables and to use them to generate an INSERT query that puts the information into the database is shown below:

$Headline = $_POST['tHeadline']; $Body = $_POST['tBody']; $Category = $_POST['sCategory']; $PIN = $_POST['tPin']; if($PIN=="1234") { $SQL = "INSERT INTO blogs (category, headline, body) VALUES ('"; $SQL = $SQL . $Category . "','"; $SQL = $SQL . $Headline . "','"; $SQL = $SQL . $Body . "')" ; if (@mysql_query($SQL)) . . } ?>

The entire PHP script is available from the download. This includes checks for connectivity to the database and valid SQL.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap