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

Need help updating db query for v3

How Do I?

Linda A's avatar
Linda A
647 posts
8 years ago
Linda A's avatar Linda A

I have a template which uses some php and a db query to fetch the contents. I had help putting it together in the first place and now it no longer works in v3, so I am not sure how to update it.

<?php

global $DB;
global $IN;

$results = $this->EE->db->query("SELECT * FROM exp_channel_data WHERE channel_id = '32' AND field_id_141 LIKE '" . $this->EE->db->escape_str($this->EE->uri->segment(3)) . "%'");
    
$entries = array();
    
if($results->num_rows > 0)
{
foreach($results->result() as $row)
{
$entries[] = $row->entry_id;
}
        
$results_returned = true;
}
else
{

$results_returned = false;
}

?>

<title>{exp:channel:entries channel="{current_channel}" limit="1" orderby="{current_channel_id}_number_sorting" sort="asc" dynamic="off" <?= (($results_returned == true) ? 'entry_id="' . join('|', $entries) . '"' : ''); ?>}{channel} - {{current_channel_id}_number_display} {title}{/exp:channel:entries}</title>
<?php

global $DB;
global $IN;

$results = $this->EE->db->query("SELECT * FROM exp_channel_data WHERE channel_id = '32' AND field_id_141 LIKE '" . $this->EE->db->escape_str($this->EE->uri->segment(3)) . "%'");
    
$entries = array();
    
if($results->num_rows > 0)
{
foreach($results->result() as $row)
{
$entries[] = $row->entry_id;
}
        
$results_returned = true;
}
else
{

$results_returned = false;
}

?>

{exp:channel:entries channel="{current_channel}" orderby="{current_channel_id}_number_sorting" sort="asc" dynamic="off" <?= (($results_returned == true) ? 'entry_id="' . join('|', $entries) . '"' : ''); ?>}
       
Derek Jones's avatar
Derek Jones
7,561 posts
8 years ago
Derek Jones's avatar Derek Jones

What type of field is field_id_141? This looks like code that originally started on v1 before you could filter channel entries tags by field content.

ref: search://field_name= parameter

I think you can get rid of this PHP rather than trying to debug it.

       
Linda A's avatar
Linda A
647 posts
8 years ago
Linda A's avatar Linda A

This particular channel is a kind of encyclopaedia, arranged according to specific topics with sub topics four layers deep. The main page still works and shows the layout: http://www.westeros.org/Citadel/Concordance/

The Section template allows someone to click anywhere on the main index and get the content belonging to the level chosen and all levels below. For example, this is one part of the tree:

  1. The Iron Islands 4.1. Geography 4.1.1. Trade and Resources 4.2. The Greyjoys 4.2.1. Pyke 4.2.2. Ancestors and History 4.2.3. Bannerhouses 4.2.3.1. The Botleys of Lordsport 4.2.3.2. The Goodbrothers of Great Wyk, Old Wyk, and Orkmont 4.2.3.3. The Harlaws of Harlaw

If you click on 4. Iron Islands, the Section template will show everything that matches 4.(x). If you click on 4.2.1. Pyke, it will show everything that matches 4.2.1.(x).

I have no idea if this can be done without the php, since the matching is pretty specific.

       
Derek Jones's avatar
Derek Jones
7,561 posts
8 years ago
Derek Jones's avatar Derek Jones

Your matching is just comparing the start of the field with the 3rd segment, but looks like with your current content design, you do need the “starts with” matching which you currently can’t do with the tag parameter. Long term—and this is probably a large project considering the scope of your content—you would be best served by using relationships for section.subsection.article schema, rather than storing string partials in a field and matching based on strings.

Second best would be converting this PHP to a plugin, which would not be difficult. For a quick fix though, replace all of the PHP above with:

<?php
$query = ee()->db->select('entry_id')
 ->where('channel_id', 32)
 ->like('field_id_141', ee()->uri->segment(3), 'after')
 ->get('channel_data');

$entry_ids = '';

if ($query->num_rows() > 0)
{
 foreach ($query->result() as $row)
 {
  $entry_ids .= $row->entry_id.'|';
 }
}

?>

{exp:channel:entries channel="{current_channel}"
 orderby="{current_channel_id}_number_sorting"
 sort="asc" dynamic="off"
 entry_id="<?=trim($entry_ids, '|')?>"
}

The title code block would use the same PHP, but:

<title>
{exp:channel:entries channel="{current_channel}"
 orderby="{current_channel_id}_number_sorting"
 sort="asc" dynamic="off"
 entry_id="<?=trim($entry_ids, '|')?>"
}
 {channel} - {{current_channel_id}_number_display} {title}
{/exp:channel:entries}
</title>

The PHP would not change for use in a plugin, but you’d instead just return the trimmed text, e.g.:

{exp:channel:entries channel="{current_channel}"
 orderby="{current_channel_id}_number_sorting"
 sort="asc" dynamic="off"
 entry_id="{exp:my_plugin}"
}
       
Linda A's avatar
Linda A
647 posts
8 years ago
Linda A's avatar Linda A

Thank you so much, that (mostly) did the trick. The only issue that I see now is that the title ends up containing all entry titles that it matches, as opposed to just the highest level. limit=”1” was taking care of that before but does not seem to work now.

I will definitely look at making it into a plugin. Never made one, but sometime has to be the first I suppose. 😊 Relationships sound like an intriguing approach, though I cannot quite picture how to get this fine-grained control with it. And, as you say, it is a massive project.

       
Derek Jones's avatar
Derek Jones
7,561 posts
8 years ago
Derek Jones's avatar Derek Jones

Ah sorry, I left that out when I cut and pasted. You can use whatever order/sort/limit parameters you need on the channel entries tag. You might also require_entry= so that you can have proper 404 behavior instead of empty pages.

       
Linda A's avatar
Linda A
647 posts
8 years ago
Linda A's avatar Linda A

I did add the limit parameter back in, setting it to 1. But it doesn’t seem to work on the title now. See for example http://www.westeros.org/Citadel/Concordance/Section/4.2./

       
Derek Jones's avatar
Derek Jones
7,561 posts
8 years ago
Derek Jones's avatar Derek Jones

It looks like it’s working to me, but maybe I am not understanding your expectation. On that page the page title is The Citadel: Concordance - 4.2. The Greyjoys, is that incorrect?

       
Linda A's avatar
Linda A
647 posts
8 years ago
Linda A's avatar Linda A

Oh, darn, looks like we ran into that other issue we’ve had, with caches not clearing, because I kept getting the old version. But thank you, that sorts out the template display at least. 😊

       
Derek Jones's avatar
Derek Jones
7,561 posts
8 years ago
Derek Jones's avatar Derek Jones

I seem to recall you having issues with memcached/redis config, so I’m not sure it’s related, but feel free to open a ticket if you need help identifying the caching issue!

       
Linda A's avatar
Linda A
647 posts
8 years ago
Linda A's avatar Linda A

Yes, we’re on redis now but still seem to have issues. Will see if I can reproduce again and open a ticket. We may be trying to upgrade to php 7 this weekend, however, so might wait and see if that changes anything. 😊

       

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.