Problems, need help? Have a tip or advice? Post it here.
5 posts Page 1 of 1
Hi,

When I'm using the <cms:php> very often I have the error message ...... eval()'d code. I think that I'm missing something in the couch manual how to avoid this error. Like:

Code: Select all
<cms:php>




    $options = array(
        'credentials'    => array(
            'username'        => 'info@e-tukku.fi',
            'password'        => '****,
            'host'            => '****:143/novalidate-cert/',
            'ssl'            => true,
            'folder'        => 'INBOX'
        ),
        'assetsFolder'    => '../couch/uploads/attachments',
        'sort'            => 'date',
        'limit'            => 5,
    );

    $emailReader = new Email_Reader($options);

    // This will contain (up to) the most recent 10 emails.
    $inbox = $emailReader->readFolder($uId, $headersOnly = true, $saveAssets = false);
   
   
     
    foreach ($inbox as $mailsin){ // this has to be adjusted..
   
        echo "<div> <fieldset>";
        echo "<legend> From: ";
        echo $mailsin['header']['fromaddress'];
        echo "</legend>";
        echo "<small> $mailsin[id] -  $mailsin[uId] </small>";
        echo "<p>";
        echo $mailsin['header']['subject'];
        echo "</p>";
        echo "<p class='label'>";
        echo $mailsin['header']['date'];
        echo "</p>";

        echo "</fieldset> </div> <br/>";
        }
       
     
     //To view a complete list of fields within this email use  print_r($mailsin);
       

</cms:php>


This part works ...
Code: Select all
echo "<p>";
echo $mailsin['header']['subject'];
echo "</p>";


But when I use this ...
Code: Select all
echo " <p>$mailsin['header']['subject']</p>";


I get the Error !

Can someone inform me about this ?

Thanks
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
Hi Tom,

Let us take Couch out of the equation for a moment.

Please try executing the problematic statement with plain PHP e.g.
Code: Select all
<?php
echo " <p>$mailsin['header']['subject']</p>";
?>
You'll get the same error :)

So, the bottom line is - we need to first check if the code we are feeding Couch with is valid PHP.
Else, the invoked PHP interpreter will simply spew out error messages.

Hope this helps.
Hi KK,

o.k. so changed it to
Code: Select all
echo " <p>" .  $mailsin['header']['subject']  . "</p>";
works fine so.

One other question regarding the code i posted here. Is it possible to pass the result of the foreach loop to a couch global variable so i can use that later in my code?

I did try something like :
Code: Select all
    foreach ($inbox as $mailsin){ // this has to be adjusted..
   
        echo "<div> <fieldset>";
        echo "<legend> From: ";
        echo $mailsin['header']['fromaddress'];
        echo "</legend>";
        echo "<small> $mailsin[id] -  $mailsin[uId] </small>";
        echo "<p>";
        echo $mailsin['header']['subject'];
        echo "</p>";
        echo "<p class='label'>";
        echo $mailsin['header']['date'];
        echo "</p>";

        echo "</fieldset> </div> <br/>";
        }

        { //split the header array into variables
       
        $efrom = $mailsin['header']['fromaddress'];
       

         global $CTX;
         $CTX->set('efrom', $efrom, 'global');

         
        }




and later try to get the result with <cms:show efrom />
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
Code: Select all
Is it possible to pass the result of the foreach loop to a couch global variable so i can use that later in my code?
Yes, of course. You are using the right method but since foreach is essentially a loop setting $CTX->set('efrom', $efrom, 'global'); within the loop will always result in holding only the last value of the loop.

The right way would be to concatenate all values in the loop in a single variable (separating each by a pipe '|' char) and then set it as a Couch variable once outside the foreach loop.

Each of the values then can be listed using cms:each tag.
Following is a sample code -
Code: Select all
<cms:php>
    ..
    foreach ($inbox as $mailsin){ // this has to be adjusted..
        ...
        ...
     
        // concatenate each value in a single variable
        $efrom .= $mailsin . '|';
    }

    // set the variable as a Couch variable
    global $CTX;
    $CTX->set('efrom', $efrom, 'global');

</cms:php>

<!-- show each of the values -->
<cms:each efrom >
    <cms:show item />
</cms:each>

Hope this helps.
Hey Thanks,

I understand when using foreach the last passed variable will only be the result in this case but it didn't !?

I know I'm going beyond the scope of couch when I use the <cms:php> tag and even more so beyond my PHP capabilities .
The homepage clearly says
No knowledge of PHP required at all.
It should also say:
Couch is so flexible and easy to use ... before you know it you try to do things that's out of your league. :lol: (at least in my case) :lol:

Anyhow will look at your code on Monday and see what I can make of it.

Again many thanks for your explanation and for Couch.
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
5 posts Page 1 of 1
cron