[PHP-DB] Re: assist with SORT array

2002-09-10 Thread Adam Royle

Bartosz was correct in saying that you should use your query to sort, 
rather than sorting array. And the array is actually sorted, although 
it keeps its index. See the manual reference on arrays to see how they 
work. To iterate over sorted arrays, use the foreach() construct.

Anyway, the better way to do it (using only 1 sql query), would be 
using the following code.

$rest_IDs = implode(',', $restaurants);
$sql = SELECT name FROM restaurants WHERE id IN ('$rest_IDs') ORDER BY 
name;
$result = mysql_query($sql) or die($sql . mysql_error());
while($row=mysql_fetch_object($result)) {
  $rest_array[] = ucwords(stripslashes($row-name));
}



And its sorted!!! If you want to check what values an array holds, use 
a combination of header(Content-Type: text/plain); and 
print_r($rest_array);

Have fun!

Adam


 I  have a head scratcher.. prob just a glitch on my side, but somehow 
 I am confused with the results.

 I have a php script pulling from a mysql database for id/names which 
 is getting choices through a check box form. All of that works just 
 fine, but I wanted to display the results sorted by title. My snippet 
 of code is:

 // $restaurants = array() of ids back from form
 for($i=0;$isizeof($restaurants);$i++) {
 $rest_id = $restaurants[$i];
 $sql=select name from restaurants where id=$rest_id;
 $result=mysql_query($sql) or die($sql . mysql_error());
 while($row=mysql_fetch_object($result)) {
 $rest_name .= ucwords(stripslashes($row-name)) . ;;
 }
 }
 $rest_array=explode(;,$rest_name);

 $rest_sort=sort($rest_array); // -- the problem line

 die(checking: $rest_namebrbr size = .sizeof($rest_array) 
 .  sorted:  . sizeof($rest_sort));

 the results come back with 6 in the $rest_array but only 1 in the 
 $rest_sort array! How is that??

   checking: Hi-Ho Tavern;Village Inn;Speak Easy;Duane's House Of 
 Pizza;Atomic Coffee;
   size = 6 sorted: 1

 What I am trying to accomplish is an array('Atomic Coffee', 'Duane's 
 House Of Pizza','Hi-Ho Tavern','Speak Easy','Village Inn')

 Help??


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] Re: assist with SORT array

2002-09-09 Thread Bartosz Matosiuk

I'm sorry if I haven't undersood your post well, but my english is not very
good. Anyway what I understood is that you want to sort the result by the
name of restaurant. So mayby it would be a better idea not to sort the
result array, but sort the query:
SELECT name FROM restaurants WHERE id=$rest_id ORDER BY name;
Using that statement you will receive array with sorted restaurants names.

Terry Romine [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 I  have a head scratcher.. prob just a glitch on my side, but somehow I
 am confused with the results.

 I have a php script pulling from a mysql database for id/names which is
 getting choices through a check box form. All of that works just fine,
 but I wanted to display the results sorted by title. My snippet of code
 is:

 // $restaurants = array() of ids back from form
  for($i=0;$isizeof($restaurants);$i++) {
  $rest_id = $restaurants[$i];
  $sql=select name from restaurants where id=$rest_id;
  $result=mysql_query($sql) or die($sql . mysql_error());
  while($row=mysql_fetch_object($result)) {
  $rest_name .= ucwords(stripslashes($row-name)) . ;;
  }
  }
  $rest_array=explode(;,$rest_name);

  $rest_sort=sort($rest_array); // -- the problem line

  die(checking: $rest_namebrbr size =
 .sizeof($rest_array) .  sorted:  . sizeof($rest_sort));

 the results come back with 6 in the $rest_array but only 1 in the
 $rest_sort array! How is that??

 checking: Hi-Ho Tavern;Village Inn;Speak Easy;Duane's House Of
 Pizza;Atomic Coffee;
 size = 6 sorted: 1

 What I am trying to accomplish is an array('Atomic Coffee', 'Duane's
 House Of Pizza','Hi-Ho Tavern','Speak Easy','Village Inn')

 Help??

 Terry Romine
 Web Developer
 JumpInteractive.com




-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php