Showing posts with label php lessons. Show all posts
Showing posts with label php lessons. Show all posts

Friday, December 14, 2012

PHP Database Function - PHP For Beginners Lesson 12


More Functionality with a database
By Stephon Unomon
Google +

 

       Nowadays you hardly ever hear PHP mentioned without MySQL close behind. Why? Well, PHP and MySQL make a great pair for web application development efforts. MySQL is often used in conjunction with PHP for the same reasons PHP is so popular - it is free, widely available, and most web hosts have it installed. There are other database systems that PHP works with, but MySQL seems to be the most common.

 

       In this section, I’ll familiarize you with using MySQL to connect to a MySQL database and perform the basics. Advanced MySQL and it’s SQL language is a little beyond the scope of this book, so we won’t dive very far in to heavy details (I don’t want to lose you!). However, MySQL’s commands are written in almost-plain-english, and you can usually figure it out just by looking at it.

 

       Most other PHP/MySQL books also tell you how to install MySQL and configure it. We’re not going to do that here. Your web host most likely has MySQL available for you, and they have simple ways to create databases. The two hosts that I have been using for years are pair Networks and Hostgator. Hostgator uses CPanel, and pair Networks uses a custom built control panel - both make it very easy to set up a database, and they provide the information you need to connect to it with.

 

So, let’s get connected!

With This:

 

Thursday, December 13, 2012

Processing PHP Forms - PHP For Beginners Lesson 11

PROCESSING FORMS
By Stephon Unomon
Google +

KoolPHPSuite.Com
 

       What is one thing almost everyone wants to do with their website? Have a contact form. And, it isn’t as difficult as you’d think. We’re going to use the PHP mail() command to make one.

 

Make an HTML form with Name, Email and Message fields. Take the code below and place it in a PHP file to use as your action.

 

<?php

mail("myaccount@myisp.com", "Form Feedback", " // your address and subject

Name: $name

Email: $email

Message: $message

", "From: $email");

// Display results

echo("

<html>

Name: $name

Email: $email

Message: $message

</html>

");

?>
 
 
Stay Tuned For The Section 2 Overview & Quiz.....
And Of Course More PHP Lessons
 

Wednesday, December 12, 2012

Cookies Are Good - PHP For Beginners Lesson 10


Cookies (not just for your sweet tooth)
By Stephon Unomon
Google + 

 

You are hopefully familiar with Cookies - you get them almost anytime you visit a website. Cookies allow you to store information about your visitor’s session on their computer. 

 

One of the most common uses of cookies is to store usernames. That way when a viewer returns to your site, they don’t have to log in each time they visit.

 

You can create cookies in PHP with:

 

<?php

setcookie(name, value, expiration);

?>

There are 3 requirements when setting a cookie:

 

  Name:  This is where you set the name of your cookie so you can retrieve it later.

  Value:  The value you wish to store in your cookie.  Some common values are usernames or date last visited.

  Expiration:  When your cookie will be deleted from the user’s computer.  If you do not set an expiration date, the cookie will be deleted when the viewer closes their browser!

 

Getting information from a cookie is just about as easy as setting one. Remember the “ISSET” stuff a few minutes ago? PHP gets cookie data in a similar way, by creating an associative array (Yes, again!) to store your retrieved cookie data...using the $_COOKIE variable.

The array key is what you named the variable when it was set (so, obviously you can set any number of cookies!) So...

 

<?php

if(isset($_COOKIE['username'])){ // If there’s a cookie...

$username = $_COOKIE['username']; // set the variable...

echo "Welcome back, $username."; // and show the data in the cookie...

} else {

echo "No username was found. Sorry!";} // or show this if there wasn’t one.

?>

 

And, FYI...Cookies are stored as small text files on your computer (in case you did not know!)

 

Our next lesson will be...one of the most useful functions of PHP... find out tommorow!

PHP Tree View

Sunday, December 9, 2012

PHP Sessions - Advanced PHP For Beginners Lesson 9


PHP SESSIONS
By Stephon Unomon

 

       When your websites start to become more advanced and you find that you have a need for specific user data to be available throughout different pages on your website (think shopping cart!), it’s time for Sessions!

 

Starting a session is a snap.

 

<?php

session start();

?>

The webserver will attach a really really long random “session ID” to indicate a unique session. It looks something like: a8486dd2a3eacc136bd44ca653d8c5a2

 

A session isn’t worth a hill of beans unless we can store data in it. Fortunately, PHP can do this for us with an associative array, based on the $_SESSION variable. (Is some of this starting to come together?)

 

Let’s make a login form based on sessions!

 

<?php

session_start(); //  Starts a PHP session

echo "<form method=POST action=index.php>

  User Name: <input type=text name=\"username\">

  Password: <input type=text name=\"password\">

  <input type=submit>

  </form>";  // This is the HTML form

$_SESSION['username']=$_POST["username"]; // Enters the username into the array

$_SESSION['password']=$_POST["password"]; // Enters the password into the array

?>

 

Your username and password are now stored in an array that will last until the session is “un-set”.

 

Removing a session is done when either: The viewer closes their browser, or PHP runs the command to un-set a session, aka “destroy”.

 

You can think of it like this - your session is an Etch-a-Sketchtm that has information drawn on it. It’s there until you shake it!

 

<?php

session_destroy();

?>

Yes, it’s a tad violent...but gets the job done. This will clear out any data associated with the current session.

 

So, what if I want to remove specific data from a session without deleting the whole thing?

 

This can be done with an IF statement combined with  commands called “ISSET” and “UNSET”.

 

<?php

if(isset($_SESSION['items'])){ 

unset($_SESSION['items']);}

?>

 

So, logically...

 

If this specific key is set in this session, remove the key’s data.

The Best PHP Editor

Saturday, December 8, 2012

Introduction To Advanced PHP - Read Before Lesson 6

You Made It Past The Basics - Congratulations!
Please read over lessons 1-5 before starting this section.
By Stephon Unomon

 

       By now, you’ve got enough PHP knowledge under your belt to add basic PHP functionality in all of your websites. It’s pretty cool knowing things that a lot of others don’t know, and they’ll never be able to find out just by doing a “View Source” on your website (especially since you cant actually SEE PHP code by viewing the source in a browser!)

 

       In this section we are going to cover some more advanced PHP code. Things that you might not use just yet but once you are comfortable with PHP and want to get more out of it, you’ll be ready, my young apprentice.

 

       Quick tangent...before we get started here you are going to want to be able to place comments in your code. Why? It’s a heck of a lot easier to know why you wrote a specific line of code if you add comments...so you don’t come back in 3 months and ask yourself, “Was I drunk when I wrote this code??” Here are some examples of comment codes:

 

<?php

// This is a single comment line

# This is also a single comment line

/* This is a block comment, useful if you are working with a multi-line comment or are writing a story on your page that you don’t want people to see */

?>

You don’t have to use semicolons after each line, since the PHP server ignores comments because they aren’t actually commands. Now, on to more CODE! (ta-daaaa!)
 

PHP Lesson 1-5 Overview and Quiz

Section One Overview...
By Stephon Unomon

 

While we have not covered the entirety of basic PHP commands, the ones we have covered are among the most common and can be used in almost any web project.

 

ECHO: Outputs to the screen.

STRING: Content within a PHP command.

VARIABLES: Something equals something else.

DATE: A way to display date/time.

INCLUDE: Pull in the contents of another file.

REQUIRE: Require the contents of another file.

HEADER: Redirects to new file / URL.

 

 

And now....for a...

 

...QUICK QUIZ!!!

 

1. What is the difference between INCLUDE and REQUIRE?

 

 

 

 

2. Can we ECHO words with quotation marks? How?

 

 

 

 

3. What is a VARIABLE used for?


PHP UI Components

PHP Echo Command - PHP Lesson 1

The PHP Echo command
By Stephon Unomon

We’re going to tell PHP to output something to the screen. Keep in mind that PHP can be used in conjunction with HTML, but we are not showing the code in this example, to keep it simple:

 

<?php echo “Creamy Bagels”; ?>

 

(I hate the “Hello World” sample that every other book in the world uses, so I’m using “Creamy Bagels” instead.)

 

       Let’s dissect the command example bit by bit, shall we? It helps to do this when you are looking at a LOT of PHP code...because, trust me, it can look like a jumbled blurry mess sometimes if you don’t take it piece by piece!
 

<?php - tells the server to process this as php code...

 

echo “Creamy Bagels” ; - tells the server to write what’s in the quotes to the screen, and that the semicolon is ending this particular command...

 

?> - tells the webserver, “OK, I’m done with PHP for now. Back to regular HTML”.

 

Pretty simple when you look at it that way, yes?

 

 

“OK, that’s cool..but what if I want to see quotes on my screen?”

 

 

This can be done by “escaping” the PHP code for what you want to show up in quotes. Let’s use the example from above...

 

<?php echo “Creamy Bagels”; ?>

If you wanted to see quotes around Creamy Bagels, you would use the following code instead:

 

<?php echo “\”Creamy Bagels\””; ?>

Using \” tells PHP that you want a quotation mark to appear. Remember this - it is important, and will be used in the later lessons.