Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Looking for a bit of guidance. I have say 50 news articles on a site, I also have a filter to allow the user to refine their search e.g. filter by category, by topic and by date.
Just now, I have a list of dates for the past 12 months (current month - 11 months) which is fine but if no news was published last month, January 2019 would still show on the dropdown menu.
What I want to do is, search all the articles, get their published date then create a dropdown list from that. So, if no news is posted in January, but there was in Dec and Feb, the dropdown list would have Decemeber 2018 and February 2019 only.
I'm thinking I need to create an array which will build up the drop down, maybe with a filter that says if the article is older than 12 months don't add - otherwise the list could get massive and I'm guessing there would be a performance hit if there are years of articles to wade through.
Any thoughts, pointers etc would be great.
I can see three potential paths to ruminate:
If the problem is framed from the perspective of search, what you describe may go away if a message displaying 'no results found' might be the easiest road to go. This logic comes from a developer mindest.
Think in terms of broad to narrow. A user would select a year first. Then you would populate the months with the values for months which there are actually articles. I suppose the same logic could be inverted and based on the month selected you can populate all the years for which articles of the month exist. This logic comes from a SQL mindset.
Another option would be to filter by a set date range(past month, past 3 months,Eternity,etc). This completely eliminates the need for specific dates and allows you to leverage date arithmetic. This logic comes from a Google(seeing what they appear to do) mindset.
Hope it makes sense...
Sorry, not sure if I explained myself very well. The drop down looks a bit like this:
What I want to do is, if there are no articles in February, then the dropdown will look like this:
But you do raise a new issue that I hadn't thought about, should I be filtering on all 3 dropdown menus all the time e.g.
When viewing all articles, then no value is set in any of the dropdown menus and all dates are shown. If I then select a category, should I then check to see what dates the category articles have and reflect that in the date dropdown.
Hmmm, this is growing arms and legs all of sudden. :)
would it not be a case of something like this?
var dates = allArticle.Where(x => x.IsVisible).Select(x => x.PublishedDate).Where(x => x > DateTime.Today.AddMonths(-12))
might need some castings but think thats the general idea
Would that not be executed on ever page load though? Just thinking from a performance point of view.
Yeah you would probably want to cache the partial or run the same sort of search / filtering through examine, if performance is an issue.
For optimal performance I would revert to either examine or a xpath query and use a xpath node navigator.
Use the xpath navigator to get you a list of dates. And use LINQ to get the distinct years and months from that list.
In all cases I would avoid Descendants api
If you need help with that. Just let me know. I'll try to to put together a code example.
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted