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.

Loop for Query Module

April 23, 2012 6:17pm

Subscribe [3]
  • #1 / Apr 23, 2012 6:17pm

    fedejp

    3 posts

    Hi, this is my first question here, so I hope I’m not posting this in the wrong place.

    My problem is the following:
    I’m using the Query Module to get information based on the year of that information. What I wanna do is wrap and print this information based on the year. In PHP I’d do this by using a loop. Example:

    <?php
    for ($count=$firstyear; $count<=$lastyear; $count++)
        query="select * from mytable where year=$count";
    ?>

    That way I don’t have to write each query. The loop takes care of that.
    So, is there any way of doing this with the Query Module?
    Once again, I really hope this is the right place to post this question. As I said, I’m new on the forum.

    Thanks in advance!

  • #2 / Apr 27, 2012 9:14am

    Man With A Peg

    124 posts

    When I come to these sorts of situations, I tend to alternate between full PHP and EE tags + PHP. You really have to look at how much other markup is going to exist in order to determine which approach is cleaner and/or faster.

    The closest to straight EE markup is something along these lines (with PHP enabled on output):

    <?php $year = 0; ?>
    {exp:query sql="SELECT * FROM mytable WHERE year >= 2000 AND year <= 2010 ORDER BY year DESC"}
    <?php if ($year != {year}) {
        $year = {year}; ?>
        {!-- All the stuff to print for the new year --}
    <?php } ?>
    
    {month} {day}, {year}: {title}
    
    {/exp:query}

    ... unless you write a plugin to take care of this for you.

  • #3 / Jul 17, 2012 10:31am

    vladyn

    176 posts

    How can I do a break in this loop when the returned result (username in my case) meets certain criteria like pre defined variable?
    Is there a way to break the query loop or ot should be done with mix of php?

  • #4 / Jul 17, 2012 11:01pm

    Man With A Peg

    124 posts

    Hey vladyn,

    Are you looking for all of the names up to a certain one, or are you only looking for one specific name?

  • #5 / Jul 18, 2012 5:19am

    vladyn

    176 posts

    I’m have to pull out all the names from a certain group and then do a matching against already inputed name by the user and submit the form based on that match

  • #6 / Jul 18, 2012 11:24am

    Man With A Peg

    124 posts

    Hey vladyn,

    If it is possible, I would recommend that you use MySQL for the pattern matching on the user’s name (make sure that you escape anything that the user has entered, perhaps with my Escape Plug-in). I just finished rewriting a project where someone thought it was a good idea to pull 2000+ entries from a database and then use that data to perform another lookup that cached to the DB from within PHP during an AJAX request. I am not saying that your situation is quite this bad, but that request went from 40+ seconds down to 2 with a simple rewrite of their initial SQL statement and cutting out the subsequent PHP processing.

    Since you asked though, the only method to break out of this loop is to set a flag when you find the item you wanted. For instance:

    <?php $found = false;
    $email = '';
    {exp:query sql="..."}
    <?php if (!$found && (strpos("{email}", 'epicvoyage') !== false)) {
        $found = true;
        $email = "{email}";
    } ?>
    {/exp:query}
    <?php if ($found): ?>
    ...
    </php endif; ?>

    Keep in mind that PHP is being executed on OUTPUT for this code snippet though, so if you are allowing EE to encode the value (as part of a hidden form field) then this will not work.

    You could also read up on ActiveRecord usage within EE (or CodeIgniter) and write this code in PHP on INPUT. This method would allow you to use the full power of PHP.

  • #7 / Jul 19, 2012 11:53am

    vladyn

    176 posts

    Many thanks EpicVoyage,
    I’ll play with this code snipped
    and already started reading the Active Record Class Documentation for further cases like this.
    Escape plug-in is really useful in this case!

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

ExpressionEngine News!

#eecms, #events, #releases