HOME       >>       Programming

How To Sort A List (new To Php)


ginginca

I have a PHP query that produces a list:

// Query the Database $specific_value = 'Industrial';$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>'; }

Can someone tell me how to sort the result alphabetically?

Thanks!

vujsa

I have a PHP query that produces a list:

// Query the Database $specific_value = 'Industrial';$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>'; }

Can someone tell me how to sort the result alphabetically?

Thanks!

Well, it would be easier to sort the data before PHP gets it since the sort feature is built into MySQL.
Just modify your query string like so:
$query = "SELECT * FROM table_links WHERE cat = '" . mysql_real_escape_string($specific_value) . "' ORDER BY 'manufacturer' ASC";}

If this isn't what you want, then I suggest that you post a NEW topic in the PHP forum.

Hope This Helps.

vujsa

ruben1405241511

Hi Ginginca!
The easiest way would be to create a new array from the MySQL results:

while($row = mysql_fetch_array($result)) { $mfr[$row['mfr_url']] = $row['manufacturer'];}
This requires the mfr_url stuff to be unique though, otherwise it won't work the way you want it. You could then use a multiple layer array.
Afterwards you can use the function asort() to sort your array.

Hope you understood,
Ruben

vujsa

Hi Ginginca!

The easiest way would be to create a new array from the MySQL results:

while($row = mysql_fetch_array($result)) { $mfr[$row['mfr_url']] = $row['manufacturer'];}
This requires the mfr_url stuff to be unique though, otherwise it won't work the way you want it. You could then use a multiple layer array.

Afterwards you can use the function asort() to sort your array.

 

Hope you understood,

Ruben

 

Like I said, if you don't want to or can'n use the MySQL method that I explained above, then you really need to post a question in the PHP forum since this really is a PHP question.

 

There are various methods for sorting data in PHP.

 

vujsa


ginginca

There are various methods for sorting data in PHP.
vujsa


The reason I'm having trouble in the first place *grin*

The mfr_url is not q unique string believe it or not. I was surprised to see that on the list I was given I have various companies all using the same web site ... all divisions of the same company I guess. I'll look at your reply and see if it helps. Thank you very much!

vujsa

The reason I'm having trouble in the first place *grin*
The mfr_url is not q unique string believe it or not. I was surprised to see that on the list I was given I have various companies all using the same web site ... all divisions of the same company I guess. I'll look at your reply and see if it helps. Thank you very much!


Killing me here. My point is that this is NOT the PHP forum, this is the MySQL forum. If you want help with a PHP sorting question, then ask it in the PHP forum. You have an array of data that needs to be sorted, that has nothing to do with the database at this point in time..

I should have just moved the topic to begin with I guess.

If you want further help with PHP sorting, do it in a new topic in the PHP section.

This topic is closed now!

vujsa


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.