Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Monday, 15 December 2014

TCExam - an Open Source Computer-Based Assessment system

TCExam is an Open Source system for electronic exams (also know as CBA - Computer-Based Assessment, CBT - Computer-Based Testing or e-exam) that enables educators and trainers to author, schedule, deliver, and report on quizzes, tests and exams. It is developed and supported by Tecnick.com LTD

Following are the features of TCExam
  •     Open Source Software
  •     Community support
  •     Platform Independent
  •     No expensive hardware requirements
  •     Internationalization (I18N)
  •     Accessibility and Usability
  •     Data Import and Export
  •     Rich Content
  •     Unique test
  •     Paper Testing with Optical Character Recognition (OMR)
It is developed using PHP for scripting and MySQL / PostgreSQL as back-end database server. Its simple and interactive installation script will provide a wizard for easy installation. It can be setup on  XAMPP, WAMP or MAMP,  or you can configure after setting up either Apache web server  or Microsoft's IIS, and MySQL or PostgreSQL server on your system.

 TCExam and  required software can be downloaded by following the links



Wednesday, 19 November 2014

How to pass variable number of arguments to a function in PHP?

In certain cases, you may require to pass different number of arguments to a function in PHP scripts. Let us consider a case where you want to calculate average of a set of values using a function. Number of values in the set may vary in each call to the function. PHP provides a mechanism to pass different number of arguments to a function and process the arguments from the function by using the following functions.

func_num_args() - Returns the number of arguments passes to the function

func_get_args() - Returns the arguments passed to the function as an array

func_get_arg($arg_num) - Returns the argument at the $arg_num th position or index . Starting index is 0.



You can see the PHP implementation of the above problem here.

Monday, 17 November 2014

How to apply alternate background color for rows of an HTML table using CSS

In some cases you may want to apply alternate background color for each row of an HTML table.Using CSS3 it is easy to apply alternate color for rows in an HTML table.

Create styles using CSS3 and specify background color by using nth-child(even) and nth-child(odd) selectors.


You can see the CSS3 implementation of the above problem here.

How to apply alternate background color for rows of an HTML table using PHP Script

In some cases you may want to apply alternate background color for each row of an HTML table. Using a simple script you can solve the problem. You can just apply one color for every odd row and apply another color for every even row. Check whether current row is even or odd by using a rowcount variable and check the result of rowcount mod 2  (ie. rowcount % 2) is 0 or not and change the value of background color attribute accordingly.

If rowcount % 2 is 0 then it is a even row and apply one color for that, other wise it is odd row and apply another color.

  $bgcolor = "#ffffff";
   if ($i % 2 == 0)
              $bgcolor = "#ECD23F";




You can see the PHP implementation of the above problem here.

Monday, 10 November 2014

Page wise or paginated listing of data from a database table using PHP

This example demonstrates how data from a database table  is displayed as different pages  in a webpage.

In this example records per page is stored in a variable $pagesize and it is set to 15 (you can change it as per your requirements). For each page the query is selecting only the required 15 records and displays it. 
Starting record of the given  page ($pageno) is calculated by the following expression.

 $start_rec = (($pageno-1) * $pagesize) ;

Query to select required number of records starting from start_rec is given below. To specify the starting record and number of records in SQL query the LIMIT clause is used.

Tuesday, 9 September 2014

How to upload a file using PHP script



In this article let us discuss how to upload a file to a web server using PHP script. It is implemented as a function in this example in upload.php file. To upload a file, you have to create an HTML form with a file input control by using the input tag like this: "<input type=file>". 

While creating the HTML form, enctype attribute of the form should have the value "multipart/form-data". So the form tag will be like this:

<form action="upload.php" method="post"  enctype="multipart/form-data">


HTML code to crate the form


<form action="upload.php" method="post" autocomplete="off" enctype="multipart/form-data">
        <table align="center" width="100%">
           
            <tr bgcolor="#ababee">
                <td colspan="2" align="center"><h2>Upload your file</h2></td>
            </tr>
            <tr>
                <td >Select file to upload</td>
                <td ><input type="file" name="file" id="file" style="left:0;">               
                </td>
            </tr>   
                          
           
            <tr>
                <td colspan="2">               
                <input type="submit" name="upload" value="Upload">
                </td>
            </tr>                
           
        </table>       
   </form>


Monday, 1 September 2014

Sample code for implementing "check availability option in registration form" using Ajax, PHP and MySQL



This example shows how you can include a ‘check availability’ option in a registration form using Ajax, PHP and MySQL. Using HTML input element, user enters the email id and submits to the server side PHP script ‘isavailable.php’ using Ajax. ‘isavailable.php’ script checks whether the email id is already available in the database table or not and returns the status as responseText.


checkavailability.php

This PHP file contains the form and the Ajax script to submit the email id to the server side PHP script (isavailable.php) and display the response on the page.


<!DOCTYPE html>
<html>
    <head>
        <title>Check Availability - Ajax, PHP and MySQL </title>
        <script>
        function checkavailability(){

        var xmlhttp;

        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari

            xmlhttp=new XMLHttpRequest();

        }

        else {// code for IE6, IE5

            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

        }



        xmlhttp.onreadystatechange=function() {

            if (xmlhttp.readyState==4 && xmlhttp.status==200) {

                document.getElementById("divInfo").innerHTML=xmlhttp.responseText;

            }

        }



        url = "isavailable.php";

        param = document.getElementById('txtemail').value;                       

        url = url + "?txtemail=" + param;

       

               

        xmlhttp.open("GET",url,true);

        xmlhttp.send();

    }
</script>

</head>
   
<body>


<form>
    <table align="center">
        <tr>
            <td colspan="2"><h2>Check Availability</h2></td>
        </tr>
        <tr>
        <td>Email</td>
        <td><input type="text" size="40" id="txtemail" name="txtemail"></td>
        </tr>
       
        <tr>
        <td></td>
        <td><input type="button" id="cmdcheck" name="cmdcheck" value="Check Availability" onclick="checkavailability()"></td>
        </tr>
       
        <tr>
            <td colspan="2"><div id="divInfo"></div></td>       
        </tr>
       
    </table>
</form>


</body>
</html>




isavailable.php

 This PHP file extracts the email id submitted to the server from $_REQUEST array and check whether this id is available in the table by using PHP mysql functions. The availability status is sends back to the requested page.


<?php
    $email = $_REQUEST['txtemail'];

    $dblink=mysql_connect("localhost","root","");
    mysql_select_db("dbcheckavailability", $dblink);

    $sql="SELECT * FROM tblmember WHERE email='" . $email . "';";

    $result=mysql_query($sql, $dblink);

    $responseText ="";
   
   
    if (mysql_num_rows($result)) {
        $responseText = "Sorry, <b>" . $email . "</b> is already in use!";
    }
    else {
        $responseText = "Congratulation, <b>" .  $email . "</b> is available!";
    }
  
    echo $responseText;
    mysql_close($dblink);  
?>



Table Structure

Thsis is the structure of the table that has to be created in MySQL for this example.

--
-- Database: `dbcheckavailability`
--

-- --------------------------------------------------------

--
-- Table structure for table `tblmember`
--

CREATE TABLE IF NOT EXISTS `tblmember` (
  `firstname` varchar(30) NOT NULL,
  `lastname` varchar(30) NOT NULL,
  `email` varchar(40) NOT NULL,
  UNIQUE KEY `email` (`email`)
);


INSERT INTO `tblmember` (`firstname`, `lastname`, `email`) VALUES
('anand', 'kumar', 'csnotes32@gmail.com');



Output