-1

hi i already can make search. now how i want to put that data based on their role, based on this picture i want to make admin ,admin , user, user,user. need to use sort by ? i dont know.

my website

and this my coding search . where should i put sort by ?

<?php
include 'config1.php';
if(isset($_POST['search'])){

$searchTerm = $_POST['search'];
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%'";
$result  = mysql_query($query);
$count = mysql_num_rows($result);
echo "<table height = '30%'border='1'>";
if($count == 0)
  {
   echo "NO ID REGISTERED!";
  }  
else
{
    while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<td width='5%'><b>USER ID:</b> {$row['userid']} </td>";
    echo "<td width='5%'><b>USER NAME :</b> {$row['username']} </td>";
    echo "<td width='5%'><b>USER EMAIL:</b> {$row['useremail']} </td>";
    echo "<td width='5%'><b>USER ROLE:</b> {$row['userrole']} </td>";
    echo "<td width='5%'><b>USER DIVISION:</b> {$row['userdiv']} </td>";
    echo "<td width='5%'><b>USER DEPARTMENT:</b> {$row['userdepartment']} </td>";
    echo "</tr>";
    }
}

echo"</table>";
}
?>
lightyagami
  • 35
  • 1
  • 8
  • 2
    use `ORDER BY userrole` on query, and consider ditching `mysql_` functions, and replace them with `mysqli_*` or `PDO` with [prepared statements](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/12860140#12860140) – Kevin Aug 28 '14 at 07:14
  • where should i put ORDER BY ? – lightyagami Aug 28 '14 at 07:14
  • can you give me complete coding for order by ? – lightyagami Aug 28 '14 at 07:15
  • 2
    `$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' ORDER BY userrole";`.... @Ghost's original response told you everything you needed to know to be able to figure it out for yourself – Mark Baker Aug 28 '14 at 07:15
  • thanks :) do you know how to make delete button from this search . i will create one button , call delete , so when i click that button the user that i search will deleted. except admin . if i search admin , cant delete. only user. can u help me ? – lightyagami Aug 28 '14 at 08:23

2 Answers2

1
  $query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' ORDER BY userrole";

Also can use ASC or DESC for displaying in ascending or descending order after ORDER BY. eg.

Select * from users WHERE choice = 'PHP' ORDER BY id ASC;
Naincy
  • 2,953
  • 1
  • 12
  • 21
  • hi guys , i want to ask . i already manage to search that user details. then i create one button called delete. i want to delete that user id . and if user role is admin i cant delete them . only user. can you help me ? – lightyagami Aug 28 '14 at 08:22
  • DELETE FROM members where userid = $userid and userrole <> 'admin'; – Naincy Aug 28 '14 at 08:26
  • where should i put this delete from ? in new page that button linked to ? or under search ? and $userid declare what ? $userid = $_POST['userid'] ? should i use if(isset($_POST['delete']))? *sorry for my bad english * – lightyagami Aug 28 '14 at 08:30
  • the list u shown above in that u can create dynamic delete button for each row.And on click of that you can call this query by passing userid of that row. – Naincy Aug 28 '14 at 08:34
  • how to put delete button in every row? i mean when i insert user id , there are only one data display. how should i put this delete button ? can you show me the coding ? i cant imagine it. heee sorry . – lightyagami Aug 28 '14 at 08:37
0
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' order by fieldWhichYouWant";

OR

 $query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' ORDER BY userrole ASC";

or add ASC or DESC as Ascending or descending oder to sort.

Replace fieldWhichYouWant with your requirement.

Naresh
  • 2,761
  • 10
  • 45
  • 78