To insert a new record to the table you created in the previous page select the Insert link at the top of the page:
Showing posts with label php. Show all posts
Showing posts with label php. Show all posts
Saturday, 30 May 2015
Create a database with phpMyAdmin
You can create all of your database tables and queries using PHP code. But
before doing that, it's a good idea to get an understanding of just what it
is you'll be creating. If you're new to the world of databases, then here's
a simple primer.
A database is a way to store lots of information. You might want to store the
names and addresses of all your contacts, or save usernames and passwords for
your online forum. Or maybe customer information.
When you create a database, you're creating a structure like this:
What is a database and what do they look like?
A database is a way to store lots of information. You might want to store the
names and addresses of all your contacts, or save usernames and passwords for
your online forum. Or maybe customer information. When you create a database, you're creating a structure like this:
Getting values out of PHP functions
When you're creating your own functions, you may notice that they can be broken
down in to two categories: functions that you can leave, and just let them do
their jobs; and functions where you need to get an answer back. As an example,
here's the two different categories in action:
print ("Get on with it!");
$string_length = strlen($string_length);
$string_length = strlen($string_length);
Check for blank Textboxes with PHP
f you remember the script that we wanted to create earlier it was this:
- Get the text that a user entered in a textbox on a form
- Trim any blank spaces from the left and right of the text
- Check that what you have left is not a blank string
<?PHP
PHP Functions and Arguments
Arguments
Functions can be handed variables, so that you can do something with what's inside of them. You pass the variable over to your functions by typing them inside of the round brackets of the function name.
<?PHP
$error_text = "Error message";
display_error_message($error_text);
display_error_message($error_text);
function display_error_message($error_text) {
print $error_text;
PHP Functions
What is a Function?
A function is just a segment of code, separate from the rest of your code. You separate it because it's nice and handy, and you want to use it not once but over and over. It's a chunk of code that you think is useful, and want to use again. Functions save you from writing the code over and over. Here's an example.Suppose you need to check text from a textbox. You want to trim any blank spaces from the left and right of the text that the user entered. So if they entered this:
" Bill Gates
"
You want to turn it into this:Sorting PHP Array values
There may be times when you want to sort the values inside of an array. For
example, suppose your array values are not in alphabetical order. Like this
one:
$full_name = array();
$full_name["Roger"] = "Waters";
$full_name["Richard"] = "Wright";
$full_name["Nick"] = "Mason";
$full_name["David"] = "Gilmour";
$full_name["Richard"] = "Wright";
$full_name["Nick"] = "Mason";
$full_name["David"] = "Gilmour";
Geting Values from Arrays
Here's an example for you to try:
<?php
$seasons = array("Autumn", "Winter",
"Spring", "Summer");
print $seasons[0];
print $seasons[0];
?>
The array is the same one we set up before To get
at what is inside of an array, just type the key number you want to access.
In the above code, we're printing out what is held in the 0 position (Key) in
the array. You just type the key number between the square brackets of your
array name:
print $Array_Name[0];
How to Set up a PHP Array
First you type out what you want your array to be called ($Order_Number,
in the array above) and, after an equals sign, you type this:
You can use two basic methods to put something into an array.
array( );
So setting up an array just involves typing the word array followed by a pair
of round brackets. This is enough to tell PHP that you want to set up the array.
But there's nothing in the array yet. All we're doing with our line of code
is telling PHP to set up an array, and give it the name $Order_Number. You can use two basic methods to put something into an array.
PHP Arrays
You know what a variable is – just a storage area where you hold numbers
and text. The problem is, a variable will hold only one value. You can store
a single number in a variable, or a single string. An array is like a special
variable, which can hold more than one number, or more than one string, at a
time. If you have a list of items (like a list of customer orders, for example),
and you need to do something with them, then it would be quite cumbersome to
do this:
$Order_Number1 = "Black shoes";
The PHP break statement
There are times when you need to break out of a loop before the whole thing
gets executed. Or, you want to break out of the loop because of an error your
user made. In which case, you can use the break statement. Fortunately, this
involves nothing more than typing the word break. Here’s some not
very useful code that demonstrates the use of the break statement:
$TeacherInterrupts = true;
$counter = 1;
$counter = 1;
while ($counter < 11) {
PHP Do ... While loops
This type is loop is almost identical to the while loop except that the condition comes at the end:
do
statement
while (condition)
The difference is that your statement gets executed at least once. In a normal
while loop, the condition could be met before your statement gets executed.
PHP While Loops
Instead of using a for loop, you have the option
to use a while loop. The structure of a while loop is more simple than a for
loop, because you’re only evaluating the one condition. The loop goes round
and round while the condition is true. When the condition is false, the programme
breaks out of the while loop. Here’s the syntax for a while loop:
while (condition) {
statement
PHP For Loops
So what’s a loop then? A loop is something that goes round and round.
If I told you to move a finger around in a loop, you’d have no problem
with the order (unless you have no fingers!) In programming, it’s exactly
the same. Except a programming loop will go round and round until you tell it
to stop. You also need to tell the programme two other things - where to start
your loop, and what to do after it’s finished one lap (known as the update
expression).
You can programme without using loops. But it’s an awful lot easier with them. Consider this.
You want to add up the numbers 1 to 4: 1 + 2 + 3 + 4. You could do it like this:
You can programme without using loops. But it’s an awful lot easier with them. Consider this.
You want to add up the numbers 1 to 4: 1 + 2 + 3 + 4. You could do it like this:
$answer = 1 + 2 + 3 + 4;
Friday, 29 May 2015
PHP and HTML Checkboxes
Note one thing about the HTML checkbox elements: they all have different NAME
values (ch1, ch2 ch3, etc). When we coded for the Radio Buttons, we gave the
buttons the same NAME. That's because only one option can be selected with Radio
Buttons. Because the user can select more than one option with Checkboxes, it
makes sense to give them different NAME values, and treat them as separate entities
(but some advocate treating them just like Radio Buttons).
In your PHP code, the technique is to check whether each checkbox element has been checked or not. It's more or less the same as for the radio Buttons. First we set up five variable and set them all the unchecked, just like we did before:
In your PHP code, the technique is to check whether each checkbox element has been checked or not. It's more or less the same as for the radio Buttons. First we set up five variable and set them all the unchecked, just like we did before:
PHP and HTML Radio Buttons
Make sure you save your work as radioButton.php, as that's where
we're posting the Form – to itself.
To get the value of a radio button with PHP code, again you access the NAME
attribute of the HTML form elements. In the HTML above, the NAME of the Radio
buttons is the same – "gender". The first Radio Button has
a value of "male" and the second Radio Button has a value of female.
When you're writing your PHP code, it's these values that are returned. Here's
some PHP code. Add it to the HEAD section of your HTML:The HTML ACTION attribute and PHP
You don't have to submit your form data to the same PHP page, as we've been
doing. You can send it to an entirely different PHP page. To see how it works,
try this:
Create the following page, and call it basicForm2.php. This is your HTML. Notice the ACTION attribue.
Create the following page, and call it basicForm2.php. This is your HTML. Notice the ACTION attribue.
<html>
<head>
<title>A BASIC HTML FORM</title>
</head>
<head>
<title>A BASIC HTML FORM</title>
</head>
PHP Submit buttons
In the previous lesson you saw how to get text
from a textbox when a Submit button on a form was clicked. However, when you
first load the page the text still displays.
The reason why the text displays when the page is first loaded is because the script executes whether the button is clicked or not. This is the problem you face when a PHP script is on the same page as the HTML, and is being submitted to itself in the ACTION attribute.
The reason why the text displays when the page is first loaded is because the script executes whether the button is clicked or not. This is the problem you face when a PHP script is on the same page as the HTML, and is being submitted to itself in the ACTION attribute.
PHP and Text Boxes on HTML Forms
If you've been following along from the in the previous lesson
then your basicForm.php now has a METHOD and ACTION set. We're going
to use these to process text that a user has entered into a text box. The
METHOD attribute tells you how form data is being sent, and the ACTION
attribute tells you where it is being sent.
To get at the text that a user entered into a text box, the text box needs
a NAME attribute. You then tell PHP the NAME of the textbox you want to work
with. Our text box hasn't got a NAME yet, so change HTML TO THIS






