Jesus 'n Jim
A mainly PC (some Mac) site w/Software, Computer Repair Info, How-To's on Using Computers
Technical Support 1-360-521-2060 (my business line cell)

PHP Programming

 
server-side scripting lesson...  you can print it out.  cheaper than a book.  paste it all into dreamweaver. it will look prettier.
all lines/statements in PHP end with a semicolon.
 
<?php
 
//this is a comment. it does nothing. 
//your PHP code between the <?php ?> above & below
 
?>
 
 
to print some text to the browser, (\n is a line break that the browser ignores but looks nice when you view the source code)
<?php
echo "this is a line\n";
//or
print "this is a line\n";
?>
 
to print a link to the browser,
<?php print "<a href=\"index.html\">home page</a>"; ?>
 
in PHP, = is always assignment. == and === are equality tests.
variables/arrays always start with a $ and arrays have [] on them. example:
$variable $array[123] $array['junk']
 
in a string (anything between ""), PHP reads it unless it hits a $ that doesn't have a \ in front of it, then it thinks it has found a variable name. then PHP tries to expand the variable it has found. for instance, if $m="Joe", the string "abc$m def." will come out abcJoe def.  if you can't afford to embed Joe with that space, try "abc${m}def." instead. By the way, in a string, you can 'escape' " $ \ [] {} and other interpreted characters with a \. this makes them a simple character instead.
 
to generate a table of cities from a database (sorted), insert this into your html:
<?php
print "<table>\n";
$q=mysql_query("SELECT city_name FROM cities ORDER BY city_name");
while ($row=mysql_fetch_assoc($q)) {
    print "<tr><td>$row[city_name]</td></tr>\n";
}
print "</table>\n";
?>
 
mysql_query() can select(grab), insert, update, create, or delete  data, users, & tables at the database.  if you grab data from a database, you will probably get a lot of rows (records) of data.  which is why we have...
 
mysql_fetch_assoc() is used to fetch a record/row from the query you just made.  you only use a fetch on a SELECT statement because it's the only statement that would return a bunch of rows.
 
a query you will generally use looks like this, and yes perens are important:
SELECT columns_separated_by_commas FROM tablenames_separated_by_commas WHERE some_condition ORDER BY columnsagain
or
INSERT INTO tablename(columnnames) VALUES (values_here)
or
UPDATE tablename SET columnname=value,columnname=value,columnname=value
DELETE FROM tablename WHERE some_condition
DELETE FROM tablename WHERE columnname='value'
DELETE FROM tablename WHERE columnname>value AND columnname < 3
 
in a condition, an = is used to test equality.  in an UPDATE...SET, = is an assignment statement.  that is SQL language, used in MySQL database.  it is not PHP.
 
this complicated thing is a form I used to send some email.
<?php
  if (!(isset($_POST['email']) && isset($_POST['text']))) { // incomplete email form data. don't send anything. show form.
        ?>
  <form action="index.php" method="post" enctype="multipart/form-data">
  <h2>E-mail us</h2>
  Your Email Address:<input name="email" type="text" size=40><br>
  Your email to us below:<br>
  <textarea name="text" cols="50" rows="4" wrap="virtual"></textarea><br>
  <input name="" type="submit" value="Send">
  </form>

  <?php
  } else { // full email form filled. attempt to send email.
   if (TRUE==mail("nobody@msn.com", "Webpage Comments", $_POST['text'],
       "From: $_POST['email']\r\n"
       ."Reply-To: $_POST['email']\r\n"
       ."X-Mailer: PHP/" . phpversion())) {
    echo "<div style=\"color:red;\">The email was successfully sent.</div>\n";
   } else {
    echo "<div style=\"color:red;\">There was a problem sending the email. Sorry  Please try again.</div>\n";
   ?>
    <form action="index.php" method="post" enctype="multipart/form-data">
    <h2>E-mail us</h2>
    Your Email Address:<input name=\"email\" type="text" size=40><br>
    Your email to us below:<br>
    <textarea name="text" cols=\"50\" rows="4" wrap="virtual"></textarea><br>
    <input name="" type="submit" value="Send">
    </form>

   <?php
   }
  }
  ?>

  
 
isset() tests to see if the variable was ever set to something or not. if it was, it returns true.
 
if statements are pretty simple:
if (condition_here_is_true) {
    do somethign here when true;
} else {
    do something here when false;
}
 
to make something not true, put a ! in front of it, even if it's an expression with perens around it. example:
<?php
if (!isset($_POST['text'])) { //no message body!
    echo "cookies tossed.";
} else { //message is in form variable $_POST['text']
    mail("nobody@msn.com","no subject",$_POST['text']);
}
?>
 
Oh - you didn't know about form variables? $_GET[] is for form fields grabbed from the URL, and $_POST[] is for form fields posted the hidden way.   If your server has an old version of PHP, you may have to get by with the longer $HTTP_POST_VARS[] and $HTTP_GET_VARS[], which *always* work.