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.

Use a PHP array with the query module?

November 19, 2007 3:03am

Subscribe [3]
  • #1 / Nov 19, 2007 3:03am

    Erin Dalzell

    790 posts

    Here’s what I want to do:

    * query the gallery to get the entry id’s for all the entries in a particular category
    * store those in an array
    * based on the URL segment (in the pagination), pull out an entry id via the index

    So my thought was to combine the EE Query module along with some PHP for the array.

    How do I have to set my PHP processing to make this work? Input or Output?

    Thanks

  • #2 / Nov 19, 2007 1:21pm

    Brian M.

    529 posts

    If you’re using PHP to query the gallery and store that info, you would put it on input because you’ll want to use that info inside an EE tag itself…  Should work, but one thing to keep in mind is the parse order, which can come back to bite you in some situations.  Here’s an article about input/output PHP parsing:

    http://expressionengine.com/knowledge_base/article/whats_the_difference_between_input_and_output_for_php_parsing/

    And this list here is the bible for parse order:

    http://expressionengine.com/wiki/Parse_Order/

  • #3 / Nov 19, 2007 1:35pm

    Erin Dalzell

    790 posts

    Here’s what I have so far, and it seems to be working:

    <?php $entry_ids = array(); ?>
        {exp:query sql="SELECT entry_id FROM exp_gallery_entries WHERE cat_id = 2"}
            
          <?php array_push($entry_ids, {entry_id}); ?>
            
        {/exp:query}
        
        <?php print_r(array_values($entry_ids)); ?>
  • #4 / Nov 19, 2007 1:47pm

    Brian M.

    529 posts

    Do you have it on output currently?

    If you’re going to use $entry_ids in another EE tag after this code it won’t work because it has parsed all the EE tags already. One thing you can do as well is just do a global $DB, and use the $DB class directly.  That way you can get around some parse order issues you might run into.

    If it’s working for you though, you can ignore everything I’ve said 😉

  • #5 / Nov 19, 2007 2:21pm

    Erin Dalzell

    790 posts

    I was going to use $entry_ids later yes, but I haven’t yet.

    I have PHP processing set to output currently.

    I was looking at the DB class last night thinking that that might be a better solution.

    Ideally, I will get this working and hopefully someone else can make it a plugin or something.

  • #6 / Nov 19, 2007 2:52pm

    Erin Dalzell

    790 posts

    Can I use the $DB class in a templat?

    How do I start?

  • #7 / Nov 19, 2007 2:55pm

    Erin Dalzell

    790 posts

    All I really need is the entry_ids in an array so I can reference them later.

    Can I do in EE directly some how?

  • #8 / Nov 19, 2007 3:08pm

    Brian M.

    529 posts

    What is it you’re trying to do, rather than how you are trying to do it?

    To use the $DB class all you need to do it put this:
    <?php
    global $DB;
    ?>

    In your template.  You’ll want PHP parsing on input for what you’re trying to do.

    There is development documentation here.

    And $DB class specific info here.

  • #9 / Nov 19, 2007 3:19pm

    Erin Dalzell

    790 posts

    What is it you’re trying to do, rather than how you are trying to do it?

    In my new gallery, I have the first image automatically loaded. The problem lies when you switch to the second (or third) page. I would like the correct image (the first one on the *page*)  loaded.

    EE won’t let me get an image by index out of its query, so to get around that, I want to get the list of entry ids (which I can do), store in an array (which I can do), then parse the last segment (P2, P3), do some calcuations to figure the correct index, then load the correct image.

    If you have any suggestions as to how else I can do this, I would really appreciate hearing them.

  • #10 / Nov 19, 2007 3:49pm

    Erin Dalzell

    790 posts

    OK, I am almost there…I have all the pieces, I just need to put them together. Here’s what my pseudo-code looks like:

    First, get the category id and page:

    $cat_id_array = explode("C", "{segment_3}");
    $cat_id = cat_id_array[1];
    
    $page_array = explode("P", "{segment_3}");
    $page = page_array[1];

    Then get the entry ids:

    $query = $DB->query("SELECT entry_id FROM exp_gallery_entries WHERE cat_id = $cat_id");
    
        if ($query->num_rows > 0)
        {
            foreach($query->result as $row)
            {
                array_push($entry_ids, {entry_id});
            }
        }

    Then do the calculation to get the correct index and get the correct image URL:

    $index = ($page - 1) * images_per_page;
       $query = $DB->query("SELECT filename, extenstion FROM exp_gallery_entries WHERE entry_id = $entry_ids[$index]");
       $img_url = "{site_url}/{gallery_name}/{category_name}/{filename}.{extension}

    And that should do it, I think. Off to try.

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

ExpressionEngine News!

#eecms, #events, #releases