Assalamu'alaikum dan selamat sejahtera
So,as previous post,i already give the PHP script ryte?so,this post,we will discuss on that script.Here is the script
<?PHP
$start = 1;
$times = 2;
$answer = array();
for ($start; $start < 11; $start++) {
$answer[$start] = $start * $times;
print (" answer = " . $answer[$start] . "<BR>");
}
?>:
So,just focus on this line:
$answer[$start] = $start * $times;
print (" answer = " . $answer[$start] . "<BR>");
As $start=1,then it will be look like this : $answer[$start]=$answer[1] and then we assign the value in the array,like this:
$answer[1] = 1 * $times;
and then,to print or get the value from the array,we just need print($answer[1]); ,as in our case,this line below will get the value from the array:
print (" answer = " . $answer[1] . "<BR>");
after that it will loop again ,and $start will update and it's value will be 2.
Instead use number to get the value from array.we also can use text,like this:
$group = array();
$group["alpha"]="Team One";
$group["beta"]="Team Backup";
and to get the value:
print($group["alpha"]);
that all for this post.
N/NOTE:any confusing?
Friday, February 4, 2011
Array-PHP
Assalamu'alaikum and selamat sejahtera
Okey,for this post,we will learn the array.What is array?click here for more.So,first of all i show to you on how declare the array in PHP.
the Declaration:
$Group = array( );
So,$Group is because i named it,well you can put any variable's name that you like.But,the important is,$Group already be an array because of "=array()".So,to assign the value to the array,look at this example below :
$Group = array();
$Group[]="Eagle";
$Group[]="Alpha";
$Group[]="Beta";
$Group[]="Charlie";
beside we also can do like this:
$Group = array("Eagle","Alpha","Beta","Charlie");
okey,as we assign this,PHP will assign this values to be like this:
$Group[0]=Eagle
$Group[1]=Alpha
$Group[2]=Beta
$Group[3]=Charlie
but,i want to be like this:
$Group[1]=Eagle
$Group[2]=Alpha
$Group[3]=Beta
$Group[4]=Charlie
well,its look common ryte?as usual,when we start to count ,we always start with one ryte?so,to make like this,follow this code below:
$Group = array();
$Group[1]="Eagle";
$Group[2]="Alpha";
$Group[3]="Beta";
$Group[4]="Charlie";
or
$Group = array(1=>"Eagle",2=>"Alpha",3=>"Beta",4=>"Charlie");
one more thing,if you want assign number instead of number,well here is the code:
$Group = array(12,34,65);
or
$Group = array();
$Group[]=22;
$Group[]=45;
so,we do not need the double quote,just straight away type what number that do you want to assign.To study more on the array.study this script:
<html>
<head>
<title>Array</title>
<?PHP
$start = 1;
$times = 2;
$answer = array();
for ($start; $start < 11; $start++) {
$answer[$start] = $start * $times;
print (" answer = " . $answer[$start] . "<BR>");
}
?>
</head>
<body>
</body>
</html>
N/NOTE:so far.....okay?
Okey,for this post,we will learn the array.What is array?click here for more.So,first of all i show to you on how declare the array in PHP.
the Declaration:
$Group = array( );
So,$Group is because i named it,well you can put any variable's name that you like.But,the important is,$Group already be an array because of "=array()".So,to assign the value to the array,look at this example below :
$Group = array();
$Group[]="Eagle";
$Group[]="Alpha";
$Group[]="Beta";
$Group[]="Charlie";
beside we also can do like this:
$Group = array("Eagle","Alpha","Beta","Charlie");
okey,as we assign this,PHP will assign this values to be like this:
$Group[0]=Eagle
$Group[1]=Alpha
$Group[2]=Beta
$Group[3]=Charlie
but,i want to be like this:
$Group[1]=Eagle
$Group[2]=Alpha
$Group[3]=Beta
$Group[4]=Charlie
well,its look common ryte?as usual,when we start to count ,we always start with one ryte?so,to make like this,follow this code below:
$Group = array();
$Group[1]="Eagle";
$Group[2]="Alpha";
$Group[3]="Beta";
$Group[4]="Charlie";
or
$Group = array(1=>"Eagle",2=>"Alpha",3=>"Beta",4=>"Charlie");
one more thing,if you want assign number instead of number,well here is the code:
$Group = array(12,34,65);
or
$Group = array();
$Group[]=22;
$Group[]=45;
so,we do not need the double quote,just straight away type what number that do you want to assign.To study more on the array.study this script:
<html>
<head>
<title>Array</title>
<?PHP
$start = 1;
$times = 2;
$answer = array();
for ($start; $start < 11; $start++) {
$answer[$start] = $start * $times;
print (" answer = " . $answer[$start] . "<BR>");
}
?>
</head>
<body>
</body>
</html>
N/NOTE:so far.....okay?
Thursday, February 3, 2011
Break while PHP looping
Assalamu'alaikum and selamat sejahtera
So,we already covered for,while and do..while loop.But as we learned that the PHP will still to loop until it reach the limit or the condition is does not true,ryte?.hOw about if we want to get out of the loop although PHP does not reach the limit or the condition still true.Okey,to do that,we must have some special code to done this task,luckily that special code is only one word that is break.Here is how the break is work:
$Interrupts = true;
$number = 1;
while ($number < =10)
{
print("number = " + $number + "<BR>");
if ($Interrupts = = true)
break;
$counter++;
}
Just try this code and run it.
N/NOTE:Explore it by yourself!see ya!
So,we already covered for,while and do..while loop.But as we learned that the PHP will still to loop until it reach the limit or the condition is does not true,ryte?.hOw about if we want to get out of the loop although PHP does not reach the limit or the condition still true.Okey,to do that,we must have some special code to done this task,luckily that special code is only one word that is break.Here is how the break is work:
$Interrupts = true;
$number = 1;
while ($number < =10)
{
print("number = " + $number + "<BR>");
if ($Interrupts = = true)
break;
$counter++;
}
Just try this code and run it.
N/NOTE:Explore it by yourself!see ya!
While Loop and Do...While Loop
Assalamu'alaikum and selamat sejahtera
After For loops ,we will learn the While loop and Do..While loop.For those who already learned the C programming or other language programming might does not need explanation on this loops.For the beginner,just click here for more explanation.
oKEy,the concept of this loop is quite simple:
while (condition) {
code that to be executed if the condition is true
}
For example:
$number = 1;
while ($number =< 10) {
print (" number = " . $number . "<BR>");
$number++;
}
and for Do....While loop:
do
statement
while (condition)
for example:
$number = 1;
do
{
print (" number = " . $number . "<BR>");
$number++;
}
while ($number =< 10);
Is quite simple ryte?why don't u try these loop on the previous exercise.
The Code for do...while loop:
N/NOTE:lalalala
After For loops ,we will learn the While loop and Do..While loop.For those who already learned the C programming or other language programming might does not need explanation on this loops.For the beginner,just click here for more explanation.
oKEy,the concept of this loop is quite simple:
while (condition) {
code that to be executed if the condition is true
}
For example:
$number = 1;
while ($number =< 10) {
print (" number = " . $number . "<BR>");
$number++;
}
and for Do....While loop:
do
statement
while (condition)
for example:
$number = 1;
do
{
print (" number = " . $number . "<BR>");
$number++;
}
while ($number =< 10);
Is quite simple ryte?why don't u try these loop on the previous exercise.
The Code for do...while loop:
<html>
<head>
<title>Do...While loop</title>
<?PHP
$number=1;
do
{
print (" number = " . $number . "<BR>");
$number++;
}
while ($number <= 10);
?>
</head>
<body>
</body>
<html>
<head>
<title>Do...While loop</title>
<?PHP
$number=1;
do
{
print (" number = " . $number . "<BR>");
$number++;
}
while ($number <= 10);
?>
</head>
<body>
</body>
<html>
N/NOTE:lalalala
Exercise For Loop
Assalamu'alaikum and selamat sejahtera
After we learn the concept and practical of For loops.So...why dont us do some activity,this activity is called MathForLoops,the objective or cause is that we try to apply the math operation in For loops.Well as previous post,we already apply it on update , $counter and $number ,if u remember.Ok,for this post,we will do math table like times,minus,add and divide.The hint is refer the image below...i use add table,and u will do the other table.okey?hehe
Input:
Result:
Code:
File name: forloops.php
N/NOTE:Try ur self!
After we learn the concept and practical of For loops.So...why dont us do some activity,this activity is called MathForLoops,the objective or cause is that we try to apply the math operation in For loops.Well as previous post,we already apply it on update , $counter and $number ,if u remember.Ok,for this post,we will do math table like times,minus,add and divide.The hint is refer the image below...i use add table,and u will do the other table.okey?hehe
Input:
Result:
Code:
<html>
<head>
<title>An Addition Table Programme</title>
<?PHP
$add = 2;
if (isset($_POST['SubmitOne'])) {
$initial = $_POST['txtInitial'];
$final = $_POST['txtFinal'];
$add = $_POST['txtAdd'];
for($initial; $initial <= $final; $initial++) {
$answer = $initial + $add;
print $initial . " added by " . $add . " = " . $answer . "<BR>";
}
}
?>
</head>
<body>
<FORM NAME = formOne Method = "POST" Action = "forloops.php">
Start Number: <INPUT TYPE = Text NAME = txtInitial SIZE = 5 value ="1">
End Number: <INPUT TYPE = Text NAME = txtFinal SIZE = 5 value ="10">
Add By: <INPUT TYPE = Text NAME = txtAdd SIZE = 5 value = <?PHP print $add; ?> >
<P>
<INPUT TYPE = Submit Name = SubmitOne VALUE = "Do Add Table">
<P>
</FORM>
</body>
<html>
<head>
<title>An Addition Table Programme</title>
<?PHP
$add = 2;
if (isset($_POST['SubmitOne'])) {
$initial = $_POST['txtInitial'];
$final = $_POST['txtFinal'];
$add = $_POST['txtAdd'];
for($initial; $initial <= $final; $initial++) {
$answer = $initial + $add;
print $initial . " added by " . $add . " = " . $answer . "<BR>";
}
}
?>
</head>
<body>
<FORM NAME = formOne Method = "POST" Action = "forloops.php">
Start Number: <INPUT TYPE = Text NAME = txtInitial SIZE = 5 value ="1">
End Number: <INPUT TYPE = Text NAME = txtFinal SIZE = 5 value ="10">
Add By: <INPUT TYPE = Text NAME = txtAdd SIZE = 5 value = <?PHP print $add; ?> >
<P>
<INPUT TYPE = Submit Name = SubmitOne VALUE = "Do Add Table">
<P>
</FORM>
</body>
<html>
File name: forloops.php
For loops-PHP
Assalamualaikum and selamat sejahtera
loops?what it means?well for those who already learn the programming might already know what it is,and for beginner,click here.
make a file named ForLoop.php
Here the code:
<?PHP
$counter = 0;
$number = 1;
for($number; $number < 11; $number++)
{
$counter = $counter + 1;
print $counter . "<BR>";
}
for(initial; limit; update){
}
initial
is where you tell PHP the initial value of your loop. In other words, which number to start the loop?So we used this:
Like all variables, you can make up your own name like $iterate,well it all up to you.
You must set or assign the value of $number before the loop begins, like this:
$number = 1
for($number; $number < 11; $number++) {
Or:
The result is the same – the start number for this loop is 1
Limit
We have to tell PHP when to end your loop or the limit of the loop.So, we’re setting PHP to keep round the loop while the value of the variable $number is Less Than 11.
When the value of $number is 11 or higher, PHP will stop the loop.
Update
The loops need the update of starting value,in this case,we use $number,so $number need to be add if we set $number=1 or minus if we set $number=10 and the its limit is $number>1.So that,we can update the counter and display its update,thus make us believe the loops is working well.So to update,we usually like this:
$number++
the double plus symbol (++) =increment (increase the value by one).
It’s just a short way of saying this:
the double minus symbol(--)=decrement (decrease the value by one)
It’s just a short way of saying this:
So the story of this looping is like this:
“Starting/Initially/$number at a value of 1, keep going round and round while the start/$number value is less than and not equal to 11. Increase the starting/$number value by one each time round the loop.”
One more trick
$counter = $counter + 1;
print $counter . "<BR>";
or we can use like this:
$counter++;
print $counter . "<BR>";
N/NOTE:that's all folk!
loops?what it means?well for those who already learn the programming might already know what it is,and for beginner,click here.
make a file named ForLoop.php
Here the code:
<?PHP
$counter = 0;
$number = 1;
for($number; $number < 11; $number++)
{
$counter = $counter + 1;
print $counter . "<BR>";
}
?>
Here a little explanation on this script
}
initial
is where you tell PHP the initial value of your loop. In other words, which number to start the loop?So we used this:
$number = 1
Like all variables, you can make up your own name like $iterate,well it all up to you.
You must set or assign the value of $number before the loop begins, like this:
$number = 1
for($number; $number < 11; $number++) {
Or:
for($number = 1; $number < 11; $number++) {
The result is the same – the start number for this loop is 1
Limit
We have to tell PHP when to end your loop or the limit of the loop.So, we’re setting PHP to keep round the loop while the value of the variable $number is Less Than 11.
for($number; $number < 11; $number++) {
When the value of $number is 11 or higher, PHP will stop the loop.
Update
The loops need the update of starting value,in this case,we use $number,so $number need to be add if we set $number=1 or minus if we set $number=10 and the its limit is $number>1.So that,we can update the counter and display its update,thus make us believe the loops is working well.So to update,we usually like this:
$number++
the double plus symbol (++) =increment (increase the value by one).
It’s just a short way of saying this:
$number = $number + 1
the double minus symbol(--)=decrement (decrease the value by one)
It’s just a short way of saying this:
$number = $number - 1
So the story of this looping is like this:
“Starting/Initially/$number at a value of 1, keep going round and round while the start/$number value is less than and not equal to 11. Increase the starting/$number value by one each time round the loop.”
One more trick
$counter = $counter + 1;
print $counter . "<BR>";
or we can use like this:
$counter++;
print $counter . "<BR>";
N/NOTE:that's all folk!
Tuesday, February 1, 2011
Checkboxes-PHP
Assalamualaikum and selamat sejahtera
From previous,we already learn the radio button and for this post,we will learn the checkboxes.For the script of checkbox is quite similar with radio button.First we view on PHP script first:
<?PHP
$obj1 = 'unchecked';
$obj2 = 'unchecked';
if (isset($_POST['SubmitOne']))
{
if (isset($_POST['obj1'])) {
$obj1 = $_POST['obj1'];
if ($obj1 == 'male')
{$obj1 = 'checked';}
}
if (isset($_POST['obj2'])) {
$obj2 = $_POST['obj2'];
if ($obj2 == 'female')
{$obj2 = 'checked';}
}
}
?>
So,first we unchecked the obj1 and obj2.then we check to see if the Submit button was clicked:
Inside of this code,we still have another isset( ) function:
If the checkbox is ticked, it will return a value. So the isset( ) function will be true,then our code inside of the if statement gets executed:
if ($obj1 == 'male') {
$obj1 = 'checked';
}
.Here is the simple example...
"If the value inside of the variable called $obj1 is 'male' then change the status of obj1 to checked
The second of the if statements are the same – one for each checkbox on the form.
<FORM NAME ="form1" METHOD ="POST" ACTION ="CheckBoxes.php">
<Input type = 'Checkbox' Name ='obj1' value ="male"
<?PHP print $obj1; ?>
>Boy
<P>
<Input type = 'Checkbox' Name ='obj2' value="female"
<?PHP print $obj2; ?>
>Girl
<P>
<INPUT TYPE = "Submit" Name = "SubmitOne" VALUE = "Gender">
</FORM>
Its quite similar ryte with our previous post,ryte?
From previous,we already learn the radio button and for this post,we will learn the checkboxes.For the script of checkbox is quite similar with radio button.First we view on PHP script first:
<?PHP
$obj1 = 'unchecked';
$obj2 = 'unchecked';
if (isset($_POST['SubmitOne']))
{
if (isset($_POST['obj1'])) {
$obj1 = $_POST['obj1'];
if ($obj1 == 'male')
{$obj1 = 'checked';}
}
if (isset($_POST['obj2'])) {
$obj2 = $_POST['obj2'];
if ($obj2 == 'female')
{$obj2 = 'checked';}
}
}
?>
So,first we unchecked the obj1 and obj2.then we check to see if the Submit button was clicked:
if (isset($_POST['SubmitOne'])) {
}
if (isset($_POST['obj1'])) {
}
this statement are checking if a checkbox was set it is because if we did not check if the checkboxes are set or not,we will deal with a lot of errors,worse is "Undefined" errors.
if ($obj1 == 'male') {
$obj1 = 'checked';
}
.Here is the simple example...
"If the value inside of the variable called $obj1 is 'male' then change the status of obj1 to checked
The second of the if statements are the same – one for each checkbox on the form.
Ok,done for PHP script,ok for HTML form:
<Input type = 'Checkbox' Name ='obj1' value ="male"
<?PHP print $obj1; ?>
>Boy
<P>
<Input type = 'Checkbox' Name ='obj2' value="female"
<?PHP print $obj2; ?>
>Girl
<P>
<INPUT TYPE = "Submit" Name = "SubmitOne" VALUE = "Gender">
</FORM>
<Input type = 'Radio' Name ='gender' value= 'male' <?PHP print $male_status; ?>>Male
Just a little change,that all..
Finally make the new file named CheckBoxes.php
The code:
<html>
<head>
<title>Lessons for Checkboxes</title>
<?PHP
$obj1 = 'unchecked';
$obj2 = 'unchecked';
if (isset($_POST['SubmitOne'])) {
if (isset($_POST['obj1'])) {
$obj1 = $_POST['obj1'];
if ($obj1 == 'male')
{$obj1 = 'checked';}
}
if (isset($_POST['obj2'])) {
$obj2 = $_POST['obj2'];
if ($obj2 == 'female')
{$obj2 = 'checked';}
}
}
?>
</head>
<body>
<FORM NAME ="form1" METHOD ="POST" ACTION ="CheckBoxes.php">
<Input type = 'Checkbox' Name ='obj1' value ="male"
<?PHP print $obj1; ?>
>Male
<P>
<Input type = 'Checkbox' Name ='obj2' value="female"
<?PHP print $obj2; ?>
>Female
<P>
<INPUT TYPE = "Submit" Name = "SubmitOne" VALUE = "Gender">
</FORM>
</body>
</html>
<head>
<title>Lessons for Checkboxes</title>
<?PHP
$obj1 = 'unchecked';
$obj2 = 'unchecked';
if (isset($_POST['SubmitOne'])) {
if (isset($_POST['obj1'])) {
$obj1 = $_POST['obj1'];
if ($obj1 == 'male')
{$obj1 = 'checked';}
}
if (isset($_POST['obj2'])) {
$obj2 = $_POST['obj2'];
if ($obj2 == 'female')
{$obj2 = 'checked';}
}
}
?>
</head>
<body>
<FORM NAME ="form1" METHOD ="POST" ACTION ="CheckBoxes.php">
<Input type = 'Checkbox' Name ='obj1' value ="male"
<?PHP print $obj1; ?>
>Male
<P>
<Input type = 'Checkbox' Name ='obj2' value="female"
<?PHP print $obj2; ?>
>Female
<P>
<INPUT TYPE = "Submit" Name = "SubmitOne" VALUE = "Gender">
</FORM>
</body>
</html>
N/NOTE: good luck!question?
Subscribe to:
Posts (Atom)