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.

Custom query usage in template - best practices

February 21, 2012 6:18pm

Subscribe [2]
  • #1 / Feb 21, 2012 6:18pm

    susanfw

    62 posts

    I am new to custom queries, and have a best practices question.

    I have a custom query that is checking the logged in user to see if he purchased a Course we are offering using Simple Commerce. If he has, then the page content is displayed. If not, he is automatically redirected to another page. So far, it’s working fine.

    What I’d like to know is the best practice for using this query in my templates. The only thing I can think of is to put the query into every template of every course. Are there any other alternatives? Can I, or is there any advantage, or disadvantage to putting the first part (before the page content) in a snippet or embed?

    And one more question. If I use this query, I don’t think I also need to use the Access options under the templates, since I will now be restricting by member_id and purchase, instead of member group. Does that sound correct?

    Thank you
    Susan

    {html-header}
    </head>
    {exp:query sql="SELECT * FROM exp_simple_commerce_purchases 
                    JOIN exp_simple_commerce_items ON exp_simple_commerce_items.item_id = exp_simple_commerce_purchases.item_id
                    JOIN exp_channel_data ON  exp_simple_commerce_items.entry_id = exp_channel_data.entry_id
      WHERE exp_simple_commerce_purchases.member_id = '<?php echo $this->EE->session->userdata['member_id'];?>'"}
                    
      {if item_id == 4}
    <body>
    page content is displayed
     {if:else}
       {redirect="anotherpage/"}
        {/if}
    {/exp:query} 
    {embed="olm_embeds/footer"}
    </body>
    </html>
  • #2 / Feb 21, 2012 9:29pm

    Bhashkar Yadav

    727 posts

    Hi Susan,

    1) you should embed the query part only. in this way you can pass whatever values to embeded template or query. it would be a better option.

    2) it’s good practice to set access option for all the dynamic templates.


    Best Regards,
    Bhashkar

  • #3 / Feb 24, 2012 11:16am

    susanfw

    62 posts

    Thanks Bhaskar for your advice!
    Susan

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

ExpressionEngine News!

#eecms, #events, #releases