We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Problems with parse order

Development and Programming

superbiaweb's avatar
superbiaweb
26 posts
15 years ago
superbiaweb's avatar superbiaweb

Hi,

I’ve written a custom query and php to improve performance on an overview page that pulls in an image from the first matrix row for a series of posts. This cuts the growing number of database queries on the page – almost 100 at the moment – to a fixed total of 31.

However I’m struggling with the parse order. I’m currently storing the image path in an array that uses the post’s entry_id as the index. I was then hoping to use this when outputting content in the exp:channel:entries tag. My code is below:

<?php

  $EE =& get_instance();
  
  $sql = "SELECT entry_id, col_id_1 AS image FROM exp_matrix_data WHERE row_order = 0";
  $query = $EE->db->query($sql);
  
  $images = array();
  foreach ($query->result_array() as $row) {
    $images[$row['entry_id']] = str_replace('https://ellislab.com/asset/images/team-photo/', '/images/projects/', $row['image']);
  }
  
?>

{exp:channel:entries channel="projects" dynamic="off" status="open" orderby="project_order" sort="asc" disable="member_data|category_fields|pagination"}
  {exp:imgsizer:size image="<?php echo $images['{entry_id}'] ?>" width="320" alt="" quality="90"}
{/exp:channel:entries}

I’ve set the template to parse PHP on input. The $images[{entry_id}] in the imgsizer tag results in an undefined index error. From what I’ve read this is because php is being parsed before the expression engine tags. The frustrating thing is that I can echo out the {entry_id} but can’t use it to access an array index.

I managed to get this to work when I put the imgsizer tag in an embed and passed in the entry_id but the added overhead of using embeds kind of defeats the point of doing this in the first place.

Is there a way to access the entry_id directly… Or can someone suggest an alternative solution. Any help would be really appreciated.

Thanks,

Dylan

       
ender's avatar
ender
1,644 posts
15 years ago
ender's avatar ender

you could probably use the string plugin to be the middleman between PHP variables and EE tags.

leaving your PHP database query in input mode, you’d need to assign the array entries to

$EE->session->cache['string']['image_' . $row['entry_id']]

and then within the imgsizer tag you’d be able to access this data by using

{exp:string:output}{image_{entry_id}}{/exp:string:output}
       
the3mus1can's avatar
the3mus1can
426 posts
15 years ago
the3mus1can's avatar the3mus1can

Get ride of the channel entries loop and just do it in PHP.

<?php 


    $EE =& get_instance();
    
    $sql = "SELECT entry_id, col_id_1 AS image FROM exp_matrix_data WHERE row_order = 0";
    $query = $EE->db->query($sql);

    $images = array();
    foreach ($query->result_array() as $row) {
        $images[$row['entry_id']] = str_replace('https://ellislab.com/asset/images/team-photo/', '/images/projects/', $row['image']);
    }
    
    //{{field_id_x}} = what ever column in the exp_channel_data the project_over field is.
    //{{channel_id}} = the channel id for projects 

    $entries_sql = "SELECT DISTINCT(ct.entry_id) FROM exp_channel_titles as ct JOIN exp_channel_data as cd ON ct.entry_id = cd.entry_id WHERE ct.channel_id = {{channel_id}} AND status='open' ORDER BY {{cd.field_id_x}} ASC";
    
    $entries_query = $EE->db->query($entries_sql);

?>

<?php foreach ($entries_query->result_array() as $row): ?>
    {exp:imgsizer:size image="<?php echo $images[$row['entry_id']] ?>" width="320" alt="" quality="90"}
<?php endforeach; ?>

Disclaimer: Not tested!

       
superbiaweb's avatar
superbiaweb
26 posts
15 years ago
superbiaweb's avatar superbiaweb

Thanks ender and the3mus1can. Both look like good approaches. I was trying to avoid going full PHP as I’ve got categories and other fields to output but I think I’ll have a go. I’ll post back when I get a chance to look at this again.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.