Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-01-22 19:44:28

nabrown78
Member
From: Northampton, MA, USA
Registered: 2006-10-04
Posts: 294
Website

Is it possible to get the number of images in a particular category?

I’d like to use this bit of information in a misc form – basically to generate a random offset number that is not greater than 10 fewer than the total number of images (so that there will always be 10 images to output)… Kind of weird thing to want, but there you have it.

I realize this may require an SQL query, and if it does, can this be done within a form? Is that a horrible idea?

Any advice appreciated,
Nora

Offline

#2 2009-01-22 20:00:17

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: Is it possible to get the number of images in a particular category?

Maybe this?

<txp:php>
$rs = safe_rows('id', 'txp_image', '1=1');
$num_images = count($rs) - 10;
if ($num_images>0) {
return rand(0, $num_images);
}
</txp:php>

Put it in a form, and then use the form output as the offset attribute. Just guessing! No idea if it’d work or not.

Last edited by jsoo (2009-01-22 20:00:43)


Code is topiary

Offline

#3 2009-01-22 20:58:48

nabrown78
Member
From: Northampton, MA, USA
Registered: 2006-10-04
Posts: 294
Website

Re: Is it possible to get the number of images in a particular category?

Awesome – thanks jsoo. I modified the code slightly as follows, to limit to one category, and to account for the fact that the images must always be in pairs.

<txp:php>
$rs = safe_rows('id', 'txp_image', '1=1 and category in ("Before-and-After")');
$half_num_images = (count($rs) - 10)/2;
if ($half_num_images>0) {
echo (rand(0, $half_num_images))*2;
}
</txp:php>

I put that code in a form called ‘random_offset’ and then I’m using wet_for_each_image to output the list:

<txp:wet_for_each_image category="Before-and-After" offset='<txp:output_form form="random_offset" />' limit="10" sort="name">
...
</txp:wet_for_each_image>

For anyone curious zem_nth works brilliantly with wet_f_i_i !

Last edited by nabrown78 (2009-01-22 21:04:01)

Offline

#4 2009-01-23 03:14:25

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: Is it possible to get the number of images in a particular category?

Glad it worked. Since you have category in your where clause you can ditch the “1=1”. Makes no difference to the result.


Code is topiary

Offline

#5 2009-01-23 04:27:24

nabrown78
Member
From: Northampton, MA, USA
Registered: 2006-10-04
Posts: 294
Website

Re: Is it possible to get the number of images in a particular category?

Oh, cool. What is the “1=1”? I was kind of puzzled by it…

Offline

#6 2009-01-23 04:59:16

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Re: Is it possible to get the number of images in a particular category?

1=1 gets around the required where parameter in safe_rows. If you’re not into PHP, you could also use jmd_count to count stuff :).

Offline

Board footer

Powered by FluxBB