Jump to content

[PHP+MYSQL] Afisare date sortate automat


uS H1gH
 Share

Recommended Posts

Tutorial cum sa 'scoti' date dintr-o baza de date mysql cu PHP sortate automat in functie de un camp.

Acest tutorial este continuare la http://forum.b-zone.ro/topic/246545-phpmysql-citire-si-afisare-date-dintr-o-baza-de-date/ .

 

Fata de tutorialul precedent, acuma vom modifica comanda "SELECT".

Sortarea poate fi alfabetica, numerica sau cronologica.

 

Data trecuta comanda arata asta:

$comanda = "SELECT * FROM users"

Acuma spunem ca vrem sa ii afisam automat in ordine alfabetica:

Comanda va deveni:

$comanda = "SELECT * FROM users ORDER BY nume"

In momentul acest el sorteaza ascendent, pentru a sorta descendent trebuie sa mai adaugam 'DESC' in comanda, de exemplu:

$comanda = "SELECT * FROM users ORDER BY nume DESC"

Daca am fi avut un camp ID care era de tip int si reprezenta id-ul userului, comanda era aproximativ identica, de exemplu:

$comanda = "SELECT * FROM users ORDER BY id" // Ascendent
$comanda = "SELECT * FROM users ORDER BY id DESC" // Descendent

Codul complet:

<html>
<head>
<title>Citire date dintr-o baza de date in ordine alfabetica</title>
</head>
<body>


<?php
$host = "mysqlserver.ro";
$user = "Testuser";
$pass = "parola";


mysql_connect($host, $user, $pass) or die (mysql_error ());


mysql_select_db("bazadedate") or die(mysql_error());


$comanda = "SELECT * FROM users ORDER BY nume";


$rezultat = mysql_query($comanda);


while($row = mysql_fetch_array($rezultat)) 
{
echo $row['nume'] . "<br />";
}


mysql_close();
?>
</body>
</html>
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.