Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-06-17 16:00:52

hicks
Member
From: Portland, OR, USA (ex-UK)
Registered: 2009-05-08
Posts: 106
Website

variable value="<txp:...>" to stick across pages. PHP?

Hello!

I’d like a variable value generated from a <txp:page_URL/> or <txp:section/> to be remembered from one page to the next, if that next page does not call a regeneration of the value. As I have it, the value just disappears. Is this something PHP could magic up? I know nothing about PHP.
(To explain why, I want content on that ‘next’ page to be effected by those values generated on the first, eg. by using <txp:article_custom id='<txp:variable name="something"/>'/> or <txp:article_custom offset='<txp:adi_calc name="orother" multiply=3"/>' /> [where orother is a variable value generated by value='<txp:page_URL type="pg"/>' on the first page] on the ‘next’ page. Maybe I shouldn’t have explained, that sounds confusing to me and I’m the one trying to do it.)

Thanks!

hicks.

Last edited by hicks (2009-06-17 16:01:50)

Offline

#2 2009-06-17 16:12:34

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: variable value="<txp:...>" to stick across pages. PHP?

hicks wrote:

I’d like a variable value generated from a <txp:page_URL/> or <txp:section/> to be remembered from one page to the next

Short of using a database field to hold the value or using the beta smd_vars plugin which is (ahem, will be) designed for this type of application, the best thing to do on your first page is:

<txp:php>
   session_start();
   $_SESSION['my_page'] = page_url(array("type" => "pg"));
</txp:php>

and then on your second page, read that value back and stuff it in a <txp:variable>:

<txp:php>
   global $variable;
   session_start();
   $variable['something'] = $_SESSION['my_page'];
</txp:php>

From then on you can use <txp:variable name="something"/> to retrieve your value from the page before.

It’s untested, but something along those lines should help.

Last edited by Bloke (2009-06-17 16:13:34)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2009-06-17 20:24:39

hicks
Member
From: Portland, OR, USA (ex-UK)
Registered: 2009-05-08
Posts: 106
Website

Re: variable value="<txp:...>" to stick across pages. PHP?

Thanks, Bloke.

I dropped that in roughly and it seems to be working in a test. Now, I also need to remember the section and article ID in a similar fashion, each with different variable names. Being the have-a-go coder that I’ve come to be, I tried to edit yours in a logical fashion…

$_SESSION['snippets'] = section;

which you’ll likely find hilarious. (“snippets” being the desired variable name for the sections variable.) How should it be done, please, for ID, and section?
(BTW, I’m not just leaning back and waiting for genii to answer my questions, I’m trying to make sense of it all at php.net as well…)

h.

Offline

#4 2009-06-18 04:54:34

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: variable value="<txp:...>" to stick across pages. PHP?

hicks

Glad it got you going in the right direction and you started experimenting. When delving into PHP there are a couple of things that will help you in TXP:

1) global article variables. The two most useful are $pretext and $thisarticle. If you do this on a page:

<txp:php>
   global $pretext, $thisarticle;
   dmp($pretext);
   dmp($thisarticle);
</txp:php>

You’ll see a whole host of values available to you. For example $pretext['section'] will give you the current section (in article lists or individual articles) and $thisarticle['section'] will give you the section of the current article. You’d normally use $pretext['section'] in most cases; the $thisarticle version is there as a convenience.

2) tag functions. Each TXP tag has an equivalently named PHP function, e.g. <txp:section/> is section(). and <txp:related_articles /> is related_articles()

Pass any attributes to the functions as an array of "name" => "value" pairs, e.g.:

$result = section(array("link" => "1", "wraptag" => "div"));

If you just want the default attributes or the tag you are calling has no attributes you must send it an empty array, e.g.:

$current_excerpt = excerpt(array());

Hope that helps.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#5 2009-06-18 05:19:03

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

Re: variable value="<txp:...>" to stick across pages. PHP?

Bloke wrote:

2) tag functions. Each TXP tag has an equivalently named PHP function, e.g. <txp:section/> is section(). and <txp:related_articles /> is related_articles()

Or alternatively you can parse the tags as tags which could make the code seem cleaner for some. But that method will be slower, as then you have to call the parse(). I would recommend to use the function directly (as Bloke descriped), because we are already in the secrets of PHP, and the direct function does the same. But anyway, you could do, for example:

$var = parse('<txp:section />');

Last edited by Gocom (2009-06-18 05:21:19)

Offline

#6 2009-06-18 16:43:15

hicks
Member
From: Portland, OR, USA (ex-UK)
Registered: 2009-05-08
Posts: 106
Website

Re: variable value="<txp:...>" to stick across pages. PHP?

Ah-ha. I did try section() and it threw up an error message saying there was no argument, or something. So I tried array ("title" => "0") in the brackets, because I idly assumed empty brackets were bad, but no dice.
I do have the Textpattern book, which of course has the global variables in, but no equivalent-PHP-tags explanation that I can find. (They could have explained that within a line or two like you have. Maybe I missed it.)
Is there a PHP for TXP site out there? I hate to keep bothering you.

Glad to see the Europeans and the British (different entities of course) keeping the coding end up. I’m a Brit meself (albeit a soft southerner from Dorset) living in Portland, Oregon, USA. I miss old Blighty… sniff.

Anyway, I’ll try those things after me toast and Marmite ($23 for a 500g jar) and get back to you.

Much appreciation,

hicks.

Offline

#7 2009-06-18 16:56:15

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,323
Website Mastodon

Re: variable value="<txp:...>" to stick across pages. PHP?

Offline

#8 2009-06-21 18:25:55

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: variable value="<txp:...>" to stick across pages. PHP?

hicks

Niiiice :-) Love the way the two columns act independently. Very neat.

smd_vars……….. well I might have done if it was out!? Ha. (Would it work for this?)

Having seen the site in action, I’m not so sure. I plan to allow ‘session’ variables (of sorts) to be stashed in the table in the plugin, so yes it could act as some kind of storage BUT the downside is that the variables currently all have to be named in your Page/Form. Thus, if two people view the site at the same time they’ll fight over the variable name and stuff will act all weird.

My goal would be to allow the plugin to store a per-session variable that gets auto destroyed (a.k.a. ‘invalid’) when the browser session dies. Whether I can pull it off remains to be seen, but I think I can do it when I get some time to make it sing.

Either way, I’d stick with the minimal PHP you have already. It’s fast, lean and does the job with very little overhead.

EDIT: umm, your post has been deleted so now I look like I’m talking to the special friend in my head… ;-)

Last edited by Bloke (2009-06-21 18:28:16)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#9 2009-06-21 20:13:33

hicks
Member
From: Portland, OR, USA (ex-UK)
Registered: 2009-05-08
Posts: 106
Website

Re: variable value="<txp:...>" to stick across pages. PHP?

Bloke, you’re not imagining things!
I deleted the “I’ve done it!” post because I spotted a total boo-boo. I think I’ve fixed it now by switching everything on its head. I think.

Here’s the “I’ve done it!” again….

All right, chaps.

FYI, here’s the site I’m building with your help.
I think I made my multiple-blogs on the same page that I was after in the first place, finally—without Ajax or javascript.

Note the “blogs” on the left and right act independently, as if they were in self-sizing iframes, and each look and act context-sensitive—i.e. with newer/older links when applicable—at all times, regardless of section… well, apart from when the stuff on the left is an individual_article, in which case that just gets a go-home link instead of newer/older. (The stuff on the right doesn’t get indi articles as it’s just snippety bits and I don’t think it needs them.)

If you do visit the site and spot any errors going back and forth and round the houses, please tell me! These might be Older/Newer links not being where they should, or being where they shouldn’t. Note only “reviews” and “shorts” on the right have more than 3 articles, as of my writing this, and therefore need Older/Newers.

The only javascript is for the round-cornering of the divs w/images on the right, the onmouseover snippet link images, and sending people living with the pain of IE 6 or earlier to a page telling them to get with the 2009. (The site does work with IE6, only without the rounded corners and some pseudo-css.)

Plug-ins used to make it happen:
•adi_calc for working out the newer/older page numbers
•smd_vars……….. well I might have done if it was out!? Ha. (Would it work for this?)

Plus, incidentally:
•upm_image to get the article_images as background images on the left blog, to retain round corners (is there a non-plug-in way to do this?)
•smd_where_used is a life-saver for finding any text within any form, page, etc.

You’re all marvellous.

hicks/Chris.

PS: Do you think this is worth a “Let’s See Yours, Then” posting, once it’s all up and running and populated with actual articles?

Last edited by hicks (2009-06-24 18:29:59)

Offline

#10 2009-06-24 18:29:03

hicks
Member
From: Portland, OR, USA (ex-UK)
Registered: 2009-05-08
Posts: 106
Website

Re: variable value="<txp:...>" to stick across pages. PHP?

I’ve moved the site. I’ll stick up the new link when it’s fully up and running.

Offline

#11 2009-06-25 19:56:18

hicks
Member
From: Portland, OR, USA (ex-UK)
Registered: 2009-05-08
Posts: 106
Website

Re: variable value="<txp:...>" to stick across pages. PHP?

Is there a way of binning the cookies that that php made—assuming they are indeed “cookies”, I’m only really accustomed to the type you dunk in tea—when the browser window closes? I have a feeling there might be one or two issues with them if articles are added between visits.

Ta,

hicks.

Last edited by hicks (2009-06-30 22:47:56)

Offline

#12 2009-07-17 21:43:37

hicks
Member
From: Portland, OR, USA (ex-UK)
Registered: 2009-05-08
Posts: 106
Website

Re: variable value="<txp:...>" to stick across pages. PHP?

The long-awaited return is fully up and running
Thanks, all.
-cn

Offline

Board footer

Powered by FluxBB