Exercise PHP - 2

6/16/13


We continue to our next excercise. We have learn that PHP can do many things with website. We can display looping text as much as we want, we can change the text that want to display, and we can stop looping, and many more. The excercise below will display us how to learn looping text, stop it, and change the last text.
First look at the code below.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Looping and Break</title>
</head>


<body>
<?php
if (!isset($_POST['submit'])){
?>
<form action="Loop.php" method="post">
<table border="solid 1 px">
<tr>
<td>Text</td>
<td>:</td>
<td><input type="text" name="text"/></td>
</tr>

<tr>
<td>Replacing Text</td>
<td>:</td>
<td><input type="text" name="rtext"/></td>
</tr>

<tr>
<td>Display as</td>
<td>:</td>
<td><select name="disp">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>Times</td>
</tr>

<tr>
<td>Do</td>
<td>:</td>
<td><select name="do">
<option value="break">Break</option>
<option value="continue">Continue</option>
</select></td>
</tr>

<tr>
<td>On count</td>
<td>:</td>
<td><select name="count">
<?php
for($i=1;$i<=15;$i++){
echo "<option value='$i'>$i</option>";
}
?>
</select></td>
</tr>

<tr>
<td></td>
<td></td>
<td><input type="submit" name="submit"/></td>
</tr>
</table>
</form>
<?php }else{
$text = $_POST['text'];
$rtext = $_POST['rtext'];
$disp = $_POST['disp'];
$do = $_POST['do'];
$count = $_POST['count'];

for($i=1;$i<=$disp;$i++){
echo "Count $i : ";
if($i==$count){
if($do=="break"){
echo "$rtext <br>";
break;
}
elseif($do=="continue"){
echo "$rtext <br>";
continue;
}

}
echo "$text <br>";
}
echo "Out of the loop";

}


?>
</body>

</html>

Okay, do you understand the code above? Well if you're not, let's learn it code by code.

We start with :

<?php
if (!isset($_POST['submit'])){
?>
it means that we if the submit button is not being set/click, then the website won't do anything, so user can fill the data in form. And of course, the opposite, when submit button is click/set, it will do the looping, the code for looping is located after </form>

Next :
<form action="Loop.php" method="post">
it means that the form will load "Loop.php" which is the file itself and the method is post.
Next is the table and form, maybe we can skip it, because it's only the form that we can use to fill the data. I assume you have known about HTML.

Next :

$text = $_POST['text'];
$rtext = $_POST['rtext'];
$disp = $_POST['disp'];
$do = $_POST['do'];
$count = $_POST['count'];



Well after this, we should create variable based on name of the form that we have created before. $text will be variable that has value based on what text that user type in the 'text' form. The rest is the same.

Next : How to make the text that we fill in the form, become looping and it will stop at the value that we input too? So here's the code :

for($i=1;$i<=$disp;$i++){
echo "Count $i : ";
if($i==$count){
if($do=="break"){
echo "$rtext <br>";
break;
}
elseif($do=="continue"){
echo "$rtext <br>";
continue;
}

}
echo "$text <br>";
}
echo "Out of the loop";

First we create the variable $i, to help us to display looping of the text. Then we make condition, If the value of variable $i same as variable $count, variable $count is variable that contain the value when the text will be stopped for being replaced by the replacing text.

If the variable has same value, then we create another condition. $do is how we want to do after the text is being replaced. We can break, or in another word, stop it and it will be out of the loop, or we can continue, so the text will be looped as much as the $disp value.

If we choose break, for example, it will be out of the loop, even though the loop has not come until the end of the loop.

Well that explain all the code above. The result of this code will be like these picture below.


Form of Looping
Form of Looping

Result
Result

No comments:

 

Tags