We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Help with plugin please? - Having trouble with a loop - I think?

Development and Programming

Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hi Daniel,

Thanks loads for your help on this one. I placed the print_r($vars); at the end of the code after the $count++; I now get this sent to the page :

Array ( [item_id] => 10 [item_name] => Item One [mb_total_results] => 3 [mb_count] => 1 ) Array ( [item_id] => 56 [item_name] => Some Item [mb_total_results] => 3 [mb_count] => 2 ) Array ( [item_id] => 73 [item_name] => Oops [mb_total_results] => 3 [mb_count] => 3 )

Looks like things are getting set but not too sure why they aren’t getting to the template?

Thanks again for any help on this one. I keep looking through the code up and down and although I still don’t totally understand it all what you have written, to me, looks like it should be working.

Best wishes,

Mark

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Sorry to be a pain on this one Daniel.

I also just tried placing in :

print_r($temp);

at the end of everything too and that spits out everything (more or less) correctly to the page as I need it to come out but it just seems as though the single variables aren’t getting swapped out somewhere. Really not too sure what to do now. Have been swapping things around and up and down all day now! 😉

Just wondered if you possibly had any more time to take a little looksie at this for me again? 😉

Thanks for any help on this.

Best wishes,

Mark

       
jeff_openmotive's avatar
jeff_openmotive
39 posts
17 years ago
jeff_openmotive's avatar jeff_openmotive

Two quick things Mark, if you are like me and enjoy little bits of sanity whenever you can, this might help.

First off you should likely wrap your print_r() inside a pre tag:

<pre>
print_r($temp);
</pre>

<p>
</pre>

Much easier to read that way, but print_r as a function does not give you as much information as var_dump (red headed step child of var_dump 😊 ) will. Using var_dump you can actually see the type of your elemets as well as their value. Again, wrap it in a pre tag to make it human readable.

<pre>
var_dump($temp);
</pre>

<p>
</pre>

Just a couple tips, I got a little lost in trying to follow the whole thread.

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hiya,

Thanks for that. Yep it makes it all look a little more easier to read but I still can’t get anything to actually output to the template which is really weird.

Don’t suppose one of the EE dev team or moderators would care to put me out of my misery would they. Have been trying this for the past few days now and I just feel like I’m going backwards. Just a little shove would be really handy. I have looked at all the core files to see how they handle it and now I just feel like I’m swimming in a load of 0s and 1s!!

Any help on this would be massively appreciated.

Best wishes,

Mark

       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

Mark can you post up your template code? Might give some hints as to what’s happening

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hi Daniel,

If you don’t mind I would love to 😉

I’m a little lost as to where I am now myself though 😊

Template Code

{exp:test_conditionals}
<h3>{title}</h3>
Qty = {qty}
Count - {count}
Results - {total_results}
{if count == 2}
This should hopefully show on the second iteration
{/if}
{/exp:test_conditionals}

Plugin Code

<?php

$plugin_info = array(
    'pi_name'            => 'Test Conditionals',
    'pi_version'        => '1.0.0',
    'pi_author'            => 'Mark Bowen',
    'pi_author_url'        => 'http://www.markbowendesign.com/',
    'pi_description'    => 'Testing conditionals',
    'pi_usage'            => Test_conditionals::usage()
);


class Test_conditionals {
    var $return_data = '';

    function Test_conditionals()
    {
        global $TMPL, $DB, $SESS, $FNS;
        $ids = array(1,2,3,4);
        $count = 1;
        $total_results = count($ids);


        foreach ($ids as $count => $vars)
        {
            $tagdata = $TMPL->tagdata;
            

            $vars['count']            = $count+1;
            $vars['total_results']    = $total_results;

            /** ----------------------------------------
            /**  Conditionals
            /** ----------------------------------------*/

            $tagdata = $FNS->prep_conditionals($tagdata, $vars);


                foreach ($TMPL->var_single as $key => $val)
                {
                    if ($key == "title")
                    {
                    $title = "Title";
                    $tagdata = $TMPL->swap_var_single($key, $title, $tagdata);
                    }
                    if ($key == "qty")
                    {
                    $qty = "2";
                    $tagdata = $TMPL->swap_var_single($key, $qty, $tagdata);
                    }
    
    
                }
                    $this->return_data .= $tagdata;
        }
    }






// ----------------------------------------
//  Plugin Usage
// ----------------------------------------
function usage()
{
ob_start(); 
?>

--- Plugin Tag ---
{exp:test_conditionals}
{count}
{total_results}
{/exp:test_conditionals}


<?php
$buffer = ob_get_contents();
    
ob_end_clean(); 

return $buffer;
}
// END
}
?>

I have renamed the plugin for now just whilst testing this all out so I don’t get so confused. This is the code I am currently working with. It exchanges the {title} and {qty} variables on the template for me but the only way I could get a count to spit out is by adding in another if ($key == “count”). This then gives me a count number but I can’t use it by doing this :

{if count == 2}
Second iteration
{/if}

It does work if I do :

{if "{count}" == 2}
Second iteration
{/if}

but I know that this is not the right thing to do. I’m quite at a loss now as I tried something out last night. I took the mod.query.php file and basically turned it into a plugin by renaming it to pi.query_test.php and then changing the class and function names inside to Query_test so that it would work as a plugin instead. It still did the exact same thing that it did as a module so I got to looking at the code in that file and I am pretty sure that I am doing the same thing in my plugin that is going on in there and yet they get a count variable which works in conditionals and mine just doesn’t want to play ball!! :down: All very frustrating!

Any help on this would be massively appreciated as I have now been through about 50 or so changes to the plugin, none of which are getting me anywhere closer to where I need to be! I’m sure I must be fairly close but just missing something really obvious here!

Thanks for any more help on this it really is appreciated a lot.

Best wishes,

Mark

       
Derek Jones's avatar
Derek Jones
7,561 posts
17 years ago
Derek Jones's avatar Derek Jones

Mark, you really need to get error reporting enabled on your server. It’s not helping your developmental learning at all to work in a vacuum. The following would display for each loop:

Warning: Cannot use a scalar value as an array in pi.test_conditionals.php on line 29

Warning: Cannot use a scalar value as an array in pi.test_conditionals.php on line 30

Code at fault:

foreach ($ids as $count => $vars)
    ...
$vars['count']            = $count+1;
$vars['total_results']    = $total_results;

$count and $vars are key => val pairs from the $ids array. You can’t then use $vars as an array. Since you aren’t using it in the loop at all, the easiest solution is to change the name of the variable you’re using for the values.

foreach ($ids as $count => $values)

Get display_errors enabled on your server, and keep your error_reporting() level at E_ALL, and PHP and MySQL errors displayed in your EE Output and Debugging settings. It’s futile to write code in an environment that doesn’t give you error feedback.

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Eek!!

Derek thanks for that.

I had put :

error_reporting(E_ALL);
ini_set('display_errors', 1);

into the index.php file after the last time you told me about it but unfortunately (or should that be stupidly! 😉 ) was testing this plugin out in a different install of ExpressionEngine where I hadn’t in fact got the error reporting turned on!! What a complete wally!!

Still probably may not have understood what the error meant if I had have seen it but at least I would have known something was going wrong so thanks for all that! 😉

Have now changed it to :

foreach ($ids as $count => $values)

and it works fine so thanks for that. Just need to go away and understand everything that you have said and then I have one other item I have to figure out, hopefully on my own this time 😉

Thanks again Derek for taking the time out on this one, it is really very much appreciated.

Best wishes,

Mark

       
Derek Jones's avatar
Derek Jones
7,561 posts
17 years ago
Derek Jones's avatar Derek Jones

Receiving an error message that you don’t understand and have to Google or ask for help on is infinitely better than just scratching your head at unexpected behavior, heh. Why not just use MAMP and keep error reporting enabled at the environment level so you don’t have to modify your files to make sure you have errors reported?

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Well there’s one I had overlooked!! 😊

Thanks for the advice on MAMP. Actually I did have them turned on but only to the log and not to the display!! As B.A. Baracus would say :

“Mark, you crazy fool!!”

Thanks again Derek. WIll leave that on forever from now on.

Hope you have a great weekend.

Best wishes,

Mark

       
1 2

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.