Jump to content

Sort the Column of Table when the header is clicked


sandy1028

Recommended Posts

Please provide me the code to sort the Column of Table when the header is clicked. I am the beginner of php.

 

The code used is

phone.php

<style>

    table, input, label, select { font-size:12pt; }

</style>

<script>

function validate(){

        var mob = document.phoneform.mobile.value;

var age1 = document.phoneform.age.value;

var ema = document.phoneform.email.value;

var at="@";

        var dot=".";

var pattern = /\@[\w\-]+\.[\w\-]/;

 

 

        if(document.phoneform.name.value == ""){

          alert("Enter Name");

          document.phoneform.name.focus();

          return false;

        }else if(document.phoneform.address.value == ""){

          alert("Enter Address");

          document.phoneform.address.focus();

          return false;

        }else if(document.phoneform.age.value == "" || isNaN(document.phoneform.age.value) || (age1.length >3)){

          alert("Enter Age and should be integer");

          document.phoneform.age.focus();

          return false;

}

else if((document.phoneform.email.value == "") || (!pattern.test(phoneform.email.value))) {

          alert("Enter Email");

          document.phoneform.email.focus();

          return false;

 

        }else if((document.phoneform.mobile.value == "") || (isNaN(document.phoneform.mobile.value))) {

 

          alert("Enter Mobile and should be integer");

          document.phoneform.mobile.focus();

          return false;

 

        }else if(document.phoneform.residence.value == "" || isNaN(document.phoneform.residence.value)){

          alert("Enter Residence number and should be integer");

          document.phoneform.residence.focus();

          return false;

}

}

 

 

</script>

 

<?

echo "<html>";

echo "<head>";

echo "<title>Phone book</title>";

echo "</head>";

 

 

echo "<form name=\"phoneform\" method=\"post\" action=\"phoneTab.php\" onsubmit=\"return validate()\">";

echo "<body bgcolor=\"#FFF0FF\">";

    echo "<div style=\"position:absolute; width=760px;\">";

    echo "<table cellpadding=\"5\" cellspacing=\"10\" border=\"0\" width=100% align=center>";

echo "<td COLSPAN=2 align=center><h2><font color='#7E59A8' face=Arial>Online Phone Directory</font></h2></td>";

        echo "<tr>";

        echo "<td align=left><b>Name:</b></td>";

        echo "<td> <input type=text name= name size=50></td>";

        echo "</tr>";

        echo "<tr>";

        echo " <td align=left><b>Address:</b></td>";

        echo "<td> <input type=text name=address size=50></td>";

        echo "</tr>";

        echo "<tr>";

        echo "<td align=left><b>Age:</b></td>";

echo "<td> <input type=text name=age size=50>";

        echo "</td>";

        echo "</tr>";

 

        echo "<tr>";

        echo "<td align=left><b>Email:</b></td>";

        echo "<td> <input type=text name=email size=50></td></tr>";

 

        echo "<tr>";

        echo "<td align=left><b>Mobile :</b></td>";

        echo "<td> <input type=text name=mobile size=50></td>";

        echo "</tr>";

 

        echo "<tr>";

        echo "<td align=left><b>Residence No: </b></td>";

        echo "<td> <input type=text name=residence></td>";

        echo "</tr>";

 

echo "<tr>";

        echo "<tr>";

        echo "<td align=right><input type=submit value=\"Register\" ></td>";

        echo "<td><input type=reset value=\"Reset\"></td>";

        echo "</tr>";

        echo "</table>";

        echo "</div>";

echo "</form>";

echo "</body>";

echo "</html>";

?>

     

 

phoneTab.php

 

<?

/*$name=$_POST['name'];

$address=$_POST['address'];

$age=$_POST['age'];

$email=$_POST['email'];

$mobile=$_POST['mobile'];

$residence=$_POST['residence'];

//print "$name";

 

$data="$name : $address : $age : $email : $mobile :  $residence\n";

//print "$data";

$fp = fopen("phonedb.php","a");

 

if(!$fp) {

    echo 'The file cannot be created';

    exit;

}

 

fwrite($fp, $data);

//fwrite($fp, $address);

//fwrite($fp, $age);

//fwrite($fp, $email);

//fwrite($fp, $mobile);

//fwrite($fp, $residence);

 

fclose($fp);

 

*/

 

echo "<html>";

echo "<head>";

echo "<title>Online phone Directory</title>";

echo "</head>";

 

 

 

echo "<body bgcolor=\"#FFF0FF\" text=\"#000000\">";

echo "<p align=\"center\"><font size=\"20\" <b>Online Phone Book<b></font><br><br>";

 

// Links

 

echo "<div style=\"position:absolute; left:600px; top:60px;\">";

echo "<a href=\"help.php\">Help</a> <a href=\"aboutus.php\">About US</a></p>";

echo "</div></p><br><hr><br><br><br>";

// List box

 

echo "<div style=\"position:absolute; left:10px; top:200px;\">";

$lines = file('./phonedb.php');

 

                foreach ($lines as $line) {

                $text_line = explode(":" , $line);

$text= "$text_line\n";

}

 

echo " <p> Name   <select name=\"name\">";

echo "<option>$text_line[0]";

echo "</option>";

  echo "</select>";

  echo "</p>";

echo "<p> Mobile  <select name=\"select2\">";

echo "<option>$text_line[4]";

  echo "</select>";

echo "</p>";

 

echo "</div>";

// Table and included the contents from phonedb.php

 

echo "<div style=\"position:absolute; left: 175; width:450; top: 180px\" align=center>";

?>

          <table width="100%" border="1" cellpadding="0" cellspacing="0">

           

              <tr>

                <td align=center width="85"><b>  Name</b></td>

                <td align=center width="100"><b>  Address</b></td>

                <td align=center width="30"><b>Age</b></td>

                <td align=center width="140"><b>  Email</b></td>

                <td align=center width="95" ><b> Mobile</b></td>

<td align=center width="75" ><b>  Residence</b></td>

 

              </tr>

</table></div>

<div style="position:absolute; overflow:auto; left: 175; width:466; height:150; top: 200" align=center>

<table class=status height=50 border="1" cellpadding="0" cellspacing="0">

      <?

$lines = file('./phonedb.php');

 

                foreach ($lines as $line) {

                $text_line = explode(":" , $line);

 

 

 

                echo "<tr>";

                echo "<td align=center width=\"83\"><b>$text_line[0]</b></td>";

                echo "<td align=center width=\"93\"><b>$text_line[1]</b></td>";

                echo "<td align=center width=\"126\"><b>$text_line[2]</b></td>";

                echo "<td align=center width=\"108\"><b>$text_line[3]</b></td>";

                echo "<td align=center width=\"126\" ><b>$text_line[4]</b></td>";

echo "<td align=center width=\"128\" ><b>$text_line[5]</b></td>";

                echo "</tr>";

        }

        ?>

 

</table>

</div>

<?

// Include Option button

 

 

echo "<div style=\"position:absolute; width:760px; left:670px; top:200px;\">";

echo "<p>Mobile      <input type=\"radio\" name=\"radiobutton\" value=\"radiobutton\">";

echo "</p>";

  echo "<p>Residence <input type=\"radio\" name=\"radiobutton\" value=\"radiobutton\">";

  echo "</p>";

echo "<p>Office        <input type=\"radio\" name=\"radiobutton\" value=\"radiobutton\">";

  echo "</p>";

 

// echo "</form>";

echo "</body>";

echo "</html>";

 

table.php

 

<?

$name=$_POST['name'];

$address=$_POST['address'];

$age=$_POST['age'];

$email=$_POST['email'];

$mobile=$_POST['mobile'];

$residence=$_POST['residence'];

//print "$name";

 

$data="$name : $address : $age : $email : $mobile :  $residence\n";

//print "$data";

$fp = fopen("phonedb.php","a");

 

if(!$fp) {

    echo 'The file cannot be created';

    exit;

}

 

fwrite($fp, $data);

//fwrite($fp, $address);

//fwrite($fp, $age);

//fwrite($fp, $email);

//fwrite($fp, $mobile);

//fwrite($fp, $residence);

 

fclose($fp);

 

 

 

echo "<html>";

 

 

echo "<body bgcolor=\"#FFF0FF\" text=\"#000000\">";

 

// Links

 

 

echo "<div style=\"position:absolute; left: 0; width:450; top: 80px\" align=center>";

?>

          <table width="100%" border="1" cellpadding="0" cellspacing="0">

           

              <tr>

                <td align=center width="59"><b>  Name</b></td>

                <td align=center width="62"><b>  Address</b></td>

                <td align=center width="41"><b>Age</b></td>

                <td align=center width="128"><b>  Email</b></td>

                <td align=center width="88" ><b> Mobile</b></td>

<td align=center width="74" ><b>  Residence</b></td>

 

              </tr>

</table></div>

<div style="position:absolute; overflow:auto; left: 0; width:466; height:50; top: 100" align=center>

<table class=status height=50 border="1" cellpadding="0" cellspacing="0">

      <?

$lines = file('./phonedb.php');

 

                foreach ($lines as $line) {

                $text_line = explode(":" , $line);

 

 

 

                echo "<tr>";

                echo "<td align=center width=\"83\"><b>$text_line[0]</b></td>";

                echo "<td align=center width=\"93\"><b>$text_line[1]</b></td>";

                echo "<td align=center width=\"126\"><b>$text_line[2]</b></td>";

                echo "<td align=center width=\"108\"><b>$text_line[3]</b></td>";

                echo "<td align=center width=\"126\" ><b>$text_line[4]</b></td>";

echo "<td align=center width=\"128\" ><b>$text_line[5]</b></td>";

                echo "</tr>";

}

//echo "<div style=\"position:absolute; left: 600; top: \" align=center>";

//echo "<input type=\"button\" value=\"Back\">";

  //  echo "</div>";

        ?>

</table>

</div>

 

<? echo "<div style=\"position:absolute; left: 190; top:270;  align=center\">";

echo "<input type=button value=Back onclick=\"history.go(-1); target=_parent\">";

echo "</div>";

?>

<?

// echo "</form>";

echo "</body>";

echo "</html>";

 

 

errr. i didnt bother reading any of the code. you didnt really ask a question about your code either, you just told people to write the code for you. why dont you give it a shot first? then post your problems here so we can help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.