PDA

View Full Version : Looping


1Unreal
03-04-2009, 08:38 AM
Im trying to get my head round more of this plugin stuff.

In vB options I have a textarea. I explode that out into an array then I want to loop each item to display in an unordered list where ever on the template.

How can I make this more compatable with vB?


if($vbulletin->options['enabled'] == 1)
{
$items = explode("\n", $vbulletin->options['html']);
echo '<ul>';
foreach($items as $item)
{
echo '<li>'.$item.'</li>';
}
echo '</ul>';
}


Thanks :)

TigerC10
03-04-2009, 03:21 PM
Your only issue is the second <ul> tag. It should be a closing </ul> instead. Aside from that it should work, as long as the $vbulletin->options['html'] variable has newline characters in it. Otherwise it will all display on one line.

As a matter of convension, you should change those option names, go with a prefix like...
$vbulletin->options['mymod_enabled']
$vbulletin->options['mymod_html']


That way nothing interferes with one another later on down the road.

1Unreal
03-04-2009, 04:36 PM
Sorry that was a problem with my syntax but the original problem still exists.

How can I display this loop in a template?

Would it be best to write whatever the loop outputs to a template then include that template on another template?

If so, how would I do that?

Lynne
03-04-2009, 05:00 PM
You need to put that php in a plugin - php cannot go into a template.

1Unreal
03-04-2009, 05:29 PM
Yeah I know, I've got trouble showing the contents of this plugin though.

TigerC10
03-05-2009, 03:34 AM
To get it in a template, you shouldn't echo. You need to stuff it into a variable like $output_list.


if($vbulletin->options['enabled'] == 1)
{
$items = explode("\n", $vbulletin->options['html']);
$output_list = '<ul>';
foreach($items as $item)
{
$output_list .= '<li>'.$item.'</li>';
}
$output_list .= '</ul>';
}


Then, in your template just put the $output_list variable where you want it to display. Tah-dah! The template engine will just output any variable values if you just call it..


<if condition="$vbulletin->options[mymod_enabled]">
$output_list
</if>


Can you dig it?

1Unreal
03-05-2009, 04:34 AM
That doesn't work :|

Dismounted
03-05-2009, 04:56 AM
You may want to check if items exist in $items first.

1Unreal
03-05-2009, 06:05 AM
Yeah there is.

Dismounted
03-05-2009, 08:00 AM
I meant add a PHP code check, and then you can just do (in the template):
<if condition="$output_list">$output_list</if>

1Unreal
03-05-2009, 08:13 AM
To simplify everything to try to root out the problem I did this:


$list = 'Item1 Item2 Item3';

$items = explode(' ', $list);
$output_list = '<ul>';
foreach($items as $item)
{
$output_list .= '<li>'.$item.'</li>';
}
$output_list .= '</ul>';


On the template I put this:


<if condition="$output_list">
$output_list
<else />
Nothing in output_list
</if>


It just said "Nothing in output_list"

I don't know what it could be :|

Dismounted
03-05-2009, 10:37 AM
What template and hooks are you placing the code in?

1Unreal
03-05-2009, 10:50 AM
None, thats my full code. I didn't think I'd need to use hooks :o

Dismounted
03-05-2009, 11:18 AM
What is your file then?

1Unreal
03-05-2009, 11:27 AM
Sorry, Im being an idiot...its the hook is global_complete