2 of 2
2
Help with plugin please? - Having trouble with a loop - I think?
Posted: 13 July 2008 05:02 AM   [ Ignore ]   [ # 19 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1097
Joined  03-22-2006

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

 Signature 

(a.k.a the_butcher)

Profile
 
 
Posted: 13 July 2008 06:33 AM   [ Ignore ]   [ # 20 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6557
Joined  04-15-2006

Hi Daniel,

If you don’t mind I would love to wink

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

Template Code

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

It does work if I do :

{if "{count}" == 2}
<p>Second iteration</p>
{/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!! downer 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

 Signature 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 13 July 2008 08:41 AM   [ Ignore ]   [ # 21 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15831
Joined  06-03-2002

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.

 Signature 
Profile
MSG
 
 
Posted: 13 July 2008 08:53 AM   [ Ignore ]   [ # 22 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6557
Joined  04-15-2006

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! wink ) 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! wink

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 wink

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

Best wishes,

Mark

 Signature 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 13 July 2008 09:08 AM   [ Ignore ]   [ # 23 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15831
Joined  06-03-2002

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?

 Signature 
Profile
MSG
 
 
Posted: 13 July 2008 09:13 AM   [ Ignore ]   [ # 24 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6557
Joined  04-15-2006

Well there’s one I had overlooked!! grin

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

 Signature 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
   
2 of 2
2
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64938 Total Logged-in Users: 68
Total Topics: 81907 Total Anonymous Users: 49
Total Replies: 440303 Total Guests: 294
Total Posts: 522210    
Members ( View Memberlist )