How to re-arrange the data order of a table? - php

I have already uploaded my website to the internet. But I need to change the order of entering data to my table. The new records appear at the bottom of the table instead i need it to be at the top. How can I do this?

Please use sort by (id) OR
When You have updated or created date then use sort by (created or updated) date. That will solve your problem.
Sort By means order by in your query
For Example :
ORDER BY column_name DESC

to display the latest on the top, you need to ORDER your results set by descending order.
But to order by descending order ( To get the latest on the top ) you need to have a column in your table with Integer value ( probably your primary key ) or Date.
Here is an example.
Assume I'm having a table in my DB called, Events and here is the structure of the table
Now i'm going to display the results without ordering, which means now the id=7 result should display on the top. Here is the php code for that
<table class="table table-striped">
<?php
require_once('../DB/connect.php');
$result4= mysql_query("SELECT * from events ");
while ($row= mysql_fetch_array($result4)){
$e_id=$row['id'];
$e_name=$row['ename'];
echo'<tr><td> <img src="../imgs/settings/event5.png" class="img-responsive"></td><td><b>'.$e_name.'</b> </td>';
echo'<td><a button type="button" href="#'.$e_id.'" data-toggle="modal" class="btn btn-primary"> EDIT </a>
</td>
<td><button type="button" class="btn btn-danger"> DELETE </button>
</tr>';
}
?>
</table>
Now the above code will give me a result like this.
you can see my older record display on the top.
Now I'm going to change my sql query a bit to display the latest/newest record on the top.
<table class="table table-striped">
<?php
require_once('../DB/connect.php');
$result4= mysql_query("SELECT * from events ORDER by id DESC ");
while ($row= mysql_fetch_array($result4)){
$e_id=$row['id'];
$e_name=$row['ename'];
echo'<tr><td> <img src="../imgs/settings/event5.png" class="img-responsive"></td><td><b>'.$e_name.'</b> </td>';
echo'<td><a button type="button" href="#'.$e_id.'" data-toggle="modal" class="btn btn-primary"> EDIT </a>
</td>
<td><button type="button" class="btn btn-danger"> DELETE </button>
</tr>';
}
?>
</table>
so the above code will give me a result like this,
Like that, you can display the latest result on the top by using ORDER BY column_name DESC

Related

how to link every user with his own information ( like friends list)

I'm a beginner in mySQL database and html/css development,
I have a database which contain an "association" table and a "FinancialMaterial_assistance" table that contains every single association register, the received assistance, each with it's own id.
So my question is: how to filter and show in my list view every association with it's only own registration? (without showing registration of other association)
In my current listview , every association could see all the received assistance, without filtering or privacy and confidentiality of every one of them.
Thank you for help I will be grateful if you create or give my PHP code or condition. This is my code:
<?php
if(isset($_GET['search'])==false){
$sql="select * from don ORDER BY idSub ASC ";
$result=mysqli_query($con,$sql);
if($result){
$num = 1;
while($ass = mysqli_fetch_array($result)){
$idSub=$ass['idSub'];
$date=$ass['date'];
$nomDon=$ass['nomDon'];
$description=$ass['description'];
$quantite=$ass['quantite'];
$source=$ass['source'];
echo '
<tr>
<th scope="row">'.$num++.'</th>
<td>'.$date.'</td>
<td>'.$nomDon.'</td>
<td>'.$description.'</td>
<td>'.$quantite.'</td>
<td>'.$source.'</td>
<td><button class="btn btn-primary">
<i class="fa fa-pencil" aria-hidden="true"></i>
Edit
</button>
</td>
<td> <button class="btn" >
<i class="fa fa-trash-o" aria-hidden="true" red-color></i>
Delete
</button>
</td>
</tr>
';
} }
?>
by the way I didn't link tables yet

Modal do not change its content because of GET ID

I have to display a content based on their id but get id is not working. But I can see the url that it retrieves the id and it will change if I click the button. Should I force myself to use ajax or there is alternative way to retrieve the id?
I really appreciate any help.
$viewquery= mysqli_query($connection,"SELECT * from orders");
while($row = mysqli_fetch_assoc($viewquery)) {
$id =$row['id'];
$full_name =$row['full_name'];
$email_address =$row['email_address'];
$contact_number =$row['contact_number'];
$address =$row['address'];
$user_id =$row['user_id'];
$reference_number =$row['reference_number'];
$additional =$row['additional'];
$payment_method =$row['payment_method'];
$transaction_status = $row['transaction_status'];
echo' <tr>';
echo '<td>';echo "<a href='#?idrequest=$id'><button type='button'
class='btn btn-primary' data-toggle='modal' data-target='#exampleModalLong'
data-formid='.$id.'>
$reference_number
</button></a>";
echo'</td>';
echo "
<td> $id </td>
<td> $full_name </td>
<td> $email_address </td>
<td> $contact_number </td>
<td> $address </td>
<td> $user_id</td>
<td> $additional </td>
<td> $payment_method </td>
<td> $transaction_status </td>
<td>
<a href='delete.php'>
<i class='material-icons' id='coloricon'>delete </i>
</a>
</td>
</tr>
";
}
?>
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog"
aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-
label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?php
include("conn.php");
$idrequest = $_GET['idrequest'];
$query = "SELECT * from orders where id ='$idrequest'";
$result = mysqli_query($connection,$query);
$view = mysqli_fetch_assoc($result);
$details = $view['summary_all'];
echo $details;
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-
dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
This code above is for modal which I used bootstrap modal.
I really didn't want to write a post on this as there is so much going on that needs to be fixed but your immediate issue is
<a href='#?idrequest=$id'> .... </a>
This hashtag # or pound sign, will create what is called a URL fragment. Now you have to understand that fragments are client side only and are not sent to the server. So anything following the # will not be sent. Therefore there is no way to access this "pseudo" query string. So at the very least toss that #.
<a href='?idrequest=$id'> .... </a>
If you had error reporting turned on you would see a warning for an undefined index.
<?php
error_reporting(-1);
ini_set('display_errors', '1');
Because $_GET['idrequest'] doesn't exist, in this codes current form.
Beyond that, your SQL is wide open for SQLInjection, as I said in the comment if someone put the URL in with
$_GET['idrequest'] = "1' OR 1=1"
$idrequest = $_GET['idrequest'];
"SELECT * from orders where id ='$idrequest'"
Your SQL query is modified to this:
"SELECT * from orders where id ='1' OR 1=1"
Because 1 is always equal to 1 this "hack" will always return some rows. Therefor always use prepared statements.
Beyond that, if this is publicly accessible any user could simply iterate though your user table to look at orders from other users. These may include personal identifiable information, such as phone and address information. You could add the user_id into this search which should be a foreign key on this table anyway. That user id would then come from whatever system you are using for login (not user entered data). Which would only allow them to see their own orders.
I won't even touch on the styling/formatting issues, with wrapping a button in a link. Or this ad-hoc mixing of HTML and HTML in PHP strings, which make it a real challenge to read the code.
Or this
include("conn.php");
Which should be
require "conn.php";
include and require don't need the () and you should use require if the script won't execute without the included file. This being a DB connection, I would say things won't go as planed if that file is missing.
Cheers.

how can I show the default table content as different and when click on button table content should be different in the same table in the UI?

I have a code to display users and I have a delete button for deleting user. When I delete any user, the deleted user will be removed from the user list. In the above I have two buttons one is for create user, and another is to show all deleted user. How can I show the deleted user when I click on "View Deleted User" button in the same below table where it was showing all the users.
Here below is the code
<body class="back-color">
<div class="container">
<div class="table-wrapper">
<div class="table-title">
<div class="row">
<div class="col-sm-7">
<h2><b>Manage Users</b></h2>
</div>
<div class="col-sm-3">
<i class="material-icons">info</i> <span>View Deleted Users</span>
</div>
<div class="col-sm-2">
<i class="material-icons"></i> <span>Add New User</span>
</div>
</div>
</div>
<?php
if($no_of_users>1){
echo '<table class="paginated table table-striped table-hover">
<thead>
<tr>
<th></th>
<th>Username</th>
<th>Email</th>
<th>Role</th>
</tr>
</thead>
<tbody>';
while($userdetails = mysqli_fetch_array($user_details, MYSQLI_ASSOC)){
echo' <tr>
<td></td>
<td>'.$userdetails['username'].'</td>
<td>'.$userdetails['email'].'</td>
<td>'.$userdetails['role'].'</td>
<td>
<i class="material-icons" data-toggle="tooltip" title="Edit"></i>
<i class="material-icons" data-toggle="tooltip" title="Delete"></i>
</td>
</tr>';
}
echo '</tbody>
</table>';
}
?>
</div>
</div>
</body>
I think you need a soft delete option,
Steps:
Create a field is_deleted, a boolean true if user is deleted and
false if not which is false by default, in your users table.
Now on Delete button's click make the is_deleted true.
To list only undeleted users, query the table with condition
is_deleted => false.
To list deleted users, query the table with condition is_deleted => true.
You can also give option for hard delete, which deletes user from table.
You should do like,
//if retrieving undeleted users from table
$sql = "SELECT * FROM users from is_deleted = 0";
//else retrieving deleted users from table
$sql = "SELECT * FROM users from is_deleted = 1";
//$con holding your database connection details
$user_details= mysqli_query($con,$sql);
while($userdetails = mysqli_fetch_array($user_details, MYSQLI_ASSOC)) {
// $userdetails array contains the data of a user
// iterate it and fill your table accordingly
}
Hope this works.

PHP: Filtering SQL query with select options

Hey guys I am a little stuck at the moment, basically I am trying to find out how I can filter my SQL queries however having a bit of trouble understanding how it can be done.
Basically I am listing all of the 'Makes' of the brands I have on my site into a form. I am simply taking the column from my products table that contains every product.
Here is an example of the code, this is the PDO and MySQL selection:
<?php
include('database.php');
try {
$results = $db->query("SELECT Make, Model, Colour, FuelType, Year, Mileage, Bodytype, Doors, Variant, EngineSize, Price, Transmission, PictureRefs, ServiceHistory, PreviousOwners, Options, FourWheelDrive FROM import ORDER BY Make ASC");
} catch (Exception $e) {
echo "Error.";
exit;
}
try {
$filterres = $db->query("SELECT DISTINCT Make FROM import ORDER BY Make ASC");
} catch (Exception $e) {
echo "Error.";
exit;
}
?>
I have added DISTINCT to that block of code as there are duplicate 'Make's' in the SQL column.
Here is the form, as you can see I am using a while loop to display every make in the table into it's own option.
<form>
<select class="form-control select-box">
<option value="make-any">Make (Any)</option>
<?php while($make = $filterres->fetch(PDO::FETCH_ASSOC))
{
echo '
<option value="">'.$make["Make"].'</option>
';
} ?>
</select>
<button href="#" class="btn btn-block car-search-button btn-lg btn-success"><span class="glyphicon car-search-g glyphicon-search"></span> Search cars
</button>
</form>
I am using this code to display the SQL rows into listed results:
<?php while($row = $results->fetch(PDO::FETCH_ASSOC))
{
echo '
<div class="listing-container">
<h3 class="model-listing-title clearfix">'.$row["Make"].' '.$row["Model"].' '.$row["Variant"].'</h3>
<h3 class="price-listing">£'.number_format($row['Price']).'</h3>
</div>
<div class="listing-container-spec">
<img src="'.(explode(',', $row["PictureRefs"])[0]).'" class="stock-img-finder"/>
<div class="ul-listing-container">
<ul class="overwrite-btstrp-ul">
<li class="diesel-svg list-svg">'.$row["FuelType"].'</li>
<li class="saloon-svg list-svg">'.$row["Bodytype"].'</li>
<li class="gear-svg list-svg">'.$row["Transmission"].'</li>
<li class="color-svg list-svg">'.$row["Colour"].'</li>
</ul>
</div>
<ul class="overwrite-btstrp-ul other-specs-ul h4-style">
<li>Mileage: '.number_format($row["Mileage"]).'</li>
<li>Engine size: '.$row["EngineSize"].'cc</li>
</ul>
<button href="#" class="btn h4-style checked-btn hover-listing-btn"><span class="glyphicon glyphicon-ok"></span> History checked
</button>
<button href="#" class="btn h4-style more-details-btn hover-listing-btn tst-mre-btn"><span class="glyphicon glyphicon-list"></span> More details
</button>
<button href="#" class="btn h4-style test-drive-btn hover-listing-btn tst-mre-btn"><span class="test-drive-glyph"></span> Test drive
</button>
<h4 class="h4-style listing-photos-count"><span class="glyphicon glyphicon-camera"></span> 5 More photos</h4>
</div>
';
} ?>
My question is how can I use the selection element to filter the exact 'Make', so for example if the SQL row contains the same 'Make' as the user has selected then I would like the whole row to display into the list and any other makes to not show.
Any code examples to how I can achieve this?
Thanks
You need to have something like:
$where = "";
if (!!$selectedMake) {
$stmt = $db->prepare("SELECT Make, Model, Colour, FuelType, Year, Mileage, Bodytype, Doors, Variant, EngineSize, Price, Transmission, PictureRefs, ServiceHistory, PreviousOwners, Options, FourWheelDrive FROM import ORDER BY Make ASC");
$stmt->execute();
} else {
$stmt = $db->prepare("SELECT Make, Model, Colour, FuelType, Year, Mileage, Bodytype, Doors, Variant, EngineSize, Price, Transmission, PictureRefs, ServiceHistory, PreviousOwners, Options, FourWheelDrive FROM import where Make = ?");
$stmt->execute(array($selectedMake));
}
Note that the order by is not needed in the else block, as you have a unique Make. If your Make is textual, then you might need '%?%' instead of ? in the query.
EDIT:
If I understood well, you still have a problem, namely, you lack the knowledge to determine what the value of $selectedMake should be. First of all, you should have a name for your select tag, so you should have this:
<select class="form-control select-box" name="selected-make">
This way when you submit the form, the value of your selected tag will be sent to the server. So if you want to pass a value when the form submits, you should give the given tag a name.
A form can be get or post. If it is get, then the passed value should be in get. Try it out with:
echo var_dump($_GET);
If it is post, the passed value should be in post. Try it out with:
echo var_dump($_POST);
Finally, you can initialize your $selectedMake like this:
$selectedMake = "";
if (isset($_GET["selected-make"])) {
$selectedMake = $_GET["selected-make"];
} else if (isset($_POST["selected-make"])) {
$selectedMake = $_POST["selected-make"];
}
There are lot of ways to achieve this, but you can use ajax request here.
Send ajax post request to your sql query page with selected option as a post parameter.
On the query page, you can pass that post parameter into where clause of sql query.
Then fill up the table on ajax success response.

Query to show "standard" theme first, then rest of the results

I have following query to show a list of installed themes. But users can make one theme as standard and I wonder if there is a way to pin the standard theme at the top?
Example:
Design 1
Design 2 (Standard)
Design 2 is marked as standard, and should be visible at top and not everywhere else.
Design 2 (Standard)
Design 1
Design 3
Design 4
SQL/PHP
$find_themes = mysqli_query($con, "SELECT * FROM installed_designs WHERE u_id='$user[id]' order by tmp_id desc");
while($themes = mysqli_fetch_array($find_themes))
{
echo '<tr>
<td>'.$themes["name"].'</td>
<td>'; if($themes["standard"]=='1') { echo '<b>Standard</b>'; } echo '</td>
<td>
<div class="btn-group pull-right">
<button type="button" class="btn btn-default btn-sm btn-grey own-toggle" data-toggle="dropdown">
Velg handling <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li>Sett som standard</li>
<li>Endre HTML/CSS</li>
<li>Historikk</li>
<li class="divider"></li>
<li>Slett og fjern</li>
</ul>
</div>
</td>
</tr>';
}
Alter your query like this:
SELECT
*
FROM
installed_designs
WHERE
u_id='$user[id]'
ORDER BY
standard DESC,
tmp_id DESC
I noticed you have a standard flag in your table so use two ordering rules: first by standard descending (standard will go first, then non-standards) and then by your tmp_id (if the standard value is the same this rule will be applied).

Categories