HOME       >>       Programming

How To Sort A List Alphabetically new to PHP


ginginca

My query is returning a list of manufacturers with a hyperlink to their web site.

I would like to LIST them alphabetically but can't seem to figure it out. Can someone lend me a hand?

<?php // Query the Database $specific_value = 'Industrial';$query = "SELECT * FROM table_links WHERE cat = '" . mysql_real_escape_string($specific_value) . "' ORDER BY 'manufacturer' ASC";$query = "SELECT * FROM table_links WHERE cat = '" . mysql_real_escape_string($specific_value) . "'";$result = mysql_query($query);if(!$result) die("ERROR: " . mysql_error());while($row = mysql_fetch_array($result)) { echo '<a href="'.$row['mfr_url'].'">'.$row['manufacturer'].'</a><br>'; } ?>


vujsa

I can't really explain it better than this: http://forums.xisto.com/no_longer_exists/

 

Here is the way it works for your script:

<?php // Query the Database $specific_value = 'Industrial';$query = "SELECT * FROM table_links WHERE cat = '" . mysql_real_escape_string($specific_value) . "' ORDER BY 'manufacturer' ASC";$query = "SELECT * FROM table_links WHERE cat = '" . mysql_real_escape_string($specific_value) . "'";$result = mysql_query($query);if(!$result) die("ERROR: " . mysql_error());$row = mysql_fetch_array($result);foreach($row as $res) $sortAux[] = $res['manufacturer'];array_multisort($sortAux, SORT_ASC, $row);$i = 0;while($i < count($row)) { echo '<a href="'.$row[$i]['mfr_url'].'">'.$row[$i]['manufacturer'].'</a><br>'; $i++;} ?>

I think that is what you are looking for.

 

Changing the value in $res[] will allow you to sort by something other than 'manufacturer'.

 

I'm sorry but I can't further explain the entire process to you as I tend to have difficulty wording the logic in a way that others understand. Probably the same reason I never understood when others tried to explain this type of multi-dimintional array. I have to see the code and learn from that.

 

I hope this helps.

 

vujsa


ginginca

Thank you for your reply.This gave me a strange result of:7CIhAs for understanding the process, I have decided to go back to college and take PHP, starting next month. I am really enjoying what I am learning so far but feel like I'm at a loss since there is "so much" that PHP can do.


TavoxPeru

My query is returning a list of manufacturers with a hyperlink to their web site.
I would like to LIST them alphabetically but can't seem to figure it out. Can someone lend me a hand?

<?php // Query the Database $specific_value = 'Industrial';$query = "SELECT * FROM table_links WHERE cat='" . mysql_real_escape_string($specific_value) . "' ORDER BY 'manufacturer' ASC";$query = "SELECT * FROM table_links WHERE cat = '" . mysql_real_escape_string($specific_value) . "'";$result = mysql_query($query);if(!$result) die("ERROR: " . mysql_error());while($row = mysql_fetch_array($result)) { echo '<a href="'.$row['mfr_url'].'">'.$row['manufacturer'].'</a><br>'; } ?>
To order alphabetically simply remove the quoutes of your order by clause, and also delete your second $query variable assignment because if you dont do it, this will replace your first assignment.

Your statement will be:
<?php // Query the Database$specific_value = 'Industrial';$query = "SELECT * FROM table_links WHERE cat = '" . mysql_real_escape_string($specific_value) . "' ORDER BY manufacturer ASC";$result = mysql_query($query);if(!$result) die("ERROR: " . mysql_error());while($row = mysql_fetch_array($result)) { echo '<a href="' . $row['mfr_url'] . '">' . $row['manufacturer'] . '</a><br>'; }?>
Best regards,

ginginca

Thanks. I'll give that a try!


ginginca

Thank you very much TavoxPeru. It worked.

 

In the statement that has ORDER BY in it ...

 

why is the there both a " (quotes) and a ' (apostrophe) ?

 

 

Sorry my understanding of PHP is very new.


TavoxPeru

Thank you very much TavoxPeru. It worked.

 

In the statement that has ORDER BY in it ...

 

why is the there both a " (quotes) and a ' (apostrophe) ?

Sorry my understanding of PHP is very new.

 


In PHP and SQL, the " and the ' are use to specify strings literals like the $query variable, in this specific case you are creating a SQL statement that will select some links of a category type whick also is a string, thats why you use this.

 

Best regards,



VIEW ORIGINAL REGISTER GET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.