Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2007-11-17 07:04:33
- mhulse
- Plugin Author
- From: Eugene Oregon
- Registered: 2005-01-21
- Posts: 200
[tip] Output <txp:category_list /> as array...
Not sure if this will help others… But here is how I got a list of article non empty categories in an array for further manipulation:
# Convert string to array by mhulse.
## @param1 = Delimited string.
## @param2 = String separator, default is a comma.
function string_to_array($str, $sep = NULL) {
$def = ','; // Default seperator.
$sep = ($sep == NULL) ? $def : $sep; // If no seperator is defined, use default.
$str = eregi_replace(' +', '', $str); // Remove all consecutive space chars.
$parts = explode(',', $str); // Break the string into pieces that are separated by commas and place them into an array.
return $parts;
}
$category_list_atts = array('section' => 'article', 'break' => ','); // Setup attributes for txp:category_list.
$category_list = category_list($category_list_atts); // Call txp:category_list.
$category_list = strip_tags($category_list); // Remove all html tags.
$category_list = string_to_array($category_list); // Call string_to_array().
print_r($category_list); // Print array.
Output:
Array ( [0] => news [1] => tutorials [2] => video )
Hth’s!
Cheers,
Micky
Last edited by mhulse (2007-11-27 18:20:31)
Offline
Re: [tip] Output <txp:category_list /> as array...
This:
function string_to_array($str, $sep = NULL) {
$def = ','; // Default seperator.
$sep = ($sep == NULL) ? $def : $sep; // If no seperator is defined, use default.
Is equivalent to:
function string_to_array($str, $sep = ',') {
… but if you’re already using PHP, this can be done much more efficient by using a MySQL query that fetches the categories directly as an array. Instead of letting category_list get them, encode them, then strip them again and turn the string into an array. Why not do it like this ?
Neither will give you an array of non-empty categories, btw. You’d have to use a INNER JOIN between the textpattern and txp_category table to achieve that.
Offline
#3 2007-11-17 19:22:26
- mhulse
- Plugin Author
- From: Eugene Oregon
- Registered: 2005-01-21
- Posts: 200
Re: [tip] Output <txp:category_list /> as array...
Hi ruud. Thanks for the feedback. Glad to get some critiques via the pro’s! :D
Unfortunately, the MySQL that I could write (er, copy) returned things I did not want (for example: image and link categories.) I quickly used what was available via tags because I am not so great at writing queries. :(
The example you point to had the above problem of giving me unwanted stuff.
Man, I wish I were better at queries. :(
Thanks for the feedback.
Last edited by mhulse (2007-11-27 18:19:33)
Offline
#4 2007-11-17 19:24:13
- mhulse
- Plugin Author
- From: Eugene Oregon
- Registered: 2005-01-21
- Posts: 200
Re: [tip] Output <txp:category_list /> as array...
Just to clarify, I use cbs_category_list
plugin:
$category_list = cbs_category_list($category_list_atts); // Call cbs_category_list.
That returns non-empty cats.
Last edited by mhulse (2007-11-27 18:10:23)
Offline
#5 2007-11-27 18:18:10
- mhulse
- Plugin Author
- From: Eugene Oregon
- Registered: 2005-01-21
- Posts: 200
Re: [tip] Output <txp:category_list /> as array...
Can anyone give me suggestions on how to write an efficient TXP query for this task?
I would totally opt for a query over my above coding… I just do not know where to start when it comes to learning such a thing.
This approach worked nicely, but gave me Image and Link categories too…
I guess I could hack the cbs_category_list
plugin, and have it just return what I want (array of non-empty cats.)
Any tips for learning how to write queries in TXP?
Thanks!
Cheers,
Micky
Offline
Re: [tip] Output <txp:category_list /> as array...
That approach you referred to can be limited to just article categories like this:
$test = doSpecial(safe_column('title', 'txp_category', "type='article' AND name != 'root' ORDER BY title ASC"));
print_r($test);
Offline
#7 2007-11-27 18:37:42
- mhulse
- Plugin Author
- From: Eugene Oregon
- Registered: 2005-01-21
- Posts: 200
Re: [tip] Output <txp:category_list /> as array...
Wow! Thanks Ruud! I greatly appreciate the code help… A query is much better than my hacks… Hehe.
Going to read about INNER JOIN now.
Are there any good plugins and/or tools to test queries on a TXP DB?
A billion thanks!
Cheers,
Micky
Offline
Re: [tip] Output <txp:category_list /> as array...
Hehe… you may want to try running queries from within phpmyadmin or directly from the commandline using the mysql program, but perhaps that rss_db_admin plugin (or one that has a similar name) allows you to test the queries from with TXP. That’s mostly useful if you have set a prefix for your TXP tables. One hint about that JOIN thing…. you’ll want to use GROUP as well in your query.
Offline
#9 2007-11-27 18:53:13
- mhulse
- Plugin Author
- From: Eugene Oregon
- Registered: 2005-01-21
- Posts: 200
Re: [tip] Output <txp:category_list /> as array...
Thanks again Rudd!
I will be back with the updated query once I figure it out. This will be good practice for me. :)
Have a great day.
Offline
#10 2007-11-27 21:59:14
- mhulse
- Plugin Author
- From: Eugene Oregon
- Registered: 2005-01-21
- Posts: 200
Re: [tip] Output <txp:category_list /> as array...
Well, I cheated just a little bit. :D
I hacked cbs_category_list and created mh_category_array.
I have not given-up on learning more about queries, but for now this seems better than my original approach.
Thanks again Rudd, I really appreciate all of your help on this.
Cheers,
Micky
Last edited by mhulse (2007-11-27 22:00:59)
Offline
Re: [tip] Output <txp:category_list /> as array...
Building your own plugins is a good learning experience. I’ve learned quite a bit while writing the rvm_latin1_to_utf8 plugin about indexes, charsets and collation. Probably more than I wanted to know ;)
Offline
#12 2007-11-27 23:00:46
- mhulse
- Plugin Author
- From: Eugene Oregon
- Registered: 2005-01-21
- Posts: 200
Re: [tip] Output <txp:category_list /> as array...
Wow, I bet! rvm_latin1_to_utf8
sounds like a complicated one. :)
It has definitely been nice to finally dig-into TXP plugins… I wrote a couple of plugins for Expression Engine, and I really enjoyed the learning experience and sharing with others.
Now, I just need to get good at making queries… I am much better at working with flat-files. ;)
Thanks again for all of your help and feedback, and have a great day Ruud.
Cheers,
Micky
Offline