0

I'm new to php & developing a page where i want to fetch data from db & the pagination should be in alphabetical format. i.e A|B|C| .... On click of A it should display names starting with A only. Currently i'm just using LIMIT function. Please Help !

<?php
     $host="localhost"; // Host name 
     $username="root"; // Mysql username 
     $password=""; // Mysql password 
     $db_name="testmra"; // Database name  
     // Connect to server and select databse.
     $conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 
     mysqli_select_db($conn,$db_name);
     $sql = "SELECT * FROM newuser WHERE role='User' ORDER BY name asc LIMIT 15";
     $query = mysqli_query($conn,$sql);

     echo "<table border='1' id='mytable' width='100%'>
         <tr><th colspan='9' align='center'><h2>User Details</h2></th></tr>
         <tr bgcolor='grey'>
             <th width='25%'>Full Name</th>
             <th width='25%'>Department</th>
             <th width='25%'>EmailId</th>
             <th width='25%'>Username</th>
         </tr>";
        while($row = mysqli_fetch_array($query))
        {
          echo "<tr>";
          echo "<td align='center'>" . $row['name']. "</td>";
          echo "<td align='center'>" . $row['department']. "</td>";
          echo "<td align='center'>" . $row['emailid'] . "</td>";
          echo "<td align='center'>" . $row['username'] . "</td>";
          echo "</tr>";
        }
     echo "</table>";
     mysqli_close($conn);   
?>
Fresher
  • 23
  • 1
  • 9

2 Answers2

1
    <?php
     $host="localhost"; // Host name 
     $username="root"; // Mysql username 
     $password=""; // Mysql password 
     $db_name="testmra"; // Database name  
     // Connect to server and select databse.

     $conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 
     mysqli_select_db($conn,$db_name);
     if(isset($_GET['get'])){
       $get = mysqli_real_escape_string($_GET['get']);
     }else{
      $get = '';
     }
     $sql = "SELECT * FROM newuser WHERE role='User' AND name LIKE '$get%' ORDER BY name asc LIMIT 15";
     $query = mysqli_query($conn,$sql);

     echo "<table border='1' id='mytable' width='100%'>
         <tr><th colspan='9' align='center'><h2>User Details</h2></th></tr>
         <tr bgcolor='grey'>
             <th width='25%'>Full Name</th>
             <th width='25%'>Department</th>
             <th width='25%'>EmailId</th>
             <th width='25%'>Username</th>
         </tr>";
        while($row = mysqli_fetch_array($query))
        {
          echo "<tr>";
          echo "<td align='center'>" . $row['name']. "</td>";
          echo "<td align='center'>" . $row['department']. "</td>";
          echo "<td align='center'>" . $row['emailid'] . "</td>";
          echo "<td align='center'>" . $row['username'] . "</td>";
          echo "</tr>";
        }
     echo "</table>";
     mysqli_close($conn);   
?>
<?php
$alphas = range('A', 'Z');
 foreach ($alphas as $key) {?>
<a href="answer.php?get=<?php  echo $key  ?>"><?php echo $key ?></a>
<?php }?>

using this you will get every alphabets so call it using ajax and you will get your alphabets using get method. Make new page answer.php for this code.

1

I wanted to add this as a comment to the accepted answer but I don't have enough reputation to do so yet, sorry!

The answer is good, but I would strongly discourage placing variables inside a SQL statement, despite escaping the string before inserting it, it's still a potential risk.

StackOverflow: Preventing SQL injection

The above link explains in excellent detail how to deal with this matter properly. A very worthwhile read for anyone involved in SQL at any level.

Community
  • 1
  • 1
Darren H
  • 910
  • 8
  • 24
  • Can you check correctly? i used mysqli_real_escape_string. so its prevent mysql injections. – Shashikant Chauhan Jul 15 '15 at 07:07
  • [Why prepared statements are better than escaping](http://stackoverflow.com/questions/4771984/are-dynamic-mysql-queries-with-sql-escaping-just-as-secure-as-prepared-statements) – Darren H Jul 15 '15 at 07:14
  • Ankit, I'd recommend leaving Shashikant's answer as the accepted answer as it is a full working solution. Mine is just a security ammendment. An up vote would be nice if you feel it is useful but it is not a full answer so should not be accepted – Darren H Jul 15 '15 at 07:21
  • yes i know that @darren thanks for suggesting issues about sql injections. – Shashikant Chauhan Jul 15 '15 at 07:22
  • @DarrenH vote up is not possible because i don't have enough reputation. – Fresher Jul 15 '15 at 07:26