Page 1 of 1

alphabetical order

Posted: Tue Mar 25, 2003 11:20 am
by micknic
Hi guys

When I'm doing a request on a database with PHP I print the result in an array. I would like to know if it's possible to print the reslut (names) in an alphabetical order????

:P :o :lol:

Posted: Tue Mar 25, 2003 12:04 pm
by daven
Just do an ORDER BY in your query.

Ex: $SQL="SELECT Name FROM Table ORDER BY Name [DESC]";

If you use DESC, the order will be descending. Otherwise, the order will be ascending.

Posted: Tue Mar 25, 2003 1:41 pm
by micknic
I tried what you suggested me but I think it doesn't work. But I have to use the syntax:

$sql="SELECT * FROM table WHERE ( ...) ORDER BY name[desc]";

Why doesn't it work???

Posted: Tue Mar 25, 2003 2:10 pm
by twigletmac
Try:

Code: Select all

$sql = "SELECT * FROM table ORDER BY name DESC";
Mac

Posted: Tue Mar 25, 2003 2:35 pm
by micknic
Sorry but it doesn't work. This is the syntax I use:
$requete="SELECT * FROM beneficiaire WHERE (login='$lg' AND pays='Belgique') ORDER BY nombenef DESC";

May be the problem come from the fact that after I use:
if ($resultat = mysql_db_query("maredsous", $requete, $connexion)) {
while($acces = mysql_fetch_array($resultat)) {


What about using something like sort or natcasesort ???

Posted: Tue Mar 25, 2003 3:38 pm
by daven
Does the query work without the ORDER BY clause?

If I am thinking correctly, putting a PHP variable in single quotes causes the variable to not be evaluated. So:
"SELECT * FROM beneficiaire WHERE (login='$lg' AND pays='Belgique') ORDER BY nombenef DESC"
will be read as (notice the login= statement):
"SELECT * FROM beneficiaire WHERE (login='' AND pays='Belgique') ORDER BY nombenef DESC".

Try concatenating the strings instead:
"SELECT * FROM beneficiaire WHERE (login='".$lg."' AND pays='Belgique') ORDER BY nombenef DESC";

Warning: I have been doing a lot of PERL and C coding recently, so it is entirely possible that my PHP syntax/theory is off a bit.

Posted: Wed Mar 26, 2003 2:54 am
by twigletmac
daven wrote:Warning: I have been doing a lot of PERL and C coding recently, so it is entirely possible that my PHP syntax/theory is off a bit.
In this case it is - the string is actually double quoted so the variables will be just fine. It would be a different case, however, if the code looked like this:

Code: Select all

$sql = 'SELECT * FROM beneficiaire WHERE (login=''$lg'' AND pays='Belgique') ORDER BY nombenef DESC';
because then the result would be:

Code: Select all

SELECT * FROM beneficiaire WHERE (login='$lg' AND pays='Belgique') ORDER BY nombenef DESC
Mac

Posted: Wed Mar 26, 2003 2:55 am
by twigletmac
micknic wrote:Sorry but it doesn't work.
What doesn't work - does it not sort at all, does it sort but incorrectly, do you get an error somewhere. It's difficult to help when the problem is just that something 'doesn't work'.

Mac

Posted: Wed Mar 26, 2003 4:45 am
by micknic
I'm going to give you my full syntax:

Code: Select all

<?php
$requete="SELECT * FROM beneficiaire WHERE (login='$lg' AND pays='Belgique') ";
if ($resultat = mysql_db_query("maredsous", $requete, $connexion)) {
	while($acces = mysql_fetch_array($resultat)) {
		
		$nump=$acces["numbenef"];
		$nombenef=$acces["nombenef"];
		$rue=$acces["ruebenef"];
		$bal=$acces["balbenef"];
		$cp=$acces["cpost"];
		$local=$acces["localbenef"];
		$pays=$acces["pays"];
		$cs1=$acces["cs1"];
		$cs2=$acces["cs2"];
		$cs3=$acces["cs3"];?>
		<table width="50%" border="1" cellspacing="2">
		<tr>
			<td width="30%"><?php echo $nombenef; ?></td>
			<td><form action="maredsous2.php" method="post"> <input type="submit" name="parti" value="Choisir">
		<input type="hidden" name="lg" value="<?php echo $lg; ?>"> 
		<input type="hidden" name="nombenef" value="<?php echo $nombenef; ?>">
		<input type="hidden" name="cs1" value="<?php echo $cs1; ?>">
		<input type="hidden" name="cs2" value="<?php echo $cs2; ?>">
		<input type="hidden" name="cs3" value="<?php echo $cs3; ?>">
		<input type="hidden" name="rue" value="<?php echo $rue; ?>">
		<input type="hidden" name="bal" value="<?php echo $bal; ?>">
		<input type="hidden" name="cp" value="<?php echo $cp; ?>">
		<input type="hidden" name="local" value="<?php echo $local; ?>">
		<input type="hidden" name="pays" value="<?php echo $pays; ?>">
		</form>
		</td>
		<td>
		<form action="modif.php" method="post">
		<input type="submit" name="change" value="modifier">
		<input type="hidden" name="nump" value="<?php echo $nump; ?>">
		<input type="hidden" name="cs1" value="<?php echo $cs1; ?>">
		<input type="hidden" name="cs2" value="<?php echo $cs2; ?>">
		<input type="hidden" name="cs3" value="<?php echo $cs3; ?>">
		<input type="hidden" name="lg" value="<?php echo $lg; ?>"> 
		<input type="hidden" name="nombenef" value="<?php echo $nombenef; ?>">
		<input type="hidden" name="rue" value="<?php echo $rue; ?>">
		<input type="hidden" name="bal" value="<?php echo $bal; ?>">
		<input type="hidden" name="cp" value="<?php echo $cp; ?>">
		<input type="hidden" name="local" value="<?php echo $local; ?>"></td>
		</tr>
		</table>
		</form>
		<p>
		<p>
		<p>
		<?php
Whith this syntax everuthing is ok but if I ad ORDER BY nombenef DESC and that I execute the request nothing appear on the screen.


Help help help help

Posted: Wed Mar 26, 2003 5:05 am
by twigletmac
For one thing you shouldn't be using mysql_db_query():
PHP Manual - mysql_db_query() wrote:Note: This function has been deprecated since PHP 4.0.6. Do not use this function. Use mysql_select_db() and mysql_query() instead.
Try:

Code: Select all

@mysql_select_db('maredsous') or die(mysql_error());

$requete = "SELECT numbenef, nombenef, ruebenef, balbenef, cpost, localbenef, pays, cs1, cs2, cs3 FROM beneficiaire WHERE login='$lg' AND pays='Belgique' ORDER BY nombenef DESC";
@$resultat = mysql_query($requete) or die(mysql_error().'<p>'.$requete.'</p>');

if (mysql_num_rows($resultat) > 0) {
    while ($acces = mysql_fetch_assoc($resultat)) {
       $nump = $acces['numbenef'];
       // the rest of the code
    }
} else {
    echo 'The query returned no results.';
}
For more info:
http://www.php.net/manual/en/function.m ... ect-db.php
http://www.php.net/manual/en/function.mysql-query.php
http://www.php.net/manual/en/function.mysql-error.php
http://www.php.net/manual/en/function.m ... m-rows.php
http://www.php.net/manual/en/function.die.php

Mac

Posted: Wed Mar 26, 2003 5:24 am
by micknic
ithank you very much it works but on the wrong side (it begin with letter Z and end with letter A instead of beginnig with A and finish with Z)


see you

Posted: Wed Mar 26, 2003 5:27 am
by twigletmac
Then you need to replace DESC (descending) with ASC (ascending).

Mac

Posted: Wed Mar 26, 2003 5:31 am
by micknic
thank you