Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-05-26 14:41:48

element
Member
Registered: 2009-11-18
Posts: 99

sorting articles with pull-down

Obvious question: How do I do it?

I know how to sort the articles but I want them to be able to sort via a pull-down.
Lets say I have a list of articles on a page sorted by title by default. How do I sort them according to a custum field for example using a pull-down on that page?

Offline

#2 2010-05-26 15:02:39

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: sorting articles with pull-down

element wrote:

Obvious question: How do I do it?

Let’s give a basic example. As you are probably aware, you can feed the sort attribute with a value that contains tags or PHP code, or you can use variety of conditionals. Thus, you can for example grap the get value submited by the select field or a link.

In a basic example you could do something like:

<txp:article sort='<txp:php> echo gps("sort"); </txp:php>' />

But, remember to validate the input made by the user. If you don’t check what they sent to the sort, they could use any SQL code inside the sort that is left unescaped by TXP, including function() calls.

So, we have to check the input. Small example:

<!--
	Gets GET parameter named "sort" and places it inside variable named sort:
-->
<txp:variable name="sort" value='<txp:php> echo gps("sort"); </txp:php>' />
<!--
	Now we match the value using variable conditionals:
-->
<txp:if_variable name="sort" value="custom_1">
	<txp:article sort="custom_1 asc" />
</txp:if_variable>
<txp:if_variable name="sort" value="id">
	<txp:article sort="ID desc" />
</txp:if_variable>
<!--	
	Default when the variable is empty
-->
<txp:if_variable name="sort" value="">
	<txp:article sort="Title asc" />
</txp:if_variable>

Ofcourse the select field, links or whatever are just normal (X)HTML:

[...]<a href="?sort=custom_1">Sort by custom</a>[...]
[...]<form action="<txp:site_url />my/page/url/path" method="get">
	<select name="sort">
		<option value="">Default</option>
		<option value="custom_1">Custom</option>
		<option value="id">ID</option>
[...]

Last edited by Gocom (2010-05-26 15:07:37)

Offline

#3 2010-05-26 16:23:55

element
Member
Registered: 2009-11-18
Posts: 99

Re: sorting articles with pull-down

I’m a txp newbie and not a coder. So I must be doing something wrong because it’s not working.
?sort=custom_1 gets added to the url but nothing happens. The order stays the same.

Offline

#4 2010-05-26 23:27:22

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: sorting articles with pull-down

element wrote:

So I must be doing something wrong because it’s not working.

Post a full tag trace from the problem page.

Offline

#5 2010-05-27 09:41:41

element
Member
Registered: 2009-11-18
Posts: 99

Re: sorting articles with pull-down

First I have a question. What does

echo gps("sort");

do?

Offline

#6 2010-05-27 13:27:39

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: sorting articles with pull-down

echo gps("sort"); outputs the value in the url for ?sort=thisvalue, so if your url is www.mydomain.com/mysection?sort=custom_1 the first line creates a txp variable with the value “custom_1” (gps is a function within textpattern). The rest of the txp:if_variable tags check if this value matches one of the allowed sorting criteria and outputs the txp:article tag with the appropriate sort order.


TXP Builders – finely-crafted code, design and txp

Offline

#7 2010-05-28 00:54:49

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: sorting articles with pull-down

Please, post the full tag trace. In that cut out you’re not defining the variable.

Offline

#8 2010-05-28 08:26:57

element
Member
Registered: 2009-11-18
Posts: 99

Re: sorting articles with pull-down

Was trying to be smart, but missed the piece above it. :-)

<!-- txp tag trace: 
[SQL (0,000117063522339): select name, data from txp_lang where lang='nl-nl' AND ( event='public' OR event='common')]
[SQL (0,000506162643433): select name, code, version from txp_plugin where status = 1 AND type IN (0,1) order by load_order]
[SQL (6,00814819336E-5): 
    SELECT
      `name` AS custom_set,
      `val` AS name,
      `position`,
      `html` AS type
    FROM
      `txp_prefs`
    WHERE
      `event`='custom'
    ORDER BY
      `position`
  ]
[SQL (3,60012054443E-5): 
      SELECT
        `name`, `val`
      FROM
        `txp_prefs`
      WHERE
        `html` = 'date-picker'
      AND
        `name` LIKE 'custom_%'
      ORDER BY
        `name`
    ]
[SQL (3,50475311279E-5): 
      SELECT
        `name`, `val`
      FROM
        `txp_prefs`
      WHERE
        `html` = 'time-picker'
      AND
        `name` LIKE 'custom_%'
      ORDER BY
        `name`
    ]
[SQL (3,79085540771E-5): select name from txp_section where `name` like 'stock' limit 1]
[SQL (3,38554382324E-5): select page, css from txp_section where name = 'stock' limit 1]
[SQL (0,000355005264282): select host from txp_log where ip='xxxx' limit 1]
[SQL (0,000197887420654): insert into txp_log set `time`=now(),page='/stock?sort=custom_4',ip='xxxx',host='xxxx',refer='',status='200',method='GET']
[SQL (3,69548797607E-5): select user_html from txp_page where name='stock']
[Pagina: stock]
<txp:output_form form="ab_meta" />
	[SQL (3,91006469727E-5): select Form from txp_form where name='ab_meta']
	[Module: ab_meta]
	<txp:page_title separator=" / "/>
		[SQL (3,38554382324E-5): select title from txp_section where name='stock']
	<txp:site_url />
<txp:output_form form="vars-sort" />
	[SQL (3,60012054443E-5): select Form from txp_form where name='vars-sort']
	[Module: vars-sort]
	<txp:php>
	</txp:php>
	<txp:if_variable name="sort" value="title">
		[<txp:if_variable name="sort" value="title">: false]
	</txp:if_variable>
	<txp:if_variable name="sort" value="custom_1">
		[<txp:if_variable name="sort" value="custom_1">: false]
	</txp:if_variable>
	<txp:if_variable name="sort" value="custom_4">
		[<txp:if_variable name="sort" value="custom_4">: false]
	</txp:if_variable>
	<txp:if_variable name="sort" value="">
		[<txp:if_variable name="sort" value="">: false]
	</txp:if_variable>
<txp:output_form form="ab_logo" />
	[SQL (3,50475311279E-5): select Form from txp_form where name='ab_logo']
	[Module: ab_logo]
<txp:output_form form="ab_navigation" />
	[SQL (3,981590271E-5): select Form from txp_form where name='ab_navigation']
	[Module: ab_navigation]
	<txp:cbs_navigation_menu sections="default,stock,garanties,faq,over,contact" titles="Home,Stock,Garanties,FAQ,Over,Contact" wraptag="ul" break="li" class="menu" activeclass="current"/>
<txp:image id="6" />
	[SQL (4,48226928711E-5): select * from txp_image where id = 6 limit 1]
<txp:mdn_count section="stock" category="#" />
	[SQL (3,91006469727E-5): SELECT COUNT(*) count FROM textpattern WHERE section='stock' AND (category1='' OR category2='') AND status='4']
<txp:output_form form="ab_sort" />
	[SQL (3,38554382324E-5): select Form from txp_form where name='ab_sort']
	[Module: ab_sort]
	<txp:site_url />
<txp:if_article_list>
	[<txp:if_article_list>: true]
	<txp:article form="ab_auto" limit="15" sort="Title asc" />
		[SQL (0,00146698951721): select count(*) from textpattern where 1=1 and Status = 4 and Posted <= now() and (now() <= Expires or Expires = '0000-00-00 00:00:00') and Section IN ('stock')]
		[SQL (0,00202298164368): select *, unix_timestamp(Posted) as uPosted, unix_timestamp(Expires) as uExpires, unix_timestamp(LastMod) as uLastMod from textpattern where 1=1 and Status = 4 and Posted <= now() and (now() <= Expires or Expires = '0000-00-00 00:00:00') and Section IN ('stock') order by Title asc limit 0, 15]
		[artikel 13]
		[SQL (4,60147857666E-5): select Form from txp_form where name='ab_auto']
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (4,88758087158E-5): select * from txp_image where id='19' limit 1]
			<txp:hak_image />
				[SQL (4,29153442383E-5): select * from txp_image where id = 19 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 14]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (5,00679016113E-5): select * from txp_image where id='596' limit 1]
			<txp:hak_image />
				[SQL (4,57763671875E-5): select * from txp_image where id = 596 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 15]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (4,98294830322E-5): select * from txp_image where id='753' limit 1]
			<txp:hak_image />
				[SQL (4,48226928711E-5): select * from txp_image where id = 753 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 16]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (5,19752502441E-5): select * from txp_image where id='616' limit 1]
			<txp:hak_image />
				[SQL (4,29153442383E-5): select * from txp_image where id = 616 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 17]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (5,07831573486E-5): select * from txp_image where id='683' limit 1]
			<txp:hak_image />
				[SQL (4,38690185547E-5): select * from txp_image where id = 683 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 18]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (4,91142272949E-5): select * from txp_image where id='96' limit 1]
			<txp:hak_image />
				[SQL (4,2200088501E-5): select * from txp_image where id = 96 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 19]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (5,19752502441E-5): select * from txp_image where id='553' limit 1]
			<txp:hak_image />
				[SQL (4,41074371338E-5): select * from txp_image where id = 553 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 20]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (5,00679016113E-5): select * from txp_image where id='792' limit 1]
			<txp:hak_image />
				[SQL (4,41074371338E-5): select * from txp_image where id = 792 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 11]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (4,91142272949E-5): select * from txp_image where id='61' limit 1]
			<txp:hak_image />
				[SQL (4,38690185547E-5): select * from txp_image where id = 61 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 10]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (5,19752502441E-5): select * from txp_image where id='58' limit 1]
			<txp:hak_image />
				[SQL (4,2200088501E-5): select * from txp_image where id = 58 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 25]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (5,19752502441E-5): select * from txp_image where id='131' limit 1]
			<txp:hak_image />
				[SQL (4,19616699219E-5): select * from txp_image where id = 131 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 26]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (5,19752502441E-5): select * from txp_image where id='601' limit 1]
			<txp:hak_image />
				[SQL (4,50611114502E-5): select * from txp_image where id = 601 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 27]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (4,91142272949E-5): select * from txp_image where id='849' limit 1]
			<txp:hak_image />
				[SQL (4,10079956055E-5): select * from txp_image where id = 849 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 29]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (5,00679016113E-5): select * from txp_image where id='150' limit 1]
			<txp:hak_image />
				[SQL (4,19616699219E-5): select * from txp_image where id = 150 limit 1]
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
		[artikel 30]
		[Module: ab_auto]
		<txp:permlink />
		<txp:hak_article_image limit="1" link="0" />
			[SQL (4,31537628174E-5): select * from txp_image where id='19' limit 1]
			<txp:hak_image />
		<txp:permlink/>
		<txp:title />
		<txp:custom_field name="kilometerstand" />
		<txp:custom_field name="brandstof" />
		<txp:custom_field name="bouwjaar" />
		<txp:custom_field name="carrosserie" />
		<txp:custom_field name="kleur" />
		<txp:if_custom_field name="verkocht">
			[<txp:if_custom_field name="verkocht">: false]
			<txp:custom_field name="prijs" />
		</txp:if_custom_field>
</txp:if_article_list>
<txp:if_individual_article>
	[<txp:if_individual_article>: false]
</txp:if_individual_article>
<txp:ob1_pagination previoustext="← Vorige" nexttext="Volgende →" outputlastfirst="0" ulclass="pagination" linkcurrent="1" liselected="selected" pagetext="Pagina" previoustexttitle="← Vorige" nexttexttitle="Volgende →" />
<txp:output_form form="ab_footer" />
	[SQL (4,41074371338E-5): select Form from txp_form where name='ab_footer']
	[Module: ab_footer]
	<txp:php>
	</txp:php>
	<txp:output_form form="ab_stats" />
		[SQL (3,69548797607E-5): select Form from txp_form where name='ab_stats']
		[Module: ab_stats]
[ ~~~ secondpass ~~~ ]
<txp:variable name="sort" value="custom_4" />
 -->

Offline

#9 2010-05-28 18:50:35

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: sorting articles with pull-down

You forgot to set the variable named sort. Your tag trace should have the line:

<txp:variable name="sort" value='<txp:php> echo gps("sort"); </txp:php>' />

But it doesn’t and currently the variable is unset. That line above, have to go before your if_variable conditional statements.

Offline

#10 2010-05-28 22:30:03

element
Member
Registered: 2009-11-18
Posts: 99

Re: sorting articles with pull-down

It’s there but it seems I didn’t add a form to my article. It changes the order but not correct, some articles are listed in correct order, some are not. What’s wrong with that?

When I add the form with variables to the search page my searchresults page have all results 2x. Grmbl.

And can you add pagination to that? I’m using ob1_pagination and when you go to the next page the sorting is gone.

Last edited by element (2010-05-28 23:38:34)

Offline

#11 2012-06-21 13:26:08

fantasmo
Member
Registered: 2010-05-05
Posts: 40

Re: sorting articles with pull-down

Hello,

is this what this plug in does: http://textpattern.org/tips/325/section-and-category-pull-down-menus ?

Currently I want to navigate through categories not by clicking a menue item but by choosing the category from a pulldown…and then the page refreshes and the content of the category shows up.

Is this, what is described in this thread here?
(And by the way…thes plug in seems to be based on java script, I wanted to avoid that…but this solution in the thread here, works without java script?!)
…EDIT: No it’s not a plug in, it’s an code example…but though based on java script.

Last edited by fantasmo (2012-06-21 13:28:23)

Offline

#12 2012-06-21 16:57:06

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,254
Website

Re: sorting articles with pull-down

I don’t think a select input element can trigger a change to the current page without JavaScript. Maybe you could use some pure css menu that acts like a dropdown? Something like this css3 dropdown

Last edited by MattD (2012-06-21 17:00:40)


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

Board footer

Powered by FluxBB