weboz Posted September 2, 2012 Share Posted September 2, 2012 Hallo, i'm desperate.. I have tried 5 forums without anyone to answer me, so trying this as last tryout Please read the note in the code <!-- -->, need to echo <ul> without looping. Need help with this code: {foreach $categories_array as $category} <!-- need a code to print <ul class="cat1" without looping--> <li class="cat1 {$class}" style="margin-left:10px;"><a href="{$category.href}">{$category.name}</a></li> {elseif $category.spacing == '2'} <!-- need a code to print<ul class="ul-cat2" without looping --> <li class="cat2 {$class}" style="margin-left:{0+$category.spacing*10}px;" ><a href="{$category.href}">{$category.name}</a></li> {else} <li class="cat0-sub {$class}" style="margin-left:{0+$category.spacing*10}px;" ><a href="{$category.href}">{$category.name}</a></li> {/if} {/foreach} Quote Link to comment https://forums.phpfreaks.com/topic/267911-loop-in-tpl-php-script-help/ Share on other sites More sharing options...
Hydrian Posted September 2, 2012 Share Posted September 2, 2012 try print ('<ul class="cat1"> or what ever); Quote Link to comment https://forums.phpfreaks.com/topic/267911-loop-in-tpl-php-script-help/#findComment-1374611 Share on other sites More sharing options...
Pikachu2000 Posted September 2, 2012 Share Posted September 2, 2012 How do you expect to not loop from within a loop? Quote Link to comment https://forums.phpfreaks.com/topic/267911-loop-in-tpl-php-script-help/#findComment-1374612 Share on other sites More sharing options...
weboz Posted September 2, 2012 Author Share Posted September 2, 2012 try print ('<ul class="cat1"> or what ever); I'm pretty sure this will loop. Need to find a method to stop <ul> from looping for each <li>. Quote Link to comment https://forums.phpfreaks.com/topic/267911-loop-in-tpl-php-script-help/#findComment-1374613 Share on other sites More sharing options...
Mahngiel Posted September 2, 2012 Share Posted September 2, 2012 You'll need to correctly set up your array and do multiple loop levels. Consider: Array( [0]=>[category1] Array([0]=>item1 [1]=>item2 [2]=>item3] ) [1]=>[category2] Array([0]=>item1 [1]=>item2 [2]=>item3] ) ) Now, you have a multidimensional array and can foreach( $array as $category ) echo '<ul>'; foreach( $category as $key=>$item ) echo "<li>{$item}</li>"; dump the array for a more explicit answer Quote Link to comment https://forums.phpfreaks.com/topic/267911-loop-in-tpl-php-script-help/#findComment-1374688 Share on other sites More sharing options...
DavidAM Posted September 2, 2012 Share Posted September 2, 2012 I haven't worked with .tpl files before, but I know a whole lot of programming languages. Not a single one of them will let you have an elseif without an if before it. If your if is outside of the foreach then this code will never work. You need to re-think your logic. If you provide a little more of the file and a good explanation of what you are trying to accomplish, I'm sure someone here can point you in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/267911-loop-in-tpl-php-script-help/#findComment-1374691 Share on other sites More sharing options...
weboz Posted September 2, 2012 Author Share Posted September 2, 2012 I haven't worked with .tpl files before, but I know a whole lot of programming languages. Not a single one of them will let you have an elseif without an if before it. If your if is outside of the foreach then this code will never work. You need to re-think your logic. If you provide a little more of the file and a good explanation of what you are trying to accomplish, I'm sure someone here can point you in the right direction. Yeah i know, looks like i skipped a line when i copied the code snip. As for better information, i do not have any more information, because the system provider for this system will not let me see more of the codes. So this is what i have to work with. Going to insert a drop-out menu, and for this i need <ul> to be printet correctly. {foreach $categories_array as $category} {if $category.spacing == '1'} <li class="cat1 {$class}" style="margin-left:10px;"><a href="{$category.href}">{$category.name}</a></li> {elseif $category.spacing == '2'} <li class="cat2 {$class}" style="margin-left:{0+$category.spacing*10}px;" ><a href="{$category.href}">{$category.name}</a></li> {else} <li class="cat0-sub {$class}" style="margin-left:{0+$category.spacing*10}px;" ><a href="{$category.href}">{$category.name}</a></li> {/if} {/foreach} Quote Link to comment https://forums.phpfreaks.com/topic/267911-loop-in-tpl-php-script-help/#findComment-1374756 Share on other sites More sharing options...
spiderwell Posted September 14, 2012 Share Posted September 14, 2012 why dont you just echo the <ul> outside of the loop, or did i miss something obvious here? Quote Link to comment https://forums.phpfreaks.com/topic/267911-loop-in-tpl-php-script-help/#findComment-1377827 Share on other sites More sharing options...
Hall of Famer Posted September 19, 2012 Share Posted September 19, 2012 Using logic code in .tpl file is not recommended. It's a good practice to provide one more layer of separation of responsibility. Create a controller or view class that handles php code processing, the .tpl file is better off simply outputting html elements and assigned php variables. Quote Link to comment https://forums.phpfreaks.com/topic/267911-loop-in-tpl-php-script-help/#findComment-1379254 Share on other sites More sharing options...
premiso Posted September 19, 2012 Share Posted September 19, 2012 Using logic code in .tpl file is not recommended. You have to have some logic in it. He is using smarty for his .tpl. View logic, does require some logic to not duplicate code and even build stuff dynamically. You are on the right track, but that statement is just wrong. Now if he was doing Business Logic in the view Logic, ok. But looping through data and displaying it conditionally is OK to do and is needed, unless you feel like making 500 files and duplicating a ton of code. And... try print ('<ul class="cat1"> or what ever); What? *premiso shakes his head slowly. And... I haven't worked with .tpl files before It is just Smarty, I don't think the .tpl extension really matters just what smarty people tend to use. I am quite partial to .phtml myself Quote Link to comment https://forums.phpfreaks.com/topic/267911-loop-in-tpl-php-script-help/#findComment-1379261 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.