Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
How to go throught custom_field as an array and write up a list?
I have for example custom field in which I’ve got this: g,x,f,c
I need to read this content as an array (maybe not – I’m not PHP coder) and then in article form (or inside the plugin) to display this:
<code><ul>
<li class=“g”>Sometext for g</li>
<li class=“x”>Sometext for x</li>
<li class=“f”>Sometext for f</li>
<li class=“c”>Sometext for c</li>
</ul></code>
So I need to read one custom_field values separeted with comma and then write up a UL LI list where the values form custom fields become a class for particular LI. And of course the values I can whenever change so in new article could be there e.g. f,h,s. That’s why I think it must be an array… Text inside particular LI could be changed inside plugin…
Could someone help?
Job: Plugo – tvorba eshopu
Projects: Czech free fonts
Offline
#2 2006-04-05 15:17:33
- KurtRaschke
- Plugin Author
- Registered: 2004-05-16
- Posts: 275
Re: How to go throught custom_field as an array and write up a list?
For use within an article form (would go in a <txp:php>...</txp:php>
block):
global $thisarticle;
$vals = explode(',',$thisarticle['custom_1']);
echo "<ul>";
foreach ($vals as $line) {
echo "<li class=\"$line\">Sometext for $line</li>";
}
echo "</ul>";
This is untested and off the top of my head, but it should work. I’m not exactly sure what is intended for the text inside each <li>
; let me know if this is what you intended or not. Also, the custom field number would need to be changed as appropriate.
Last edited by KurtRaschke (2006-04-05 15:18:05)
kurt@kurtraschke.com
Offline
Re: How to go throught custom_field as an array and write up a list?
Hi KurtRaschke,
your code look very easy, but doesn’t work. Mistake is right at the bigenning because when I add echo $vals; it’s empty. Syntaxe is probably ok, but maybe we need to call $thisarticle in another way. Now it returns:
<code><ul>
<li class=”“>Sometext for </li>
</ul>
</code>
<code> </code>
Could you fix it?
Job: Plugo – tvorba eshopu
Projects: Czech free fonts
Offline
#4 2006-04-06 06:34:47
- whatbrick
- Member
- From: Texas
- Registered: 2006-03-13
- Posts: 100
Re: How to go throught custom_field as an array and write up a list?
I tested it and the code does work, you just need to make sure that $thisarticle['custom_1']
is using the name you gave the first custom_field. So if your first custom_field name is “stuff” (very creative, I know) then you would want to change the code to reflect that, with $vals = explode(',',$thisarticle['stuff']);
.
Check admin » preferences » Advanced preferences for the correct name.
Do not taunt the Markup Monkey!
Offline
Re: How to go throught custom_field as an array and write up a list?
Yes, whatbrick, you’re right. I tried it before as you described, but I made mistake, so I didn’t notice that it works as I need. Now it works perfectly.
Thanks to all for you contribution. I appreciate it.
When my work will be finished on a new site, I will show you to see how much you helped me… (Now I don’t want to be index my site…)
Best Regards,
Jiří Melčák
Last edited by beztak (2006-04-06 07:34:40)
Job: Plugo – tvorba eshopu
Projects: Czech free fonts
Offline
#6 2006-04-06 19:54:10
- KurtRaschke
- Plugin Author
- Registered: 2004-05-16
- Posts: 275
Re: How to go throught custom_field as an array and write up a list?
So the custom fields get populated in $thisarticle
by name and not number, eh? I’ll have to remember that for the future—I don’t ordinarily use the custom fields.
Glad to see the code works as intended.
-Kurt
kurt@kurtraschke.com
Offline