sort my display data based on their user role - php

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.
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>";
}
?>

$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;

$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.

Related

Cannot sort by including the date

When I enter the vehicleID and Date, the records must be sorted by using both vehicle ID and Date, however it doesn't get sorted from the Date but only with the vehicleID. How can I achieve this ?
if ($vid != null && $datepicker != null) {
$conn = new Db();
$sql = "SELECT * FROM trip_details where vehicle_id = '".$vid."' AND date_t = '".$datepicker."'";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td> ". $row["trip_id"]."</td>";
echo "<td> ". $row["vehicle_id"]."</td>";
echo "<td> ". $row["total_trip_km"]."</td>";
echo "<td> ". $row["predict_fual"]."</td>";
echo "<td> ". $row["date_t"]."</td>";
// echo "<td><input type=\"submit\" value=\"view map\"></td>";
echo "</tr>";
}
}
SQL makes no guarantee on the order results are returned from a select query unless you explicitly add an order by clause, so the fact that you observe the returned records sorted by the vehicle_id is coincidental. You need to add an order by clause to the query:
ORDER BY vehicle_id, date_t
to sort you data by the columns you have define the name of columns in order by clause with order Acs(ascending) or Desc (descending)
e.g.
order by desc column1, asc column2
Order by needs to define to get the desired result.
try this
$sql="SELECT * FROM trip_details where
vehicle_id='".$vid."' AND date_t='".$datepicker."'
ORDER BY vehicle_id DESC,date_t DESC";

SELECT userinformation where userid is in the most rows

I have a table that contains the same userid multiple times.
I would like to return the picture of 9 users that have the most rows with their userid in it.
How can I select the user with the most rows and the ones following (from most to least)?
It's probably something easy but I can't find an answer.
$sql ="SELECT * FROM beoordelingen,users WHERE beoordelingen.userid=users.userid
GROUP BY beoordelingen.userid LIMIT 9 ";
$result = $conn->query($sql) or die ("The query could not be completed. try again");
$cols=3;
echo "<table width='150' border='0' cellpadding='0'>"; // The container table with $cols columns
do{
echo "<tr>";
for($i=1;$i<=$cols;$i++){ // All the rows will have $cols columns even if
// the records are less than $cols
$row=mysqli_fetch_array($result);
if($row){
echo"<td>
<table width='150' border='0' cellpadding='0'>
";
if ($row["userimage"] == '') {
echo "<a href='user.php? id=" . $row['userid'] . "'</a><img src='Images/users/nopicture.png' alt='nopicture'
class='userimage-tooltip-large' title='".$row['username']."''>";
} else {
echo "<a href='user.php? id=" . $row['userid'] . "'</a><img src='Images/users/".$row['userimage']."'
class='userimage-tooltip-large' title='".$row['username']."''>";
}
echo"</table>
</td>";
} else {
echo "<td> </td>"; //If there are no more records at the end, add a blank column
}
}
} while($row);
echo "</table>";
Add a count of the rows with the userid, order by that descending and then use the limit:-
SELECT users.userimage,
users.userid,
users.username,
COUNT(*) AS userid_count
FROM beoordelingen
INNER JOIN users
ON beoordelingen.userid=users.userid
GROUP BY beoordelingen.userid
ORDER BY userid_count DESC
LIMIT 9
Note that which values you get of the columns other than the userid is not defined. If you want the values from a particular row then this is a bit more complicated and you need to define which one.

Using data from first mysqli query in second query and nesting results

I have multiple tables I am joining to use in results for a second query and nesting the second results inside the first results.
I am using the following code:
$result = mysqli_query($con,"SELECT info.lotto_id, info.name, info.number_balls, info.number_bonus_balls, info.db_name, country.name_eng AS country, currency.name AS currency, currency.symbol AS symbol, next.draw_date AS next_draw, next.jackpot AS next_jackpot
FROM info
LEFT JOIN country ON info.country_id = country.id_country
LEFT JOIN currency ON info.currency_id = currency.currency_id
LEFT JOIN next ON info.lotto_id = next.lotto_id
WHERE (info.active='1')
ORDER BY next_jackpot DESC");
while($lotto = mysqli_fetch_array($result))
{
echo "<table border='0' width='600px' align='center'>";
echo "<tr>";
echo "<td>";
echo "<h1>Results for:</h1>";
echo "</td>";
echo "<td align='right'>";
echo "<p><img src='images/". $lotto['lotto_id'] ."_big.png' alt='". $lotto['name'] ." Results'/></p>";
echo "</td>";
echo "</tr>";
echo "</table>";
$result2 = mysqli_query($con,"SELECT * FROM" .$lotto['db_name'].
"ORDER BY date DESC
Limit 3");
while($draw = mysqli_fetch_array($result2))
{
echo "<table class='results' align='center'>";
echo "<tr>";
$draw['display_date'] = strtotime($draw['date']);
$lotto['cols'] = $lotto['number_balls'] + $lotto['number_bonus_balls'];
echo "<td class='date' colspan='".$lotto['cols']."'>".date('D M d, Y', $draw['display_date']). "</td>";
if ($draw[jp_code] < "1")
{
echo "<td class='winner' align='center'>Jackpot Amount</td>";
}
else
{
echo "<td class='rollover' align='center'>Rollover Amount</td>";
}
It is giving me the following error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/content/95/11798395/html/results/info_mysqli.php on line 59
This relates to my results2 query. Can somebody please suggest what I am doing wrong.
Thank you.
Change:
$result2 = mysqli_query($con,"SELECT * FROM" .$lotto['db_name'].
"ORDER BY date DESC
Limit 3");
to:
$result2 = mysqli_query($con, "SELECT * FROM {$lotto['db_name']} ORDER BY date DESC LIMIT 3");
if ($result === false) {
exit("Error: " . mysqli_error($con));
}

PHP ordered list from latest post

I have 3 divisions in my table, first is the "ID", second is the "TOPIC", then lastly is the "DATE POSTED". I have this prob, I can't make the new topic be on top. it just goes on the last of the list. Now, how can I make the latest topic that I posted be on top of my lists?
my codes:
$query = mysql_query("SELECT * FROM PythoN_Blog")or die(mysql_error());
echo "<table border='0' width='700'>";
while($result = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td align='center' style='padding-left:30;'>".$result['id']."</td>";
echo "<td align='center' style='padding-left:10;'><a href='#'>".$result['topic']."</a></td>";
echo "<td align='center'>".$result['date']."</td>";
echo "</tr>";
}
echo "</table>";
Change your query to this
"SELECT * FROM PythoN_Blog order by DATE_POSTED DESC";
if your DATE_POSTED column is NOT datetime format (e.g. varchar) then you have to convert it:
"SELECT * FROM PythoN_Blog ORDER BY CONVERT(DateTime, DATE_POSTED,103) DESC"
if your DATE_POSTED column is datetime format then:
"SELECT * FROM PythoN_Blog ORDER BY 'DATE_POSTED' DESC";

sorting row data in table PHP?

i have a table being echo'd from a single query to a table in our database and i get it to echo out the following table;
http://www.skulldogs.com/dev/testview.php
i want it to sort the "yellow" rows under the correct green rows where the "mainToon" name matches for example:
high voltege
--REAL MCCOY
--Cpt Hook
riazall
-- Valeside
my code to echo the above page is;
<?php
$result = mysql_query("SELECT * FROM `members`");
echo "<table border='1'>
<tr>
<th>Character ID</th>
<th>Name</th>
<th>MainToon</th>
<th>toonCategory</th>
</tr>";
while ($row = mysql_fetch_array($result)) {
$characterID = $row['characterID'];
$name = $row['name'];
$startDateTime = $row['startDateTime'];
$logonDateTime = $row['logonDateTime'];
$logoffDateTime = $row['logoffDateTime'];
$location = $row['location'];
$role = $row['role'];
$vouchedBy = $row['vouchedBy'];
$positionHeld = $row['positionHeld'];
$remarks = $row['remarks'];
$afkNotice = $row['afkNotice'];
$toonCategory = $row['toonCategory'];
$mainToon = $row['mainToon'];
$watch = $row['watch'];
if ($toonCategory == 'Main Toon') {
echo "<tr bgcolor='#00FF00'>"; }
else {
echo "<tr bgcolor='#FFFF00'>"; }
echo "<td>" . $characterID . "</td>";
echo "<td>" . $name . "</td>";
echo "<td>" . $mainToon . "</td>";
echo "<td>" . $toonCategory . "</td>";
echo "</tr>";
}
echo "</table>";
?>
at the moment i am not echo the other data until i can figure out how to display this table accordingly. can it be done this way?
this is how i want to display the table;
http://www.skulldogs.com/dev/mockup.php
Add an ORDER BY clause to your sql:
SELECT * FROM `members` ORDER BY toonCategory;
If there are other values above and below "Main Toon", You can order by a boolean:
SELECT * FROM `members` ORDER BY toonCategory = 'Main Toon' DESC;
EDIT:
Now I see what you are after as you have put up the example, try:
SELECT * FROM `members` ORDER BY CONCAT(MainToon, Name);
if the blank spaces are empty strings or:
SELECT * FROM `members` ORDER BY COALESCE(MainToon, Name) DESC, Name;
if the blank rows are null.
Try SELECT * FROM members ORDER BY toonCategory;

Categories