Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2012-01-18 07:03:43

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

Re: txp:images with image id in url instead of page number

photonomad wrote:

<txp:images break="" sort='FIELD(ID,<txp:custom_field name="article_image" />)'>

Keep in mind that the above doesn’t prevent SQL injections, accidental or otherwise. I wouldn’t use that on multi-author/portal website. For example adding truncate textpattern (etc) to an article’s Article Image field would make the above code, when executed, to delete all articles.

$currimg = gps('image');

Would be best if you casted the above as integer, i.e. $currimg = (int) gps('image');. Trying to calculate with anything else won’t work as expected.

Offline

#14 2012-01-18 07:14:27

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

Re: txp:images with image id in url instead of page number

Ah, I think bloke forgot to grab the value for the sequence number. Try this (it adds $imgs[…] around the respective $found sequence number:

<txp:php>
global $variable, $thisarticle;
$imgs = do_list($thisarticle['article_image']);
$currimg = (int) gps('image');
$found = ($currimg) ? array_search($currimg, $imgs) : 0;
$variable['curr_image'] = $imgs[$found];
$variable['prev_image'] = ($found < 1) ? '' : $imgs[$found-1];
$variable['next_image'] = ($found >= count($imgs)-1) ? '' : $imgs[$found+1];
</txp:php>

Then in the rest of your form use:
previous image ID#: <txp:variable name="prev_image" />
current image ID#: <txp:variable name="curr_image" />
next image ID#: <txp:variable name="next_image" />

Don’t change the zero value to one as array positions in a sequence start from 0. You shouldn’t need any variables in variables. For example if you want the title of the next image you can do <txp:image_info id='<txp:variable name="next_image" />' type="caption" />.

Last edited by jakob (2012-01-18 07:16:10)


TXP Builders – finely-crafted code, design and txp

Offline

#15 2012-01-18 09:07:17

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

Re: txp:images with image id in url instead of page number

jakob wrote:

I think bloke forgot to grab the value for the sequence number.

Ah yes, I was outputting the index just to check the numbers were right and forgot to actually display the values when I was done. Thanks for spotting that. Sorry for the confusion.

One thing at the moment is that at the extremities of the list (missing ?image, first image, last image) the prev/next will be set empty as appropriate. Thus you can’t blindly add the prev_image and next_image variables into your page flow without testing if the value exists first:

<txp:if_variable name="prev_image" value="">
<txp:else />
 ... display your image here ...
</txp:if_variable>

Gocom wrote:

Would be best if you casted the above as integer, i.e. $currimg = (int) gps('image');

I actually put the cast in the first version then took it out in a later edit. Stupid of me. Thanks for pointing it out. I’ll edit the post above to fix it.

Last edited by Bloke (2012-01-18 09:14:54)


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

Board footer

Powered by FluxBB