• Hello!

    Code in a template runs in the global context. Code in the widget will run in a function context. Make sure that you declare any global variables as global before attempting to use them”

    What does that mean?
    How do i configure the code below to work in my widget that can handle php?

    <?php $lunch = get_group('lunch');  foreach($lunch as $lunch){
    echo '<span class="lunch-dag">' . $lunch['lunch_dag'][1]. '</span><br />';
    echo '<span class="lunch-matratt">' . $lunch['lunch_matratt'][1]."<br />";
    echo $lunch['lunch_matratt'][2]."<br />";
    echo $lunch['lunch_matratt'][3]."<br />";
    echo $lunch['lunch_matratt'][4]. '</span><br /><br />';}?>

    The code works perfect in a template!

    Best regards
    Andy

Viewing 5 replies - 1 through 5 (of 5 total)
  • I believe you’ll have to declare $lunch as a global variable, i.e.

    global $lunch = get_group('lunch');

    Thread Starter AndySwede

    (@andyswede)

    I have tried that but get error message.
    Hmmm…
    I have also been told (in a very rude way) that this foreach($lunch as $lunch) make no sense at all, and it may be so but it still works in my template and does the job.

    As surely everybody understands im a total novice when it comes to this. I have made some “custom fields” in backend and want to show them on my page, frontend, sorry for my bad english, and it works without no problems at all when i use that code in a template.

    How would u write it in proper way?

    Ah, yes, I should have noticed that before. foreach($lunch as $lunch) is not correct. This might help clarify how foreach loops should be structured

    Dion

    (@diondesigns)

    You can’t declare the same variable in foreach(){}. If that worked for you previously, you were very lucky. Try something like this:

    <?php
    $lunch_ary = get_group('lunch');
    foreach ($lunch_ary as $lunch) {
    	echo '<span class="lunch-dag">' . $lunch['lunch_dag'][1]. '</span><br />';
    	echo '<span class="lunch-matratt">' . $lunch['lunch_matratt'][1]."<br />";
    	echo $lunch['lunch_matratt'][2]."<br />";
    	echo $lunch['lunch_matratt'][3]."<br />";
    	echo $lunch['lunch_matratt'][4]. '</span><br /><br />';
    }
    ?>

    If the above doesn’t work, then we must start digging. What is this get_group() function? Is it globally accessible?

    EDIT: is this code supposed to loop? Your code makes me wonder if the echo statements are meant to only be executed once…

    EDIT: is this code supposed to loop? Your code makes me wonder if the echo statements are meant to only be executed once…

    Maybe that is what the

    foreach($lunch as $lunch)

    is about, will only process the first item in the array, stepping to the 2nd item probably fails.

    I think that:

    $lunch_ary = get_group('lunch');
    print_r($lunch_ary);

    is called for.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Php in widget issue!’ is closed to new replies.