I am trying to display latest articles using views module. There are 12 article should be displayed per click of pagination excluding the duplicate entry. So, I tried as:
checked Advanced > Query settings > Distinct but no luck.
I have also tried as Views Distinct Settings > Filter/Aggregate this field > Filter Repeat, it works but number of articles are displayed less than 12.
Then, I tried to do it using the hook mymodule_views_query_alter but I do not understand how can I accomplish this task. I tried as:
if($view->current_display == "latest_article"){
$query->add_groupby('node.title');
}
But no solution. How can I override the query and inject DISTINCT function to get unique record.
Did you tried using Use aggregation option from Advanced section ? Hope this helps.
Related
I am learning PHP and have decided to code my own OOP MVC framework. Now, I have realized several times already that it might not be the smartest move but I mean to see this out to the end. And then onwards...
My issue is creating a listing sidebar based on categories and a second based on year-month-postname.
I am officially stuck on the first one, let alone the second damn option. I have included some code and description of what I have tried. The lack of OOP info on the net is daunting or maybe it is because I am searching for the wrong thing, I dont know. But the tutorials have not given me any insight as to how to actually do this in a way where my database is in a model file and my class logic is in the class file.
Sorting logic should be like this Array-Object-Propertyname-Value.
The value, as I hope is easy to understand in my example below, is the category name eg Javascript, PHP, HTML. By that category i wish to sort my blog posts. But not in the way that requires me to manually input the category names to the code. I want to allow users to enter categories if they so choose.
I also wish to display the blog posts inside said category, lets say 5 most recent. But that should not be too hard with a
for($i=0,$i<5,i++)
nested inside whatever solution in the end will work for the category sort.
I have tried MySQLI procedural solutions ranging from multiple google searches and tutorials. Can do it, but dont want to do it procedurally. Tried foreach loop and nesting multiple foreach loops - simply cannot get either the problem of having duplicates based on the shared category name or if trying to group in the SQL query, it simply groups results with same category and then displays only the first one in the group. While loops with mysqli procedural work but with pdo in my case they produce infinite loops, no matter the condition I try to set.
So foreach is the way to go I believe. I have read up on loops and array sorting but I've yet to find a solution. I thought of sorting by key because that is what i need but to no applicable solutions.
It's easy to display the category names and dates and all that. But with category I always get duplicates.
Ive tried some logic where as to assign category names as variables but only to have them all be different variables, meaning still having duplicates or only rewriting the variable with each iteration.
Also, array sorts havent worked because I havent gotten any to work with sorting either on property or if converting Objects to a multidimensional array. Granted that may be because I am a beginner and not understood the syntax but I am not going to post them all here I think.
If you think an array sorting function will do the trick then perhaps give me an example and I will look into it with some new perspective hopefully.
PDO query :
'SELECT * FROM postTable
INNER JOIN userTable ON postTable.postUserId = userTable.id
INNER JOIN postCategories ON postTable.postCategoryId = postCategories.categoryName
ORDER BY postTable.postDate DESC'
Tried also to add
GROUP BY categoryName
but that resulted in only one entry per category shown when using var_dump. Sidenote - same is when grouping by creation date. Is there another layer added to the array when using group in the SQL command and I missed that in the docs?
PDO returns to view file :
$this->stmt->fetchAll(PDO::FETCH_OBJ);
this all gets passed into an array of
$results
and then that is sent to the php on the view page where the resulting array has this structure with var_dump.
array() {
[0]=>
object(stdClass)# () {
["categoryName"]=>
string() "Help"
}
[1]=>
object(stdClass)# () {
["categoryName"]=>
string() "Me"
}
and so on.
Note - also tried using -
fetchAll(PDO::FETCH_ASSOC);
But ive had similar failures with attempting any sorting or limiting to just one category name displayed but all entries under said category being displayed correctly and not just one per category.
I will be checking back when i finish work tomorrow so in about 20-22 hours from the time of posting.
If you need any more info just let me know and I'll post it.
You can order by multiple columns. Use:
ORDER BY categoryName, postDate DESC
This will keep all the posts in the same category together, and in decreasing date order within each category.
See How can i list has same id data with while loop in PHP? for how you can output the results, showing a heading for each category.
If you just want to get the 5 latest posts in each category, see Using LIMIT within GROUP BY to get N results per group?
I'm using MySQL and Php.
Here's the thing : I'm building a forum, with topics and posts.
Posts are displayed in a topic in different pages (let's say 10 posts/page).
My question is : if I ask to see a specific post in a topic, how can I know the page where the post is displayed ? (With the purpose to display the page required)
Should I calculate the number of pages then check each group of items of all possible pages or something like that ?
Thx for your help !
You could use Google Custom Search and let Google do all the hard work. I hear they're quite good for searching websites.
If you want something a little fancier you can pay for Google Site Search.
if your site is small and you want to try a really really simple solution why don't you explore MySQL Fulltext search?
That said the field, fields to search must be a Fulltext index. So you can grab a pair of text fields and some varchar fields and jointhem all in a Fulltex Index.
SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('database' WITH QUERY EXPANSION);
But go and have a better look at MySQL manual!
Ok, I was eating and then... THE IDEA.
I guess a simple way to doing this is to count the number of items before the needed one (with ID or, in a better way, with Timestamps cause posts are ordered with those), and then calculate how many pages it takes.
Meaning, for example, if pages display 10 items, we have 24 items before our targeted one : ceil(24/10) = 3 item will be in page 3 !
Very simple when we think of it.
#Get the page displaying the selected item
#Step 1 : calculate how many items are before the selected one
$nbr_of_items = SELECT COUNT(post_id) FROM posts WHERE post_topic_id = :topic_id AND post_timestamp < :timestamp_of_selected_item
#Step 2 : do simple maths
$calcul = ceil($nbr_of_items / $nbr_of_items_displayed_in_a_page);
For example, we have 24 items before the selected one and pages display 10 items :
$calcul = ceil(24 / 10) = 3
Of course it could be updated (what if an other post has the same timestamp, etc) but it's a good start.
I use the Tx_news Extension (not TT_news) and have 2 pages where i show my newsitems. On Home I show only some topnews with the Listview, and there is a second page with the full News overview, the last one is ordered by Date & Time field.
But for the items on the Home I want a Manual sorting if it's possible.
I have added the following code in my resources:
$GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['orderByNews'] .= ",sorting";
This gives in the plugin the option for Manual Sorting, so that part works. Only How and Where can I order the News Items?
Note: I see in the Newsrecord table (tx_news_domain_model_news) the column sorting, my records are all 0 in there, I think its something with this, but can't get my finger on it.
I hope somebody can help me.
You need to set the news-plugin settings in ts-setup.
plugin.tx_news {
settings {
orderBy = sorting
orderDirection = desc
orderByAllowed = sorting,author,uid,title,teaser,author,tstamp,crdate,datetime,categories.title
}
}
I have a concrete5 site already using an autonav block in the global header for the main site navigation.
I now need to add a secondary navigation to the header containing different, less important links.
These links aren't necessarily to be shown in sitemap order, but I would prefer to use an auto-nav block if possible to do so.
Can anyone give me any tips on how to achieve this?
Can I get there somehow using Aliases on the sitemap?
Are you looking at using Bread Crumbs? We also use Mega Menu.. There are quite a few menuing systems from concrete5.org. Note: Mega Menu is not free, but is worth it.. The other thing is that sometimes the "File Tree" and "File Sets" may have to be re-ordered.. While in the file Tree, choose the option to update cache..
You should use a page attribute. You could use a checkbox for instance, call it secondary and if checked make your secondary nav to show it.
You can use a page list and an boolean attribute(for example: display_in_second_nav)
Here is an example:
$pl = new PageList();
$pl->filterByAttribute('display_in_second_nav');
$pages = $pl->get();
Also, you can sort results:
$pl->sortByRelevance()
Orders by index score descending. Only available when searching by keywords.
$pl->sortByDisplayOrder()
Orders by sitemap display order ascending.
$pl->sortByDisplayOrderDescending()
Orders by sitemap display order descending.
$pl->sortByPublicDate()
Orders by public date ascending.
$pl->sortByPublicDateDescending()
Orders by public date descending.
$pl->sortByName()
Orders by page name ascending.
$pl->sortByNameDescending()
Orders by name descending.
I have to create a browse page like monter.com or dmoz directory. My problem is the counts for each category.
What's the best practive to do a similar thing?
I am using PHP/MySQL
Thx!
Did you try to implement it the way that you calculate these numbers dynamically when the page loads? How do you know that it is indeed a performance issue? Calculations are pretty fast these days.
Otherwise put a counter field next to each (sub-)category. When inserting(/deleting) a new post, then you update the counter of all categories it belongs to.
As a result, when you query any category, or set of categories, you will have a counter field for each giving the exact numbers of posts the category holds.
Solr can do that with Faceted Search http://lucene.apache.org/solr/tutorial.html#Faceted+Search
I'll test it now.