Jump to content
xisto Community
ginginca

How To Sort A List Alphabetically new to PHP

Recommended Posts

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>'; }  ?>

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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,

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • 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.