Link Intelligently
We're going to take that looping power we just learned and use it in a more practical example. But before
we proceed here, you should know how to work with forms, the querystring, and the GET and POST
methods. Jay covered this not long ago, so go take a look at his article if this is unfamiliar to you.
Right now I'm going to work with the querystring. As you should know, there are three ways to get
information into the querystring. The first is to use the GET method in a form. The second is to type the
information into the URL on your browser. And third, you can embed a querystring in a standard link. Just
make the anchor tag look something like this: <a href="http://my_machine/mypage.php3?id=1">. We're
going to use this technique right now.
First off, lets query our database again and list the employee names. Look at the following script. Much of
this should look pretty familiar by now.
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$result = mysql_query("SELECT * FROM employees",$db);
if ($myrow = mysql_fetch_array($result)) {
do {
printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"],
$myrow["first"], $myrow["last"]);
} while ($myrow = mysql_fetch_array($result));
} else {
echo "Sorry, no records were found!";
}
?>
</body>
</html>
Everything's about the same except the printf function, so let's look at it in some detail.
First notice that each quotation mark is preceeded by a backslash. The backslash tells PHP to display the
character following it, rather than treat it as part of the code. Also note the use of the variable
$PHP_SELF. This variable, which stores the script's name and location, is passed along with every PHP
page. It's helpful here because we just want this file to call itself. Using $PHP_SELF, we can be sure that
will happen, even if the file is moved to another directory - or even another machine.
As I just mentioned, these links will recall the page. On the second time through, however, some
information will be added to the querystring.
PHP does a nifty thing when it sees a name=value pair in the querystring. It automatically creates a
variable with the name and value the querystring indicated. This feature allows us to test if it's the first or
second time through this page. All we have to do is ask PHP if the variable $id exists.
Once I know the answer to that question, I can display a different set of information the second time
through. Here's how:
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
// display individual record
if ($id) {
$result = mysql_query("SELECT * FROM employees WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
printf("First name: %s\n<br>", $myrow["first"]);
printf("Last name: %s\n<br>", $myrow["last"]);
printf("Address: %s\n<br>", $myrow["address"]);
printf("Position: %s\n<br>", $myrow["position"]);
} else {
// show employee list
$result = mysql_query("SELECT * FROM employees",$db);
if ($myrow = mysql_fetch_array($result)) {
// display list if there are records to display
do {
printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"],
$myrow["first"], $myrow["last"]);
} while ($myrow = mysql_fetch_array($result));
} else {
// no records to display
echo "Sorry, no records were found!";
}
}
?>
</body>
</html>
This code is getting complex now, so I've started to use comments to keep track of what's going on. You
can use // to make a single-line comment or /* and */ to start and end a large comment block.
And there we have it: your first truly useful PHP/MySQL script! Now let's take a look at how to plug forms
into it and send information back into the database.
0 Comments:
Post a Comment