Welcome Guest, Not a member yet? Register   Sign In
How to begin a foreach loop in one function and end it in another. Possible?
#1

[eluser]Gwarrior[/eluser]
Code:
function create_loop($loop_post_name, $content, $order="desc") {


            return 'foreach ($test as $t) :';
                
            
        }
            
        function loop_data() {
            
               return 'echo $t->example';
            
        }
        
        function end_loop() {
            
            return 'endforeach;';
            
        }

Above is an example of what I speak of. It's pretty imperative that it functions this way, as I have designed an entire CMS thus far around this model. Yet when I run it, it generates the error:

Quote:Fatal error: Cannot redeclare create_loop() (previously declared in C:\wamp\www\contentcreator\system\application\helpers\cc_helper.php:42) in C:\wamp\www\contentcreator\system\application\helpers\cc_helper.php on line 42


Here is my usage of it:

Code:
<?php create_loop('News Posts', 'article'); ?>
    
    
    <?php end_loop(); ?>

So obviously I am doing something wrong. Problem is, I have no idea what that something may be. Any help would be great!

*ps, the functions are being called from a helper and I am aware they would produce no content and an error if ran this way, but I can even get to that point, therefore I didn't include database calls and other things that would be looped.
#2

[eluser]bigtony[/eluser]
Sounds like the helper is being included more than once. You could wrap your helper function within an existance check:
Code:
if (! function_exists('create_loop'))
{
    function create_loop(etc...
    {
        // your code...
    }
}
#3

[eluser]Colin Williams[/eluser]
First of all, please realize that your functions just return strings. Nothing is going to happen when you successfully run the functions. You would have to eval() the function result: eval(create_loop('News Posts', 'article)Wink;

My suggestion is that you go back to the drawing board.
#4

[eluser]Gwarrior[/eluser]
What other way would I have of deploying this? I know I've seen MANY CMS's do something very similar. Sometimes it's with a templating language like Wordpress and sometimes it's with functions, like Perch.

If it's not possible, hey, I won't do it. But there has to be another workaround other than making users write:

Code:
<?php
$loop = get_loop_contents($example, $example);

foreach($loop as $row) {

echo $row->post_name;

}




Theme © iAndrew 2016 - Forum software by © MyBB