Answer the question
In order to leave comments, you need to log in
How to sort alphabetically and save by ID?
Good day. I have a MySQL database using PhpMyAdmin.
There is such a structure:
id, name, price
With the command
I sort everything alphabetically, but how can I change the id according to this sorting?
That is, I get as a result of the request:
5, Car, 1.600.000
8, Apartment, 22.455.000
2, Hotel, 1.500.000.000
19, Yacht, 324.666.000
But I need to make their ID 1,2,3,4 , according to the alphabet. Tell me how to sort and immediately change their ID in order? Thank you. SELECT * FROM `product` ORDER BY name
Answer the question
In order to leave comments, you need to log in
It's in HTML
_______________________________________________
This is in css
.line-title {
font-size: 20px;
margin-bottom: 10px;
padding-top: 1px; /* Allows for hr margin to start at top of h2 */
}
/* clearfix for floats */
.line-title:after {
content: "";
display: table;
clear: both;
}
.line-title span {
padding-right: 10px;
float: left;
}
.line-title hr {
border:1px solid #DDD;
border-width: 1px 0 0 0;
margin-top: 11px;
}
<div class="bproduct">
<h2><span>Название</span></h2>
</div>
.bproduct>h2 {
height: 10px;
border-bottom: 3px solid #ED1C24;
font-size: 14px;
color: #ED1C24;
}
.bproduct>h2 span {
float: left;
padding: 0 10px 0 0;
background-color: #fff;
font-size: 14px;
color: #ED1C24;
}
It can be easier - https://jsfiddle.net/trushka/zs1pknsj/
Only you need to explicitly set the font sizes and line-height, ato floats vertically in different browsers
$sql = "SELECT * FROM `product` ORDER BY name";
$data = $mysqli->query($sql)->fetch_all(MYSQLI_ASSOC);
foreach ($data as $i => $row) {
echo ($i+1), $row['name'], $row['price'];
}
ID to change, really, nafig it is not necessary. You, in fact, need to display the serial number of the line in the request, if I understand correctly. It's not hard:
SELECT row_number() over (), product.* FROM product ORDER BY name
No way, because the index is auto-incrementing and its values cannot be changed. You need to add one more field in which to store the serial number for your purposes. Or just substitute the index number of the element in the array + 1, as suggested by FanatPHP above .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question