Jump to content

How do i get paging and sorting column by Column name?


jimjack145

Recommended Posts

 

Hi All,

 

I am using below code to paging my data. I want to sort my data by Column time. Not getting where i do changes. Please hlep me to build proper code.

 

Below i paste my code. Hope i get reply soon from ur site.

 

Regards.

Jim

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

 

<?php

 

if (!isset($_GET['how'])) {

    $_GET['how'] = "ASC";

    $query =""

}

 

if ($_GET['how'] == "ASC") {

    $how="DESC";

    $query=

} elseif ($_GET['how'] == "DESC") {

    $how="ASC";

}

 

 

//Set the page size

$PageSize = 20;

$StartRow = 0;

 

//Set the page no

if(empty($_GET['PageNo'])){

    if($StartRow == 0){

        $PageNo = $StartRow + 1;

    }

}else{

    $PageNo = $_GET['PageNo'];

    $StartRow = ($PageNo - 1) * $PageSize;

}

 

//Set the counter start

if($PageNo % $PageSize == 0){

    $CounterStart = $PageNo - ($PageSize - 1);

}else{

    $CounterStart = $PageNo - ($PageNo % $PageSize) + 1;

}

 

//Counter End

$CounterEnd = $CounterStart + ($PageSize - 1);

?>

 

<html>

<head>

<title>Current Candidate</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link rel="stylesheet" href="/var/www/style1.css" type="text/css">

</head>

<?php

include '/var/www/dbConnection.php';

$TRecord = mysql_query("SELECT `first_name` , `last_name` , `phone_home` , `phone_cell` , `city` , `current_employer` , `email1` FROM `candidate` ORDER BY `current_employer` ASC");

$result = mysql_query("SELECT `first_name` , `last_name` , `phone_home` , `phone_cell` , `city` , `current_employer` , `email1` FROM `candidate` ORDER BY `current_employer` LIMIT $StartRow,$PageSize");

 

//Total of record

$RecordCount = mysql_num_rows($TRecord);

 

//Set Maximum Page

$MaxPage = $RecordCount % $PageSize;

if($RecordCount % $PageSize == 0){

    $MaxPage = $RecordCount / $PageSize;

}else{

    $MaxPage = ceil($RecordCount / $PageSize);

}

?>

<body class="UsePageBg">

<center><B><font face="arial" size="5">List of Candidate</font></B></center>

<center><?php print "$RecordCount record(s) founds - You are at page $PageNo  of $MaxPage" ?></center>

<table width="100%" border="0" class="InternalHeader">

  <tr>

    <td>

 

      <div align="center">

      <?php

        //Print First & Previous Link is necessary

        if($CounterStart != 1){

            $PrevStart = $CounterStart - 1;

            print "<a href=stafflist_sort.php?PageNo=1>First </a>: ";

            print "<a href=stafflist_sort.php?PageNo=$PrevStart>Previous </a>";

        }

        print " [ ";

        $c = 0;

 

        //Print Page No

        for($c=$CounterStart;$c<=$CounterEnd;$c++){

            if($c < $MaxPage){

                if($c == $PageNo){

                    if($c % $PageSize == 0){

                        print "$c ";

                    }else{

                        print "$c ,";

                    }

                }elseif($c % $PageSize == 0){

                    echo "<a href=stafflist_sort.php?PageNo=$c>$c</a> ";

                }else{

                    echo "<a href=stafflist_sort.php?PageNo=$c>$c</a> ,";

                }//END IF

            }else{

                if($PageNo == $MaxPage){

                    print "$c ";

                    break;

                }else{

                    echo "<a href=stafflist_sort.php?PageNo=$c>$c</a> ";

                    break;

                }//END IF

            }//END IF

      }//NEXT

 

      echo "] ";

 

      if($CounterEnd < $MaxPage){

          $NextPage = $CounterEnd + 1;

          echo "<a href=stafflist_sort.php?PageNo=$NextPage>Next</a>";

      }

 

      //Print Last link if necessary

      if($CounterEnd < $MaxPage){

      $LastRec = $RecordCount % $PageSize;

        if($LastRec == 0){

            $LastStartRecord = $RecordCount - $PageSize;

        }

        else{

            $LastStartRecord = $RecordCount - $LastRec;

        }

 

        print " : ";

        echo "<a href=stafflist_sort.php?PageNo=$MaxPage>Last</a>";

        }

      ?>

 

      </div>

    </td>

  </tr>

</table>

<br>

<table width="95%" Border="1" class="NormalTableTwo" align="center">

  <tr>

    <td class="InternalHeader" width="4%"><center><B>No</B></center></td>

    <td class="InternalHeader" width="4%"><center><B><a href="<?php echo $_SERVER["PHP_SELF"]."?page=stafflist_sort.php&sort=first_name&how=".$how; ?>">First Name</B></a></center></td>

    <td class="InternalHeader" width="4%"><center><B>Last Name</B></center></td>

    <td class="InternalHeader" width="20%"><center><B>Phone Home</B></center></td>

    <td class="InternalHeader" width="20%"><center><B>Cell No</B></center></td>

    <td class="InternalHeader" width="5%"><center><B>City</B></center></td>

    <td class="InternalHeader" width="15%"><center><B>Current Employer</B></center></td>

    <td class="InternalHeader" width="20%"><center><B>Email</B></center></td>

  </tr>

 

<?php

$i = 1;

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

    $bil = $i + ($PageNo-1)*$PageSize;

?>

  <tr>

    <td class="NormalFieldTwo" width="4%"><?php echo $bil ?></td>

    <td class="NormalFieldTwo" width="4%"><?php echo $row[0] ?></td>

    <td class="NormalFieldTwo" width="4%"><?php echo $row[1] ?></td>

    <td class="NormalFieldTwo" width="20%"><?php echo $row[2] ?></td>

    <td class="NormalFieldTwo" width="20%"><?php echo $row[3] ?></td>

    <td class="NormalFieldTwo" width="5%"><?php echo $row[4] ?></td>

    <td class="NormalFieldTwo" width="15%"><?php echo $row[5] ?></td>

    <td class="NormalFieldTwo" width="20%"><?php echo $row[6] ?></td>

  </tr>

<?php

  $i++;

}?>

</table><br>

<table width="100%" border="0" class="InternalHeader">

  <tr>

    <td>

 

      <div align="center">

      <?php

        //Print First & Previous Link is necessary

        if($CounterStart != 1){

            $PrevStart = $CounterStart - 1;

            print "<a href=stafflist_sort.php?PageNo=1>First </a>: ";

            print "<a href=stafflist_sort.php?PageNo=$PrevStart>Previous </a>";

        }

        print " [ ";

        $c = 0;

 

        //Print Page No

        for($c=$CounterStart;$c<=$CounterEnd;$c++){

            if($c < $MaxPage){

                if($c == $PageNo){

                    if($c % $PageSize == 0){

                        print "$c ";

                    }else{

                        print "$c ,";

                    }

                }elseif($c % $PageSize == 0){

                    echo "<a href=stafflist_sort.php?PageNo=$c>$c</a> ";

                }else{

                    echo "<a href=stafflist_sort.php?PageNo=$c>$c</a> ,";

                }//END IF

            }else{

                if($PageNo == $MaxPage){

                    print "$c ";

                    break;

                }else{

                    echo "<a href=stafflist_sort.php?PageNo=$c>$c</a> ";

                    break;

                }//END IF

            }//END IF

      }//NEXT

 

      echo "] ";

 

      if($CounterEnd < $MaxPage){

          $NextPage = $CounterEnd + 1;

          echo "<a href=stafflist_sort.php?PageNo=$NextPage>Next</a>";

      }

 

      //Print Last link if necessary

      if($CounterEnd < $MaxPage){

      $LastRec = $RecordCount % $PageSize;

        if($LastRec == 0){

            $LastStartRecord = $RecordCount - $PageSize;

        }

        else{

            $LastStartRecord = $RecordCount - $LastRec;

        }

 

        print " : ";

        echo "<a href=stafflist_sort.php?PageNo=$MaxPage>Last</a>";

        }

      ?>

      </div>

    </td>

  </tr>

</table>

<?php

    mysql_free_result($result);

    mysql_free_result($TRecord);

?>

</body>

</html>

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

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.