ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

[SOLVED] Getting PHP to play nice with EEPoll

October 16, 2007 12:30pm

Subscribe [2]
  • #1 / Oct 16, 2007 12:30pm

    APGWest

    295 posts

    This might belong in another forum so mods feel free to move it if need be.

    What I’m trying to do is list all previous polls via this code:

    {exp:query sql="SELECT MAX(poll_id) AS 'maxPoll' FROM exp_eepoll"}    
        <?php                            
        $curPoll = {maxPoll};
        echo $curPoll;
        while ($curPoll > 0) {
            echo "{exp:eepoll poll_id=\"$curPoll\"}{poll}{/exp:eepoll}";
            $curPoll = $curPoll - 1;
        }                        
        ?>
    {/exp:query}

    I have PHP turned on for the template and set to output (I’ve tried with both input and output by the way).  All I get is an echo of “75” which is the number of polls we have but no polls show up.  If I replace

    $curPoll = {maxPoll};

    with

    $curPoll = 75;

    I get the same echoed “75” but all the polls show up.  I’m confused because it’s echoing the proper number of polls but somehow it’s not translating down into the while loop.  Any thoughts?

  • #2 / Oct 17, 2007 12:23pm

    APGWest

    295 posts

    Hmmm.  I used to be impressed by the amount of support that was offered around here…

  • #3 / Oct 17, 2007 5:29pm

    BlackHelix

    226 posts

    I’d try fixing your echo statement in your while loop.  I bet something funky with the string is happening.  Try this:

    echo '{exp:eepoll poll_id="' . $curPoll . '"}{poll}{/exp:eepoll}';

    All I did was change the quote style from double quotes to single quotes.  See if that works.

  • #4 / Oct 17, 2007 5:41pm

    Lisa Wess

    20502 posts

    Thank you to Vanceone for responding.

    svh1- I’m sorry that you’re not getting the responses - but you’re asking for help from two limited communities - the one that knows PHP and Queries, and the other that knows EEPoll - and those may not even cross-over, and you’re asking for help with a fairly advanced customization. 

    The community here really does try to help, but in some cases, the answer may just not be known.

  • #5 / Oct 17, 2007 5:55pm

    BlackHelix

    226 posts

    Now that I look at that, it IS a bit funky syntax.  if you are using php anyway, why use the query module?  Just use the DB class directly.  😊  Less chance of a possible translation…. after all, setting $cur_poll to {max_poll} is setting it to a string, most likely.  That would be my next guess…. sorry I don’t know EEpoll at all.  I’m just coming from a mysql and php pov.

  • #6 / Oct 17, 2007 6:00pm

    APGWest

    295 posts

    Care to give an example of the DB class, Vanceone?  Thanks for the help by the way!

    Lisa: Thanks for the reply.  I never thought I would get to the point where someone told me that I was doing something “fairly advanced”.  Proof that EE kicks much butt!!

  • #7 / Oct 17, 2007 6:21pm

    BlackHelix

    226 posts

    An example, eh?  Lessee….  Replace your entire code with this:

    <?php
    global $DB;
    $query = $DB->query("SELECT MAX(poll_id) AS 'maxPoll' FROM exp_eepoll");
    if ($query->num_rows > 0)
        {
            $currPoll = $query->row['maxPoll'];
        } 
    else
        {
         $currPoll = 0; // this sets it to zero if there are no polls--so nothing is displayed
        }
    
    echo $currPoll; // This is a test to see if we got it right.  Should kick out at the moment 75.
    while ($currPoll > 0) 
           {
               echo '{exp:eepoll poll_id="' . $currPoll . '"}{poll}{/exp:eepoll}'; 
               $currPoll = $currPoll - 1;
           }  
    ?>

    Set php on input. 

    I figured out what was going on with your original code—you ran into a loop that never ends, and php timed out or shorted it out at some point.

    What happened is that when you dropped into PHP, you never got the value 75 into the php code.  What you did was set the currPoll variable to a string, not an integer.  currPoll was set to the string “{maxPoll}”—including the braces.  It was then echoed to the rest of the screen.  I’ll bet that with php on input, it translated it into 75—because EE saw the php output, which was “{maxPoll}” and inside the query module tag, it saw that as a variable, so it replaced it with the value of that variable, 75.  But php never got the integer.  So inside your while loop, it never had a number to subtract, only the string “{maxPoll}”  And subtracting one from that is rather interesting, to say the least.  It shows that when you hard coded an integer to the variable currPoll, it works—so you didn’t have a valid integer. 

    My code above will do what you want, and it should work.  The only thing left is that you don’t have any html coding—no paragraph tags, etc.  So change the echo inside the while loop.  (BTW, when you take it live, get rid of the echo currPoll line above the while loop… it doesn’t do any good—just debugging code.  )

    You can do something like this:

    ...
    echo '
    {exp:eepoll poll_id="' . $currPoll . '"}{poll}{/exp:eepoll}</br>';

    or other such like code. 

    Hope that helps!

  • #8 / Oct 17, 2007 8:25pm

    APGWest

    295 posts

    I noticed there was some syntax issues in your suggested code where there were some $curPoll and some $currPoll, but I corrected them.  Thanks for the help, however it’s spitting this out:

    Description: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '. $currPoll . ''' at line 2
    
    Query: SELECT * FROM exp_eepoll WHERE poll_id = '' . $currPoll . ''

    Looks to be a syntax problem maybe?  I tried it with ‘\’ in front of the double quotes but than it shows nothing at all again.

  • #9 / Oct 17, 2007 8:34pm

    BlackHelix

    226 posts

    Oh.  It shouldn’t be running any query off of that line.  When I said replace your code, I meant, take out the exp:query tag call, too.  Thanks for correcting my curPoll errors, too. 

    That is a REALLY funky sql call.  And shouldn’t exist.

  • #10 / Oct 17, 2007 8:41pm

    APGWest

    295 posts

    You know, it helps if I read your instructions more carefully.  I forgot to set PHP to input.  I did that and it’s working like a champ!  Thanks so much for your time and energy, Vanceone!

  • #11 / Oct 17, 2007 8:45pm

    BlackHelix

    226 posts

    Glad it’s working for you!  And my pleasure!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases