Thursday 26 May 2016

Week 13

We had discussion on our final individual project regarding the steps to design a website and the functions need to include in our project. Dr Syamsul opened a link to allow us to book for our presentation date. 

For the lab session 
Today is our last class and we had a short online quiz. Below are the questions and answer of my quiz:

1. Which of the following is true in PHP if $x = 4 and $y = 9?

Select one:

 $y >  "9"

 $x % $y >  $y - $x

 $x == 4 Correct
Correct

 none of the answer

2. What will be displayed in a browser when the following PHP code is executed:

<?php
 $variable = 2;
 switch ($variable)
 {
  case 1:
   echo "Hello World";
   break;
  default:
   echo "Hello Robert";
   break;
 }
?>
Select one:
Correct

3. Which operator is used to compare two values.

Select one:
Correct
Correct
 

4. The if statement is used to execute some code only if a specified condition is true.

Select one:
Correct
 

5. What will be displayed in a browser when the following PHP code is executed:

<?php 
   $x = 6;
   $y = 4;
   $sum = $x + $y; //sum = 10
   $ten = "Ten";

   if ($sum >= 10 && $ten == "Ten") {
       $answer = "Correct";
       echo "Your answer is: $answer";
   }
   else {
       $answer = "Incorrect";
       echo "Your answer is: $answer";
   }
?>
Select one:
Correct
 

6. What will be printed?

if ('2' == '02') {   
  echo 'True';
} else {
  echo 'False';
}
Select one:
Correct

7. How do you get information from a form that is submitted using the "get" method?

Select one:
Correct
 

8. When using the POST method, variables are displayed in the URL.

Select one:
Correct  

9. Since PHP is _____________, the source code is freely available on the web, and developers can install and use it without paying licensing fees or investing in expensive hardware or software.

Select one:
Correct
 

10. What will be displayed in a browser when the following PHP code is executed:

<?php 
   if (8 <= 7) {
       echo "Hello World";
   }
   else {
       echo "Hello YouTube";
   }
?>
Select one:
Correct


11. What will be printed by the code below?


<?php

    FUNCTION TEST()
    {
        ECHO "HELLO WORLD!\n";
    }

    test();
?>
Select one:
Correct

12. All the following statements are TRUE when describing form controls, EXCEPT:

Select one:

13. PHP server scripts are surrounded by which delimiters?

Select one:
Correct

14. What is the correct way to create a function in PHP?

Select one:
Correct

15. Which of the following is NOT a valid PHP variable?

Select one:
Correct

16. Which one is the expected output of the following codes?


============================
<html><head></head> 
<body> 
<h4>CSS stands for:</h4> 

<?php 
//print output 
echo '<h4><i>A: Cascading Style Sheet </i></h4>'; 
?> 
</body></html> 
==========================
Select one:

17. In PHP you can use both single quotes ( ' ' ) and double quotes ( " " ) for strings.

Select one:
Correct

18. The built-in PHP function ___________ is used to collect values in a form with method="post"

Select one:
Correct

19. What is the output of this code?


<?php
function apple($apples=4)
{
 $apples=$apples/2;
 return $apples;
}

$apples = 10;
apple($apples);
echo $apples;
?>
Select one:
Correct

20. What is the expected output of the following PHP codes?

<?php 
$dob= "hello"; 
$age=$dob + 15; 
echo "$age"; 
?> 
Select one:
Correct
 
 

21. Other than echo, state the other way of printing a statement in PHP.

Select one:
Correct

22. What is the output of the following?

<?php
function a($number)
{
  return (b($number)*$number);
}
function b($number)
{
  ++$number;
}
echo a(5);
?>
Select one:
Correct

23. What will be displayed in a browser when the following PHP code is executed:


<?php 
   $bookColor = "red";
   $bookName = "The PHP Basics";

   if ($bookColor == "red" && $bookName == "Steve Jobs") {
       echo "You have the wrong book";
   }
   else {
       echo "You might have the right book";
   }
?>
Select one:
Correct

24. Which one of these variables has a legal name?

Select one:
Correct
Correct

25. PHP and JavaScript are the same.    

Select one:
Correct

26. In PHP, both ________ and ________ can be used for strings.

Select one:

27. What is a correct way to add a comment in PHP?

Select one:
Correct

28. What is the output produced by the PHP codes below:

<?php 
$x = 1.5; 
settype ($x, "integer”); 
echo $x; 

?> 
Select one:
Correct

 29. Which statement about PHP functions is correct?

Select one:
Incorrect

30. What would be the output of the following PHP codes?


<?php 
 $languages=array(0 => "Perl", 1 => "PHP"); 
 $search= "PHP"; 
 $limit= count($languages); 

 for ($i=0;$i<$limit;$i++){ 
  echo "<br>Testing for match with $languages[$i]"; 
  if ($search == $languages[$i]) 
  { 
   echo "<br> $search is an approved language"; 
  } 
 } 
?> 
Select one:
Correct
Correct

Saturday 21 May 2016

Week 12

What is a PHP Session?

When you work with an application, you open it, do some changes, and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn't maintain state.
Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser.
So, Session variables hold information about one single user, and are available to all pages in one application.

To start a session and to track session variables across user session:  
session_start();

To register a session: 
session_register(variables);

To destroy a session:
session_destroy();


Lab class was cancelled as we were asked to join the event organized on SMMTC week.
 

Saturday 14 May 2016

Week 11

Lecturer had uploaded a zip file and we were required to connect it to the database. 
We need to add user account

We need to create a new user account.
Tick "Grant all privileges on wildcard name"

Creating a table.
We encountered some problems while connect the files to the database. However, the problem is solved by deleting the semicolon for the php.ini file as shown in  picture below.



Saturday 7 May 2016

Week 10

We learnt about the Server Side Script which is MySQL. SQL is a standard language for accessing databases.
To build a web site that shows data from a database, you will need:

  • An RDBMS database program (i.e. MS Access, SQL Server, MySQL)
  • To use a server-side scripting language, like PHP or ASP
  • To use SQL to get the data you want
  • To use HTML / CSS
Some of The Most Important SQL Commands:
Data Manipulation Language
  • SELECT - extracts data from a database
  • UPDATE - updates data in a database
  • DELETE - deletes data from a database
  • INSERT INTO - inserts new data into a database
Data Definition Language
  • CREATE TABLE - creates a new table
  • ALTER TABLE - modifies a table
  • DROP TABLE - deletes a table
  • CREATE INDEX - creates an index (search key)
  • DROP INDEX - deletes an index 
Advantages of using database to store web data:
  • faster access
  • better concurrent access
  • easier changes to data and scripts
  • increased security
Accesing MySQL via PHP
  1. Make a connection to the MySQL database server.
  2. Select the database within MySQL that you want to work with.
  3. Issue an SQL statement/ create the query.
  4. If you are issuing a SELECT statement, iterate through the result set and extract each record.


There is no class for the lab session as Thursday is a holiday.
 

Friday 29 April 2016

Saturday 23 April 2016

Week 8



This week we learned on PHP and form processing. PHP is good at handling data submitted to the server in HTML forms. Form is used to accept user input in a systematic and structured manner. There are two main attributes in a form which are method and action.

1.   $_POST
Form processing with $_POST- welcome.html
welcome.html with POST method
output


Displaying Submitted Values- welcome.php
welcome.php with POST method
output



2.   $_GET
Form processing with $_GET- welcome.html

welcome.html with method GET


output

Displaying Submitted Values- welcome.php


welcome.php with GET method

output

 3.   $_REQUEST variable: it contains contents both $_GET, $_POST and $_COOKIE. It can be used to get the result from form data sent with both the GET and POST methods. 



For the lab session

We have done some discussion on past year question regarding to processing CGPA.  
Click to visit the link.

//retrieved information from the form
   

    $name = $_POST["name"];
    $matric = $_POST["matric"];
    $stiv1023_mark = $_POST["stiv1023_mark"];
    $stiv2023_mark = $_POST["stiv2023_mark"];
    $stiv3013_mark = $_POST["stiv3013_mark"];
    $vkhb2031_mark = $_POST["vkhb2031_mark"];


//calculate grade for each course by calling calculateGred() function
   
    $stiv1023_gred = calculateGred($stiv1023_mark);
    $stiv2023_gred = calculateGred($stiv2023_mark);
    $stiv3013_gred = calculateGred($stiv3013_mark);
    $vkhb2031_gred = calculateGred($vkhb2031_mark);
     

 //calculate point for each course by calling calculatePoint() function
   
    $stiv1023_point = calculatePoint($stiv1023_gred);
    $stiv2023_point = calculatePoint($stiv2023_gred);
    $stiv3013_point = calculatePoint($stiv3013_gred);
    $vkhb2031_point = calculatePoint($vkhb2031_gred);
   
    //calculate TotalPoint based on point for each course and credit
   
    $TotalPoint = $stiv1023_point * 3 + $stiv2023_point * 3 + $stiv3013_point * 3 +    $vkhb2031_point * 1;
   
    //calculate GPA based on TotalPoint and total credit
   
    $GPA = $TotalPoint / 10;
   
    //calculate final result by calling calculateResult() function
   
    $result = calculateResult($GPA);
   
    //function to calculate the grade based on mark

   
    function calculateGred($mark)
    {
        if ($mark >= 90)
            $gred = "A";
           
        else if ($mark >= 80)
            $gred = "B";
           
        else if ($mark >= 70)
            $gred = "C";
           
        else if ($mark >= 60)
            $gred = "D";
           
        else if ($mark >= 50)
            $gred = "E";
           
        else
            $gred = "F";
       
        return $gred;
    }
   
    //function to calculate the point based on grade

   
    function calculatePoint($gred)
    {
        if ($gred == "A")
            $point = 4.00;
           
        else if ($gred == "B")
            $point = 3.50;
           
        else if ($gred == "C")
            $point = 3.00;
           
        else if ($gred == "D")
            $point = 2.50;
           
        else if ($gred == "E")
            $point = 2.00;
           
        else
            $point = 1.00;
           
        return $point;
    }
   
    //function to calculate the result based on GPA
   
    function calculateResult($GPA)
    {
        if ($GPA >= 3.00)
            $result = "Pass with distinction";
       
        else if ($GPA >= 2.00)
            $result = "Pass";
           
        else
            $result = "Fail";
           
        return $result;
    }





    //to print output of the name and matric   
    Name: <?php echo $_POST["name"]; ?><br>
    Matric: <?php echo $_POST["matric"];?>


    //to display output in a Table format

<table width = "100%" border="1" cellspacing="2" cellpadding="2">
<tr>
<th align="left">Course Code</th>
<th align="left">Course Name</th>
<th align="center">Credits</th>
<th align="center">Marks</th>
<th align="center">Gred</th>
<th align="center">Point</th>
</tr>
<tr>
<td>STIV1023</td>
<td>Multimedia Foundation</td>
<td align="center">3</td>
<td align="center"><?php echo $stiv1023_mark;?></td>
<td align="center"><?php echo $stiv1023_gred;?></td>
<td align="center"><?php echo $stiv1023_point;?></td>
</tr>
<tr>
<td>STIV2023</td>
<td>Graphic &amp; Animation</td>
<td align="center">3</td>
<td align="center"><?php echo $stiv2023_mark;?></td>
<td align="center"><?php echo $stiv2023_gred;?></td>
<td align="center"><?php echo $stiv2023_point;?></td>
</tr>
<tr>
<td>STIV3013</td>
<td>WWW Programming</td>
<td align="center">3</td>
<td align="center"><?php echo $stiv3013_mark;?></td>
<td align="center"><?php echo $stiv3013_gred;?></td>
<td align="center"><?php echo $stiv3013_point;?></td>
</tr>
<tr>
<td>VKHB2031</td>
<td>Kelana Siswa</td>
<td align="center">1</td>
<td align="center"><?php echo $vkhb2031_mark;?></td>
<td align="center"><?php echo $vkhb2031_gred;?></td>
<td align="center"><?php echo $vkhb2031_point;?></td>
</tr>
</table><br/>

<p>Total Credit: 10<br/>
Gred Point Average(GPA) = <?php echo $GPA;?><br/>
Result : <?php echo $result;?></p>
 



output