Rank Result data set
i am building meta search tool which get results from different sources in response to user query. i have saved the results in array of object having information like title, description , release date etc.before showing it on interface i want to rank them, so that most relevant result should be on top just like search engines. but i am new to ranking and don't know about it. so kindly guide me in this matter which ranking algorithm i should follow or any useful link for help.
I think you need to add "weighting" column (can be null) on your array of Object and just before to show the elements you need to loop on weighting if they got one.
Then your results will be shown in first and if no weight just the normal display.
Here an example :
<?php
//Function to compare weightings
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$searches = array(
'songs'=> array(
0 => array(
'title' => 'coldplay',
'weight'=> 3
),
1 => array(
'title' => 'eminem',
'weight'=> 2
),
2 => array(
'title' => 'rihanna',
'weight'=> 2
),
3 => array(
'title' => 'shakira',
'weight'=> 1
),
4 => array(
'title' => 'nirvana',
'weight'=> null
),
5 => array(
'title' => 'acdc'
)
),
);
//this foreach is used to apply the weighting on itterations
foreach($searches['songs'] as $key => $search){
//if no weight of weight is null
if(array_key_exists('weight', $search) && $search['weight']){
$array_by_weight[$key]['weight'] = $search['weight'];
$array_by_weight[$key]['title'] = $search['title'];
}else{
$array_by_weight[$key]['weight'] = 5; //Value max of weighting
$array_by_weight[$key]['title'] = $search['title'];
}
}
//We use our function to compare and sort our array
uasort($array_by_weight, 'cmp');
//display
foreach($array_by_weight as $songs){
echo $songs['title'].' | ';
echo $songs['weight'].PHP_EOL;
}
Output :
shakira | 1
eminem | 2
rihanna | 2
coldplay | 3
acdc | 5
nirvana | 5
I hope it can help you.
Related
I have a list of (xml) items, each item has a category and a name:
Category | Name
1 | Joe
2 | Carol
3 | Bruce
1 | Michael
1 | Alan
2 | Brian
I want to sort the names ascending within categories descending like so:
Category | Name
3 | Bruce
2 | Brian
2 | Carol
1 | Alan
1 | Joe
1 | Michael
with the aim of creating a Select dropdown on a web page with each category as an OptGroup and the names sorted within the OptGroup.
I have very little experience with PHP, I think I need to sort merge arrays, but after many hours trying to understand how to, I'm not making much progress.
Any help greatly appreciated
$data[] = array('category' => 1, 'name' => 'Joe');
$data[] = array('category' => 2, 'name' => 'Carol');
$data[] = array('category' => 3, 'name' => 'Bruce');
$data[] = array('category' => 1, 'name' => 'Michael');
$data[] = array('category' => 1, 'name' => 'Alan');
$data[] = array('category' => 2, 'name' => 'Brian');
<?php
// Obtain a list of columns
$category =array();
$name =array();
foreach ($data as $key => $row) {
$category[$key] = $row['category'];
$name[$key] = $row['name'];
}
// Sort the data with category descending, name ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($category, SORT_DESC, $name, SORT_ASC, $data);
echo '<pre>'; print_r($category);
?>
Hi thanks to everybody for your help including all the other Stackoverflow questions which I googled to piece this together, not sure how I did it, bit of a case of an infinite number of monkeys or should that be newbies, anways here's what worked for me.
Sorts names ascending within contributions descending
function compare($a, $b) {
if ($a['contribution'] == $b['contribution']) {
return ($a['name'] < $b['name']) ? -1 : 1;
} else {
return ($b['contribution'] - $a['contribution']);
}
}
usort($ads_by_cotribution, 'compare');
It's all magic to me, that's the beauty of it. I didn't include the case of names being equal
'cause there shouldn't be any and as I understand equal values would still stay together just in a different order. I'd like to understand how these functions work, do they continuously walk through the array until there's no change in the order - what we used to call a bubble sort? Does ($b['contribution'] - $a['contribution']) indicate which is the larger? Finley is there a difference between sorting contributions - a numeric field and names - an alpha field?
$name = array("Joe", "Carol", "Bruce", "Michael","Alan","Brian");
sort($name);
php-in built function sort will produce desire result
i m trying to build recursive menu using PHP but not succeeding
mysql table
menuid name parentid
and my php code
function generateMenubar()
{
$data = Yii::app()->db->createCommand("select * from menu");
$result = $data->queryAll();
$html = '<ul class = "navigation">';
foreach($result as $row)
{
if($row['parentid'] == "0")
{
$html .= '<li><span>'.$row["menuname"].'</span>';
$menu_id = $row['menuid'];
$html .= $this->generateHTML($result,$menu_id,$html);
}
}
return $html;
}
function generateHTML($result,$menu_id,$html)
{
foreach($result as $row_sub)
{
if($menu_id == $row_sub['parentid'])
{
$html .= '<ul><li><span>'.$row_sub['menuname'].'</span>';
$menu_id = $row_sub['menuid'];
$html .= $this->generateHTML($result,$menu_id,$html);
$html .= '</li>';
}
}
return $html.'</ui>';
}
but this loop is not stopping and generating wrong output. it can have sub levels upto n level. i want to make it dynamic cuz levels may change in future any suggestion ?
Your problem is that you do not have a structured result to iterate over. You might have SubElement 2 > 3 > 4 as your first result but 2 > 3 > 1 as your 5th Result. So you can't just iterate over the result once and build your html.
What you want to do (appart from, switching to nested sets, which is what you REALLY want to do wink) is structure your result first.
Iterate over your result and build a nested array to iterate over recusively to build your HTML. To find "where to put your element" in your recursive array you need to recursively check back with your existing array always as well. IF all you store is id and parent id, how to find out what the root element is before you checked ALL elements right?
So i could write the code to do so, but i rather do not because it would be a horrible solution anyway.
To do so more efficiently it would really help if you do not only store your parentid but a level as well.
Then you could store your elements in a two dimensional array storing all elements for each level and then recursively use that array. i.e.
$navigationTempArray = array();
foreach($fakeMySQLResult as $row)
{
if(!array_key_exists($row['level'], $navigationTempArray )) {
$navigationTempArray[$row['level']] = array();
}
if(!array_key_exists($row['parentid'], $navigationTempArray[$row['level']] )) {
$navigationTempArray[$row['level']][$row['parentid']] = array();
}
$navigationTempArray[$row['level']][$row['parentid']][] = $row;
}
now you have an array like this:
array (
0 => array(
'root' => array(
1 => array('title' => 'Start' ...)
2 => array('title' => 'Team' ...)
3 => array('title' => 'Projects' ...)
)
),
1 => array(
2 => array(
4 => array('title' => 'Development' ...)
5 => array('title' => 'Design' ...)
6 => array('title' => 'Sales' ...)
),
3 => array(
7 => array('title' => 'Mayhem' ...)
8 => array('title' => 'X' ...)
)
),
2 => array(
4 => array(
9 => array('title' => 'PHP' ...)
10 => array('title' => 'MySQL' ...)
)
)
)
Now you can iterate over this array recursively, solving every level for every item to infinity ;-)
function returnSubNavigation($id,$level,$fullNavigationArray) {
$html = '';
if(array_key_exists($level, $fullNavigationArray) && array_key_exists($id, $fullNavigationArray[$level])) {
$html .= '<ul>';
foreach($fullNavigationArray[$level][$id] as $subElement) {
$html .= '<li><span>'.$subElement["menuname"].'</span>';
$html .= returnSubNavigation($subElement['id'], $level+1, $fullNavigationArray);
$html .= '</li>';
}
$html .= '</ul>';
}
return $html;
}
echo returnSubNavigation('root', 0, $navigationTempArray);
Here is an online fiddle kind of thing that proves it works
Some people who do not want to use nested sets often store pathes rather than parent id's.
i.e.:
| id | path |
| 1 | 1 |
| 2 | 2 |
| 3 | 1.1 |
| 4 | 1.2 |
| 5 | 2.1 |
| 6 | 3 |
This is a lot easier (cheaper in terms of performance) to iterate over. You can sort it a lot easier. Still, it brings a lot of problems and restrictions.
I have an array:
$data = array(
1 => array(
"time" => 1,
"parent" => array(4)
),
2 => array(
"time" => 3,
"parent" => array(4, 5)
),
3 => array(
"time" => 2,
"parent" => array(6)
),
4 => array(
"time" => 1,
"parent" => array(6)
),
5 => array(
"time" => 1,
"parent" => array(4)
),
6 => array(
"time" => 1,
"parent" => array()
)
);
Key is the ID of an element, parent is an array of elements, which refers to element id and time is just an integer.
This is an illustrated example of a given array:
Schema
The integer on the bottom-left is the "id" and the integer in the middle is "time".
My goal here is find the most time-consuming path of this array. In the given example, the path would be 2->5->4->6 (id wise) resulting in 6 "time" overall. It looks pretty easy on paper, however I can't really seem to code an algorythm to get the elements of the most time-consuming path. I would appreciate any kind of help.
I think the algorythm should be kind of bruteforce-ish and check through all of the options available. Thus with the given array it would go like:
1 -> 4 -> 6 = 3
2 -> 4 -> 6 = 5
2 -> 5 -> 4 -> 6 = 6
3 -> 6 = 3
4 -> 6 = 2
5 -> 4 -> 6 = 3
Thanks in advance.
Note that this will only work if there are no loops in the array.
// Note: built this in the SO editor, might have bugs
$cached = [];
$arrays = []; // Do this yourself
function get_path($num) {
global $arrays, $cached;
if (isset($cached[$num])) return $cached[$num];
$array = $arrays[$num];
$maxtime = $array['time'];
$bestpath = array($num);
foreach ($array['parent'] as $i) {
$path = get_path($i);
if ($path['time']+$array['time'] > $maxtime) {
$maxtime = $path['time'] + $array['time'];
$bestpath = array_merge(array($num),$path['path']);
}
}
$cached[$num] = array('path' => $bestpath, 'time' => $maxtime);
return $cached[$num];
}
var_dump(get_path(5));
Not really a bruteforce way, should be close enough to O(n). The basic idea is that you just cache the paths it can take.
Note: I used a bit of C-style syntax here, but ideally you wouldn't actually write the code like this
I have the following values from a database call that I want to apply some logic to. I thought I could originally use PHP's max however this doesn't appear to be the case.
I have three suppliers of a product. They might not all stock the item I am displaying, and they all offer a different margin, on a product by product basis though, so that is why I can't just say generally supplier 1 is better than supplier 2 etc.
$supplier1Live = 1
$supplier2Live = 1
$supplier3Live = 0
$marginSupplier1 = 20
$marginSupplier2 = 40
$martinSupplier3 = 50
In this example I would want to use Supplier 2 as they stock the product supplier2Live = 1 and also have the better margin than the other supplier who stocks the product (supplier1)
My mind however is drawing a complete blank in how to code this?
I thought I could add it to an array giving:
$array = array(
"supplier1" => array(
"live" => 1,
"margin" => 20
),
"supplier2" => array(
"live" => 1,
"margin" => 40
),
"supplier3" => array(
"live" => 0,
"margin" => 50
)
);
And run something on that, but not sure what to.
Filter the array using array_filter (filter by live==1), and then find the maximum out of the resultant array (maximum on the "margin" value)
Like this, if I understand correctly
$array = array(
"supplier1" => array(
"live" => 1,
"margin" => 20
),
"supplier2" => array(
"live" => 1,
"margin" => 40
),
"supplier3" => array(
"live" => 0,
"margin" => 50
)
);
$res = array_filter($array,function($v){return $v["live"];});
$supplier = array_reduce($res, function($a, $b){
return $a["margin"]>$b["margin"]?$a:$b;
});
print_r($supplier);
Try something like this:
$best_supplier = null;
$best_supplier_margin = null;
foreach($array as $name => $supplier) {
if($supplier['live']) {
if($supplier['margin'] > $best_supplier_margin || is_null($best_supplier_margin)) {
$best_supplier = $name;
$best_supplier_margin = $supplier['margin'];
}
}
}
if(is_null($best_supplier)) throw new Exception('No suppliers are live!');
echo $best_supplier;
So you basically want to find the max of supplierXLive * marginSupplierX?
You can also implement a custom compare function and provide it to PHPs usort() function
Hey guys I'm trying to learn codeigniter, but once again I'm STUCK and I seek help (as usual :P )
What I need to do?
-> I need to get the data related to a article from the database along with other stuff like the tags for the article and all the comments. I'm thinking of keeping single level nested comments for the article.
Well I'm done with the tag part [link to the answer which helped me with the same : Returning and using multidimensional array of records from database in CodeIgniter 2.0 ] but the comment part is driving me nuts.
Well to get started here is my comments table
Comments
+---------------+-------------+
| Field | Type |
+---------------+-------------+
| commentId | int(10) |
| PostId | int(10) |
| author | varchar(30) |
| email | varchar(30) |
| url | varchar(50) |
| date | datetime |
| comment | text |
| parent | int(10) |
+---------------+-------------+
I'm using the parent field to keep a track of the parent for a nested child comment. By default the value is 0 which means it the parent. Child comment will have the commentid of its parent comment
public function getPost($postName = NULL , $year = NULL, $month = NULL ){
if($postName != NULL && $year != NULL && $month != NULL){
//single post
$this->load->model('comment_model');
$this->db->where('postName',$postName);
$this->db->where('year(date)',$year);
$this->db->where('month(date)',$month);
$q = $this->db->get('mstack_Post');
if($q->num_rows()>0){
$post = $q->result();
foreach ($post as &$p) {
$p->tags = $this->getAllTags($p->postId);
/* getting the comments */
$com = $this->comment_model->getComments($p->postId);
/*echo count($com).' is the total count'; output= 4 */
foreach ($com as &$c) {
/* trying to filter the comment. but all I get is 1 comment as the output*/
if($c->parent==0){
$p->comments->parentComment = $c;
}elseif($c->commentId==$c->parent){
$p->comments->childComment = $c;
}
}
}
return $post;
}else{
return array();
}
}
}
Any help will surely be appreciated.
If you have any other technique /idea to display multi level comments then do let me know. :)
Here is the solution that might be helpfull:
First you need 2 helper recursive function:
// Building comments.
function buildComments($list, $parent = 0)
{
// Creating result array.
$result = array();
//looping...
foreach ($list as $item)
{
//iteration starts with 0 as default.
if ($item->parent == $parent)
{
// add to the result
$result[$item->commentId] = array(
'author' => $item->author,
// ... other definitions
'child' => buildComments($list, $item->commentId) //execute this function for child.
);
}
}
return $result;
}
function printComments($arg, $depth = 1)
{
foreach ($arg as $item)
{
// Printing comment...
echo str_repeat(' ', $depth) . $item['author'] . "<br />\r\n";
// extra echoes...
// if it has a child comment...
if (count($item['child'] > 0))
{
printComments($item['child'], $depth + 1);
}
}
}
A little explaining:
The buildComments() function will starts with rows that parents has 0. Then it will execute itself for child. if child as a child, it will add it. In the end, result will be like this:
$result = array(
1 => array(
'author' => 'John',
'child' => array(
8 => array(
'author' => 'Jane',
'child' => array(
3 => array(
'author' => 'Jamie',
'child => array()
)
)
),
6 => array(
'author' => 'Jackie',
'child => array()
),
9 => array(
'author' => 'Harry',
'child => array()
)
)
),
4 => array(
'author' => 'Jack',
'child' => array()
),
10 => array(
'author' => 'Clark',
'child' => array(
11 => array(
'author => 'Lois',
'child' => array()
)
)
),
12 => array(
'author' => 'Luthor',
'child' => array()
)
);
In the printComments() function we are printing results recursive. for each child, function repeats itself. You will get result like this:
John
Jane
Jamie
Jackie
Harry
Jack
Clark
Lois
Luthor
For more information about recursive functions Look this answer
USAGE
$this->db->where('postName',$postName);
$this->db->where('year(date)',$year);
$this->db->where('month(date)',$month);
$this->db->order_by('parent', 'asc');
$query = $this->db->get('comments');
$comments = buildComments($query->result());
printComments($comments);
that'is that simple...