Skip to main content.

News Category Dropdown

16/10-09 at 13.15 by: Robert Campbell

People have asked numerous times:  How do I create a pulldown of categories from the News (or some other module)... so that the user can select a category from a pulldown, and have the page automatically filter the results.

Well, I knew it would be relatively simple with some smarty magic, and a custom built (but simple) form.  So I set out to do it.... it took me all of 10 minutes.

For this demo we'll be using the News module, though the principle still applies for things like the products module, CGBlogs, or even CompanyDirectory, or if you wanted to allow the user to change the sorting.   But we'll leave that as an exercise for you to do on your own.

The first step is to create a new BrowseCategory template in the news module.  I called mine 'dropdown' and used this text:

{if $count > 0}
<form method="get">
<select name="news_category">
<option value="-1">All</option>
{foreach from=$cats item=node}
{if $node.count > 0}
<option value="{$node.news_category_id}"  {if $news_category == $node.news_category_id}selected="selected"{/if}>{$node.news_category_name}</option>
{/if}
{/foreach}
</select>
<input type="submit" name="submit" value="Submit"/>
</form>{/if}

As you can see here,  I'm creating a simple form with a dropdown and a submit button.  The dropdown has one hardcoded entry with a value of -1 and a label of "All"... the rest of the entries in the dropdown are populated dynamically from the News categories.... and only the categories with entries are added to the list.  The form uses the GET method (which means all of the parameters will be returned on the URL) but you could have just as easily used POST.  The GET method has the advantage that people will be able to bookmark the result page properly.   That's the first part of the solution.

The second part of the solution is to create a new page (or edit an existing page) and do some smarty magic on some built in smarty variables to determine what dropdown option was selected, and then call News appropriately.   Here's the code I have in my page:

{assign var='news_category'  value='-1'}
{if isset($smarty.get.news_category)}
{assign var='news_category' value=$smarty.get.news_category}
{/if}

{news action=browsecat}

<h3>Debug: News category = {$news_category}</h3>

<fieldset>
<legend>Articles:</legend>
{if $news_category != -1}
{news category_id=$news_category}
{else}
{news}
{/if}
</fieldset>

Lets start from the top.   First I use the smarty "assign" plugin to create a smarty variable called news_category with a value of -1.   Secondly, I test to see if the smarty variable $smarty.get.news_category is set, and if it is, I re-adjust the news_category smarty variable to its value.   If you look up, I gave the select box in the News browse_category template the name of news_category.

Next, I simply call {news action=browsecat}, and because I marked the dropdown browse category template as the 'default' browse category template, this will display my form.  If I didn't want to set my dropdown template as the default, I could have called news like: {news action=browsecat browsecattemplate=dropdown}.

Finally, I need to display the news articles that match the category id stored in the "news_category" smarty variable.... but if "All" is selected in the dropdown, I want to display the news articles from all categories.    I put that code inside of a fieldset with a legend so it would be easier to see.

And that's it.   Three simple steps that took me 10 minutes to implement, with no PHP coding what-so-ever.   This is the power of CMSMS and Smarty.  Once you get your head wrapped around this, the power you have in your fingers is unlimited, without really having to know how to program.

Extensions to this would be to modify the form, and page code to allow for a sort order, or maybe a page limit... and a further extension would be to get rid of the submit button (optionally) and just use some jquery triggers.  This would be trivial to do.

I hope you have fun with this.

15/7-10 at 18.53 by: kjwpijzp
dtbbxivu http://vzlnfjsg.com fdqnaokx iignotct [URL=http://nglrtvts.com]nvguwbmm[/URL] <a href=" http://qcbtjvoz.com">vvqrongd</a>
05/7-10 at 02.38 by: Cheap Soccer Jerseys
[url=http://www.jerseyofficial.com/]Cheap Soccer Jerseys[/url] [url=http://www.jerseyofficial.com/]Cheap NFL Jerseys[/url] [url=http://www.jerseyofficial.com/]Cheap MLB Jerseys[/url] [url=http://www.jerseyofficial.com/]Cheap NHL Jerseys[/url]
27/4-10 at 15.31 by: Phil
Thanks CalGuy - a more elegant version of what I had written myself. One problem: with pagelimit links at the bottom (the page displays fine) the category is lost. As far as I can tell, this is because the & symbol in the URL is being converted to "&m8d765" or something similar (it changes every time). Any ideas why?
01/4-10 at 20.57 by: omega watches sale
100402SLCHF Jovons saw the kettle boil and omega classic for sale cried out with the delighted voice of discount omega casino royale a child; Marshal too had seen the omega watches sale kettle boil and sat down silently to build an engine. Man errs so long as he rolex watches sale strives. Johann Wolfgang Goethe, German poet and dramatist Man errs so long as he rolex air-king sale strives. Johann Wolfgang Goethe, German poet and dramatist Miracles sometimes occur, but one has to work terribly for rolex daytona sale them. http://www.belovewrist.com/
01/4-10 at 20.57 by: omega watches sale
100402SLCHF Jovons saw the kettle boil and omega classic for sale cried out with the delighted voice of discount omega casino royale a child; Marshal too had seen the omega watches sale kettle boil and sat down silently to build an engine. Man errs so long as he rolex watches sale strives. Johann Wolfgang Goethe, German poet and dramatist Man errs so long as he rolex air-king sale strives. Johann Wolfgang Goethe, German poet and dramatist Miracles sometimes occur, but one has to work terribly for rolex daytona sale them. http://www.belovewrist.com/
11/3-10 at 10.09 by: aascsa
ascascasc
09/3-10 at 10.30 by: Nick
This is fantastic. I would really appreciate some help in customising this for the Product module so i can have a 'sort by' dropdown menu so that the products can be sorted by price? category?
16/10-09 at 13.27 by: Compufairy
Fabulous! And thank you! Regards, Anne-Mieke
Written by:
Comment:
Write the chars you see in the square
This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)
 
 

CMS Made Simple - Modules/Add-Ons

Re: Custom JQuery Image Rotation?

- Thu, 29 Jul 2010 21:23:39 GMT

I ment the CMSms Gallery module... of course  Cool

but you asked:

Is there another module you'd prefer that makes it easier for the end user?

Re: Announcement: New plug-in SuperSizer

- Thu, 29 Jul 2010 20:40:15 GMT

ok.. here is it.. all fixed and should be ready now to release..

@NikNak please test but here is my test on your images..
http://www.corbensproducts.com/blog/experimental/supersizer.html#NikNakcrop4



Please let me know.. cheers -Jeremy

PS.. I'm headed...

Re: How do I get the latest version of a file

- Thu, 29 Jul 2010 20:28:47 GMT

Hi
Many thanks for your prompt reply, its a great help to a newbe like me. I will try and sort it from hear.

Re: Custom JQuery Image Rotation?

- Thu, 29 Jul 2010 20:21:31 GMT

wait.... i AM using the gallery module...

is there something i'm missing here?

or is gallery a .js you like to use?

Re: CGBlog - A several of things

- Thu, 29 Jul 2010 20:17:22 GMT

never mind fixed it.  there was some google analytics that were not liberalized.  My mistake.

CMS Made Simple Blog

Announcing CMS Made Simple 1.8.1 - Mankara

- Tue, 13 Jul 2010 14:06:34 -0400

This release fixes an important security vulnerability, we recommend that ALL users upgrade as soon as possible. The local inclusion vulnerability fixed is old and affects many previous versions of CMSMS. Therefore it is important for ALL...

Announcing CMS Made Simple 1.8 - Madagascar

- Sat, 03 Jul 2010 20:26:55 -0400

Onwards and upwards we go. The dev team is proud to announce the latest version of your favorite content management system. This version is primarily aimed at rounding out some of the rough edges that our primary audience...

New Site Launch

- Mon, 28 Jun 2010 00:29:44 -0400

After weeks of blood, sweat and tears, we've finally launched our long awaited redesign. It's taken approximately two months from design to implementation to launch, but we're finally here. Along with our main site, we've also reskinned the...

Announcing Geek Moot 2010

- Mon, 21 Jun 2010 10:19:21 -0400

It's that time again. The developers of CMS Made Simple have finalized plans for our yearly user conference. This year, we're thinking much bigger, so join us. On 16-17 September, CMS Made Simple will host the second public Geek...

Announcing CMS Made Simple 1.7.1 - Escade

- Sat, 01 May 2010 09:47:13 -0400

Woot! CMSMS keeps getting better and better. Even though it's only a point release, this version of CMSMS fixes numerous minor bugs and adds some important features. We didn't feel that the list of changes were extensive enough to...