PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Mon Jan 31, 2005 2:54 am
i have created a simple search script for my site...nothing flashy.
Anyway. During the search, i store the number of matches for the serch term and the path to that page in an array. A sample of my array looks like this.
Code: Select all
Array
(
ї0] => Array
(
їpath] => /data/web/_o/_c/_o/oconnordesign.co.uk/public/tsys/content/about/about_tsys.php
їoccureneces] => 4
)
ї1] => Array
(
їpath] => /data/web/_o/_c/_o/oconnordesign.co.uk/public/tsys/content/about/global_operations.php
їoccureneces] => 16
)
ї2] => Array
(
їpath] => /data/web/_o/_c/_o/oconnordesign.co.uk/public/tsys/content/about/in_europe.php
їoccureneces] => 9
)
ї3] => Array
(
їpath] => /data/web/_o/_c/_o/oconnordesign.co.uk/public/tsys/content/benefits/benefits.php
їoccureneces] => 5
)
ї4] => Array
(
їpath] => /data/web/_o/_c/_o/oconnordesign.co.uk/public/tsys/content/career_opportunities/career_opportunities.php
їoccureneces] => 3
)
ї5] => Array
(
їpath] => /data/web/_o/_c/_o/oconnordesign.co.uk/public/tsys/content/contact/contact.php
їoccureneces] => 4
)
ї6] => Array
(
їpath] => /data/web/_o/_c/_o/oconnordesign.co.uk/public/tsys/content/culture/history.php
їoccureneces] => 8
)
)
I need to be able to sort the array so the entry with the most number of occurences is first descending the the lowest number of occurences.
1) Am i storing the values in the best way?
2) How to sort the array on the value 'ocurrences'?
Thanks
Mark
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Jan 31, 2005 7:17 am
usort()
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Mon Jan 31, 2005 8:57 am
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Jan 31, 2005 9:20 am
I don't remember the exact return info, but it's the same as doing strcmp()
Code: Select all
function mySort($a, $b)
{
if( $aї'occureneces'] > $bї'occureneces'] )
return -1;
elseif( $aї'occureneces'] < $bї'occureneces'] )
return 1;
else return strcmp( $aї'path'], $bї'path'] );
}
usort( $array, 'mySort' );
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Mon Jan 31, 2005 10:36 am
it bloody works!!!
Cheerz mate
magicrobotmonkey
Forum Regular
Posts: 888 Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA
Post
by magicrobotmonkey » Mon Jan 31, 2005 1:04 pm
another, perhaps more efficient option would be to have an array like
Code: Select all
Array(
їpath1] => occurences
їpath2] => occurences
}that way you can use a simple sort on it and it may be easier to display.
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Feb 01, 2005 2:44 am
yes magicrobotmonkey, that is how i did have it, but i failed to mention there is actually going to be more info for each search result like page title etc.