| 
	
 | 
 Posted by Erwin Moller on 08/26/05 16:32 
Karin Jensen wrote: 
 
> Erwin Moller wrote: 
>>Karin Jensen wrote: 
>>> Hi 
>>> I am running a PHP program that connects to an Access 2000 
>>> database via ODBC: 
>>>   $results = odbc_exec($connection_id, $sql_select); 
>>> Is it possible to sort the contents of $results? I wish to use a 
>>> regex-based sort order that you can't do in Access 2000* SQL 
>>> with "ORDER BY". 
>> 
>> Hi, 
>> 
>> PHP is so friendly, you can write your own comparision-functions, 
>> which you can let PHP call to define how to sort. 
>> :-) 
>> 
>> Have a look at usort 
>> http://nl2.php.net/usort 
>> 
>> See also uasort(), uksort(). 
>  
> Thanks, Erwin!  Sorry to ask more, but the question I was getting at 
> was whether (in my example) $results would have to be dealt with in 
> a special way, as it is from an ODBC function. 
>  
> I am new to PHP and not sure what sort of object $results is or how 
> to find out what sort of object it is. 
 
Hi, 
 
Yes, your $result is a special case: it is a 'resource'. 
I wouldn't be surprised if you could treat it like an array after some  
fiddling around, but I do not know. 
Safest bet is to copy all the rows to your own array-structure, then use  
that structure to do the sorting. 
 
But a fair warning, if you have little experience with PHP and arrays in  
PHP, you better take a day or two to get some experience. 
PHP is very powerfull when it comes to arrays, but things can get  
confusing/intimidating in the beginning. 
 
eg: Try to create your own datastructures, using strings as keys. 
 
$myArr["userid12"]["name"] = "john"; 
$myArr["userid12"]["city"] = "Amsterdam"; 
$myArr["userid12"]["admin"] = "N"; 
$myArr["userid12"]["articles"][] = 21; 
$myArr["userid12"]["articles"][] = 26; 
 
$myArr["userid23"]["name"] = "Bert"; 
$myArr["userid23"]["city"] = "Los Angeles"; 
$myArr["userid23"]["admin"] = "Y"; 
$myArr["userid23"]["articles"][] = 17; 
 
etc. 
 
PHP can use the keys ("useridxx") as material to do the sorting AND keep the  
underlying arrays coupled to the keys. 
 
I (and others) find that kind of datarepresentation extremely powerfull to  
use and easy to implement. 
(Note: My example is a bit over the top, but illustrates the use of  
associative arrays to create/store all kind of datastructures.) 
 
Well, just read up at php.net about arrays, it is all there. :-) 
 
Good luck. 
 
Regards, 
Erwin Moller 
 
>  
> PHP clearly knows that $results is ODBC-derived, as you have to 
> extract data/info from it using special functions (odbc_fetch_row, 
> odbc_result and so on).  I was wondering if this affected how you 
> would sort it. 
>  
> Many thanks, 
>  
> Karin
 
[Back to original message] 
 |