Jump to content
xisto Community
ginginca

How To Sort A List (new To Php)

Recommended Posts

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!

Share this post


Link to post
Share on other sites

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. :D

vujsa

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.