I have a news website and i want to create popular post based on user visit with interval between 7 Days.
I created counting mechanism using Redis::incrby() with prefix :
Redis::incrby( 'news:popular:count:' . $news_id . ':' . \Carbon\Carbon::now()->format('d-m-Y'), 1 );
but the problem is, i don't know how to retrieve it using GET and sort the value.
So i expect to get the popular post based on user count.
Is there any way to achieve it and get the popular post? or is there any better method to do this?
I've found my own solution and i will publish it here so if another user facing similar issue like me, you can use this solution.
First, we retrieve the key using Redis::keys using wildcard to get a list of a keys that contain the post count :
$popular = Redis::keys('news:popular:count:*');
and then we create empty array to store the count data :
$get_count = [];
after that we use foreach to explode and get the news_id and then get the post count
foreach ($popular as $key => $res) {
// Get news_id
$news_id = explode( ":", $res );
// Get count each post
$get_count[ $news_id[3] ] = Redis::get( $res );
}
and then we sort the array result from high to low using arsort() method
// Sort news count from high to low
arsort( $get_count );
That's it, the hardest part that i'm facing already solved. Maybe you can share a better answer if you have.
Related
I'm sure this may be a simple solution, but I can't seem to work it out.
I am trying to use Laravel's where() clause to build an array of $courses that belong to each $student. I cycle through each $student and filter the $courseRecords to find matching courses based on their StudentCode.
Here is my sample code snippet:
// Cycle through the students and add their relevant course details
foreach( $students as $student ) {
// Find matching courses to the student
$courses = $courseRecords->where( 'StudentId', $student->StudentCode );
// Add the course array to the student record
$student->Courses = $courses;
}
However, the result I get, gives me each student's course, but with a leading index number (as shown below in random results):
I can't seem to work out why this is happening. The first entry (Id 0) is the result I am expecting, but for some reason, every other result seems to give me the matching index number of $courseRecord.
I have tried using $courses->all(); and $courses->toArray(); but this doesn't make any difference. From the Laravel documentation (that I have read), it doesn't mention this behaviour which makes me think I have something incorrect.
$students and $courseRecords are both a collection.
Use values():
$courses = $courseRecords->where('StudentId', $student->StudentCode)->values();
I have a table called post that HAS MANY comments I need to sort the list of post by how many comments there are in a post.
In fuelPHP, is there a way to do this in ORM? I want to make sure before I do it manually.
Thanks.
Short answer for this: No, there's not. It is not possible for the ORM to sort for you like that without doing it manually.
To work around it:
// construct an array with post-id => comment-count
$ordered = array();
foreach($posts as $id => $post)
{
$ordered[$id] = count($post->comments);
}
// order the array, largest count first
arsort($ordered, SORT_NUMERIC);
// merge the posts in, maintaining order
$ordered = array_replace($ordered, $posts);
This uses takes advantage of the feature that array_replace will maintain the key order of the first array.
First of all, I found similar questions in SO but there is not any answer for them. So, the first part of the question is a little bit duplicated. I want to improve search results in Magento. Here is what I've done already:
1. Search with AND instead of OR when there are multiple words.
2. Ajax search starts searching from anywhere and not only from the beginning of the fields.
3. Trim the last s from the words to prevent empty results when searching with plurals.
4. I changed the search type from Like to Fulltext or Combine but the results were not better and even were worst, so I leave it as is. It's Like now, so there is no relevance ordering.
The last thing which I want to try is adding this to the search query:
SELECT ... other non-full-text-cols
MATCH (product_title) AGAINST ('lean body for her') AS rel1,
MATCH (content) AGAINST ('lean body for her') AS rel2
FROM table
WHERE MATCH (product_title,content) AGAINST ('lean body for her')
ORDER BY (rel1*1.5)+(rel2)
Here is my query but I'm not sure if it would work because I can't test it:
$this->_productCollection->addAttributeToSelect(
array(
'rel1' => new Zend_Db_Expr('MATCH (name) AGAINST ("'.$queryText.'")'),
'rel2' => new Zend_Db_Expr('MATCH (short_description) AGAINST ("'.$queryText.'")')
)
);
$this->_productCollection->getSelect()
->where('MATCH (name,short_description) AGAINST ("'.$queryText.'")')
->order('(rel1*1.5)+(rel2)');
The main idea is to add bonus weight to a result if the search query is found in the title of the product. The problem is that I don't know where to modify the query. I can't find where it is at all. $this->_productCollection is not the right object, I know it. I looked at all the Collection.php files, resource models, models and even the query log but no luck. There are just little 1 or 2 row parts in some files but not a full query. I'm new to Magento and still have problems with finding this type of stuff. So, where I have to place my additional stuff when I have to extend a query?
Community Edition Magento, version 1.6.1.0.
Note: I know that some extension for improving search results will work much better than my solutions but for now I have to do it in that way. It would be a good experience for me, too.
Edit:
So, I figured out how to add my custom fields for the ordering but it's
untruly I think. In class Mage_CatalogSearch_Model_Layer extends Mage_Catalog_Model_Layer's prepareProductCollection method I added two joins to the query and get the fields rel1 and rel2:
$collection->getSelect()->joinLeft(
array('cpev' => 'catalog_product_entity_varchar'),
'cpev.entity_id = e.entity_id AND cpev.attribute_id = 96',
array('rel1' => new Zend_Db_Expr('2.01*(LENGTH(cpev.value) - LENGTH(REPLACE(LCASE(cpev.value), LCASE("'.$queryText.'"), ""))) / LENGTH("'.$queryText.'")'))
);
$collection->getSelect()->joinLeft(
array('cpet' => 'catalog_product_entity_text'),
'cpet.entity_id = e.entity_id AND cpet.attribute_id = 506',
array('rel2' => new Zend_Db_Expr('(LENGTH(cpet.value) - LENGTH(REPLACE(LCASE(cpet.value), LCASE("'.$queryText.'"), ""))) / LENGTH("'.$queryText.'")'))
);
I have these fields now but as you can see I have hard coded stuff like attribute_id = 96 etc. which is not good at all and it will not work everytime - I checked these ids directly from the database tables. I wrote it like this because I haven't access to name and short_description fields but they are in the result. Don't know why. So, cpev.value is name field and cpet.value is the short_description field. Moreover I can't order the results by these fields. I tried $collection->addOrder('SUM(rel1+rel2)');, $collection->getSelect()->order(new Zend_Db_Expr('SUM(rel1+rel2)').' DESC');, some addAttributeToFilter stuff etc. but it's not working.
Edit 2:
I accepted #james' answer but finally we bought an extension for improving the search results.
In Mage_CatalogSearch_Model_Resource_Fulltext check out prepareResult (line 310 in 1.7 CE) and look for the following:
$select->columns(array('relevance' => new Zend_Db_Expr(0)));
Magento sets all search result relevances as 0 (!); add the relevances you want (higher is better) here. You can create a custom query in Zend_Db_Expr() to generate higher relevances on attribute matches.
The answer to your first question (1):
To make an AND search instead of OR, you will need to rewrite the class
Mage_CatalogSearch_Model_Resource_Fulltext
In the method
public function prepareResult($object, $queryText, $query)
you want to switch the part
$likeCond = '(' . join(' OR ', $like) . ')';
to
$likeCond = '(' . join(' AND ', $like) . ')';
Be sure to reindex the search index afterwards to have an effect.
I am using a CDbCriteria with its own conditions, with & order clauses. However, the order i want to give to the elements in the array is way too complex to specify in the order clause.
The solution i have in mind consists of obtaining the active records with the defined criteria like this
$theModelsINeed = MyModel::model()->findAll($criteria);
and then rearrange the order from my php code. How can i do this? I mean, i know how to iterate through its elements, but i donĀ“t know if it is possible to actually change them.
I have been looking into this link about populating active records, but it seems quite complicated and maybe someone could have some better advice.
Thanks
There is nothing special about Yii's active records. The find family of methods will return an array of objects, and you can sort this array like any other array in PHP.
If you have complex sort criteria, this means that probably the best tool for this is usort. Since you will be dealing with objects, your user-defined comparison functions will look something like this:
function compare($x, $y)
{
// First sort criterion: $obj->Name
if ($x->Name != $y->Name) {
return $x->Name < $y->Name ? -1 : 1; // this is an ascending sort
}
// Second sort criterion: $obj->Age
if ($x->Age != $y->Age) {
return $x->Age < $y->Age ? 1 : -1; // this is a descending sort
}
// Add more criteria here
return 0; // if we get this far, the items are equal
}
If you do want to get an array as a result, you can use this method for fetching data that supports dbCriteria:
$model = MyModel::model()->myScope();
$model->dbCriteria->condition .= " AND date BETWEEN :d1 AND :d2";
$model->dbCriteria->order = 'field1 ASC, field2 DESC';
$model->dbCriteria->params = array(':d1'=>$d1, ':d2'=>$d2);
$theModelsINeed = $model->getCommandBuilder()
->createFindCommand($model->tableSchema, $model->dbCriteria)
->queryAll();
The above example shows using a defined scope and modifying the condition with named parameters.
If you don't need Active Record, you could also look into Query Builder, but the above method has worked pretty well for me when I want to use AR but need an array for my result.
How do URL shortener's like bit.ly calculate a random key for each link? What algorithm would I need to know to create my own?
So far I found the code from http://briancray.com/2009/08/26/free-php-url-shortener-script/
function getShortenedURLFromID ($integer, $base = ALLOWED_CHARS)
{
$length = strlen($base);
while($integer > $length - 1)
{
$out = $base[fmod($integer, $length)] . $out;
$integer = floor( $integer / $length );
}
return $base[$integer] . $out;
}
and the more complex answer by Marcel J. mentioned above.
I think they DON'T random a new key and checks if exists in database, because it its slower than just use a sequencial number and apply some criptography algoritm to convert sequencial id to a UNIQUE string.
Ex:
idUrl = 1003;
urlCode = doSomething(idUrl); // 161Llz
URL to use:
http://bit.ly/161Llz
Tks: mykhal and nick johnson
Maybe they store it in the database and just give you an link id. When you query this key they look in their database and forward you to the stored real link. To encode the id something like base64 (or similar) might be used.
They most likely store it in a database and just generate the key randomly. I assume this because you can make your own key, and if they just decoded it you wouldn't be able to choose it yourself.
As for how to do it, you could just create a database in mySQL and have it hold the key and full site. Just search it for the key and then redirect the user to the full site.