Forum Moderators: coopster

Message Too Old, No Replies

Create a ranking method?

How to rank listings in a directory using complex rules

         

ashishp

5:57 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



Hi,

I am trying to have a directory of businesses on my site, but I do not just want to rank via most hits or last viewed or clicked. I want to make a ranking algorithm like a search engine.

How does one go about creating a ranking algorithm in PHP and then order the data in a database based on the calculated weightage from the various rules.

For eg:

Business has website? if yes then 2 points or -1 if no
Business is verified? if yes then 3 points or -1 if no
Business has uploaded logo? then 2 point or -1 if no

then the total points for a listing is computed and then the results are ranked based on score.

Any pointers anyone?

Thanks!

-- Ashish

dreamcatcher

2:41 pm on Nov 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi ashishp,

Did you find a solution for this? I would assume you would need to use sessions and total up as you go along, based on what input was entered. Then enter the total into the database with the rest of the information that was gathered. Then it would be a simple case of ordering by total.

dc

panos

5:38 pm on Nov 1, 2007 (gmt 0)

10+ Year Member



you can use arrays:

$query=mysql_query("select id, website, verified, logo from business");

while ($aquery=mysql_fetch_array($query))
{
$points=0;
$id=$aquery['id'];
$website=$aquery['website'];
$verified=$aquery['verified'];
$logo=$aquery['logo'];

$array[$id]['website']=$website;

//wesite
if (!empty($website)){
$points=$points+2;
}
else
{
$points=$points-1;
}

//verified
if ($verified==1){
$points=$points+3;
}
else
{
$points=$points-1;

}

//logo
if (!empty($logo)){
$points=$points+2;
}
else
{
$points=$points-1;

}
$business[$id]=$points;
}

//sort array
arsort($business, SORT_NUMERIC);

//create array with business ids
foreach ($business as $key => $row) {
$ids[]=$key;
}

now you have an array with ids sorted by rank

ashishp

4:31 pm on Nov 3, 2007 (gmt 0)

10+ Year Member



Hi dreamcatcher,

Thank you for enquiring. :)

and panos thank you for the reply.

I wrote a script that basically does what panos described, only it updates the score in the DB for each listing. I am now running that script weekly to compute the score.

sort of my own weekly google dance! ;)

I was wondering if any one else has any better idea or way to reduce the intensity of the task. it takes long/load currently.

Thanks again!

panos

6:03 pm on Nov 3, 2007 (gmt 0)

10+ Year Member



Why do you register the results in the database? Just calculate the ranks on the fly ;)

ashishp

6:29 am on Nov 14, 2007 (gmt 0)

10+ Year Member



The reason is that there are 35000 listings to be ranked and at any time I am displaying only 10 listings.

Am I going the wrong way?

panos

4:20 pm on Nov 14, 2007 (gmt 0)

10+ Year Member



... 35000 listings ...

Am I going the wrong way?

No :)

whoisgregg

4:34 pm on Nov 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it takes long/load currently.

Instead of recalculating all 35,000 at one time, split up the task into more manageable chunks. Here's some different ideas for how to do that.

  1. Add a "group" column to your db and set them up into group 1,2,3,4,5,6,7 and then just update one group per day.
  2. Add an "hour" column to your db and set them up into group 1-24. Update one group per hour.
  3. Use some data already in the table to make groups (state, first letter of name, day of week record was created, etc.)
  4. Update the rank anytime a record is changed. Or, any time it is viewed.
  5. On every page load of your site, update a random 10 records. Or, set a "rank_last_updated" timestamp column on the records and update the ten that haven't been updated for the longest time. Make sure to update that timestamp or else you'll keep updating the same ten ;)

ashishp

4:30 pm on Nov 16, 2007 (gmt 0)

10+ Year Member



Excellent tips, whoisgregg!

I especially love the timestamp idea I am combining that with everytime a record is changed.

Since the rank is based on information given by the user, everytime they update their profile, the rank gets updated and the timestamp will assign a rank to the existing ones. :)

I cant thank you enough, for helping me see the obvious! :)

brotherhood of LAN

1:32 am on Nov 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Another alternative; If the range of possible scores is lower than your # of records, consider updating per score rather than per record.

e.g. a 10K record database with 10 possible scores, updating individual records results in 10K UPDATEs, updating by unique score results in 10 updates.