Jump to content

Php Minitutorial 2


Recommended Posts

Before I move on... I thought I'd get some feedback on how this info is perceived...

  • Useful
  • Boring
  • Etc....

This is pretty time intensive... so if no one's getting anything out of this... I'd be willing to stop.

 

Just reply to this thread if you have an opinion you'd like to share.

 

Thanks

Link to comment
Share on other sites

On to Part Two of PHP Basics...

 

Even if you have a popular WYSIWYG editor (Dreamweaver, etc.) creating forms with tons of options can be a drag. Especially if you find yourself creating the same form over and over again.

 

I'll share some ideas with you for saving time with forms.

 

I wanted to create a form for one of my sites that involved the user choosing the year of their birth. Obviously it wouldn't be too hard to create the entire form using html... but it would be tedious. I knew I'd have to go back to the 1940's just to be safe.

 

There are plenty of ways to save yourself time. Some would create some code in Javascript. But since this is a tutorial on PHP (and I'm not nearly as good at Javascript) let's look at one way to do it.

 

We want to use PHP to tell the server "Count backwards from 2003 to 1940 and create the necessary html code so I don't have to write it."

 

So let's look at a very watered down version of this form:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>Untitled Document</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

 

<body>

<form name="form1" method="post" action="another_script.php">

  <table width="100%" border="0" cellspacing="0" cellpadding="0">

    <tr>

      <td width="40%"> <div align="right">Year you were born</div></td>

      <td width="15"> </td>

      <td><select name="yob" id="yob">

        </select></td>

    </tr>

    <tr>

      <td colspan="3"><div align="center">

          <input type="submit" name="Submit" value="Submit">

        </div></td>

    </tr>

  </table>

</form>

</body>

</html>

Now we have nothing to show for the drop down select box... yet.

 

Here's what we do. We create a loop, have the server print out html code as it counts backward, and quite the loop when we reach 1940.

 

Let's look at the finished code and then dissect it:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>Untitled Document</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

 

<body>

<form name="form1" method="post" action="another_script.php">

  <table width="100%" border="0" cellspacing="0" cellpadding="0">

    <tr>

      <td width="40%"> <div align="right">Year you were born</div></td>

      <td width="15"> </td>

      <td><select name="yob" id="yob">

   <?php

   for($x=2003; $x>1939; $x = $x - 1)

   {

          print("<option value=\"$x\">$x</option>");

    }

    ?>

        </select></td>

    </tr>

    <tr>

      <td colspan="3"><div align="center">

          <input type="submit" name="Submit" value="Submit">

        </div></td>

    </tr>

  </table>

</form>

</body>

</html>

 

Step by step...

 

<?php starts the php code

for($x=2003; $x>1939; $x = $x - 1)
Tells the server

"Variable x is now 2003... stop the loop when it's greater than 1939... and each pass through the loop, reset variable x at one less than it's currentl value."

 

Or "Count backwards from 2003 to 1939 and do everything between the brackets {} while you're counting."

 

So, everything between the brackets gets done until the loop is finished ($x reaches 1940).

 

Inside the brackets we have:

print("<option value=\"$x\">$x</option>");

It looks close to html... but different. This is our familiar print function, but now we have slash marks (or whatever they're called) in front of the quotation marks.

 

This tells the server... whatever follow the \ is a symbol I really mean... and nothing else.

 

If we left them out, then the server would think our print statement was finished at the first quotation mark right after value=

 

But we want the print statement to cover the entire line... so we use the \ to tell the server "I really mean print a quotation mark."

 

The $x variable is whatever number we're on.

 

Save this code to a php file and upload it to your server to see that it really works. Isn't that a lot easier than typing 60+ lines of repetitive code?

 

I think so.

 

 

***Final note***

Don't bother hitting Submit on this form you created since the form will post to a php script that doesn't exist. If you hit submit, you'll get an error message. To avoid the error message, you could have the form POST to a real url. None of this has anything to do with the tutorial info except to prevent someone from thinking "This darn form doens't work". All it's supposed to do is list the dates in reverse order.

Link to comment
Share on other sites

Very cool stuff, both this tutorial and the last. Good to see some user submited content here that's helpful and fun to read! Only problem is instead of naming each one PHP tutorial 1, PHP tutorial 2, and so on, how about giving us a little one or two word definition like:

 

php tutorial 3: fun with includes!

 

That way if there are lots more or at least some more (crossing my fingers) it won't be so hard to navigate through them. B) Good job though, once again.. I really enjoyed it, and think you spoke in a lanuage people can actually understand!

 

Maybe next time, you can show people how to set static php headers and footers they can use on every page instead of having to update each one. I know thats one of my favorites. :unsure:

Link to comment
Share on other sites

Maybe next time, you can show people how to set static php headers and footers they can use on every page instead of having to update each one. I know thats one of my favorites.

 

Unless I misunderstand the request, I think I covered that at the end of the last tutorial.

 

If I misunderstood your request, please explain a bit more.

 

Thanks.

Link to comment
Share on other sites

I don't use your tutorials myself, since I've already progressed past the basic stage, but I think you're doing a fine job here!

 

It's always nice to see example codes that we can relate to practical and common usage. These code snippets would have been useful for people who are new to PHP.

 

Thanks, surefire! :unsure:

Link to comment
Share on other sites

{

          print("<option value=\"$x\">$x</option>");

    }

 

I need a little more help with breaking down this line. I know that it is saying "SAY THIS". I know that the \ escapes the quotation marks! My questions are:

 

1. If the quotation mark is escaped, why doesn't the year print with quotation marks around it?

 

2. I'm not familiar with the "print" function, although I see what it does. Is it an html tag, a form tag, or a php command?

 

That's all I have for now, but I'm sure I'll come up with more later!!! Thanks for all the time and effort, Surefire! I'm at least feeling a little more hopeful that I may learn this!

 

Tracy ;) ...feeling only a little less bewildered!! ;)

Link to comment
Share on other sites

Tracy, your question is a good one...

 

Both your questions can be answered at once. The print command is a PHP function (I think that's the right terminology) that tells the server to output certain code into your html.

 

So when you take the code we created and ftp it to your site, you see a well formatted html page when you view source.

 

Here's a piece of html code we asked php to help us create:

 

<form name="form1" method="post" action="another_script.php">

  <table width="100%" border="0" cellspacing="0" cellpadding="0">

    <tr>

      <td width="40%"> <div align="right">Year you were born</div></td>

      <td width="15"> </td>

      <td><select name="yob" id="yob">

  <option value="2003">2003</option>

<option value="2002">2002</option>

 

So, you are correct... the year does have quotes around it. But this is html we're writing. All we've asked php to do is to help us write the same html code we would've written by hand.

 

You don't see the quotes on your display because the html code you've written tells your browser... "Here's a drop down selection box, everything in quotes is a possible choice that you should show the user, and everything between > < is the corresponding value for that choice."

 

It's just html... nothing more.

 

But by using PHP's print function, a simple loop, and a little planning, we don't have to write all that html code ourselves. We get help from the server.

 

The important thing to understand is that PHP does it's work on the TCH server... then the server sends HTML code to your visitor. So no matter what you ask PHP to do... at the end of it all, html code is sent.

 

Which is why PHP code does neat tricks that everyone can see... the tricks are done before html code is sent over the internet.

 

Get it????

 

So don't think of switching from HTML to PHP... think of pumping up your HTML with a little PHP. You're still sending html to your visitors.

 

I mentioned in a previous post two styles of PHP coding...

 

Some like to write PHP code that asks the server to create almost every line of html.

Others (like me) write in html, and then insert snippets and blocks of php to spruce things up and make our lives easier.

 

No right or wrong... just different ways of achieving the same result: HTML with extra flair.

 

Hope this explanation helps.

Link to comment
Share on other sites

Jim,

 

I have contacted SureFire and we are arranging to include his PHP tutorials as part of the TCH Help Web Site.

 

To that end, if someone else has a knack for writing easy to use user instructions on a topic of value to the TCH user community, we could certainly explore the possibilities. For example, I think a small tutorial on how to properly publish to TCH using FrontPage would be of some benefit.

 

-kw

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...