Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6011033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:12:04+00:00 2026-05-23T02:12:04+00:00

I have an app that uses the codeigniter CXTags tagging library. The database structure

  • 0

I have an app that uses the codeigniter CXTags tagging library.

The database structure is as follows:

posts

id

tags_ref

row_id

table

tag_id

tags

id

safe_tag

tag

My query basically goes if $safe_tag is not null then join tags_ref on post.id = tags_ref.row_id, join tags on tags_ref.tag_id = tags.id, where tags_ref.table = ‘posts’ and tags.safe_tag = ‘food’

SELECT * FROM posts 
JOIN tags_ref ON posts.id = tags_ref.row_id
JOIN tags ON tags_ref.tag_id = tags.id
WHERE tags.safe_tag = $safe_id

Unfortunately the query I’ve written in active record is not functioning properly. The query works perfectly when £safe_tag is null but when it’s not I get wrong results.

function get_posts($id = NULL, $safe_tag = NULL) {

    if($safe_tag != NULL){
        echo $safe_tag;//debugging
        $table = 'posts';
        $this->db->join('tags_ref', 'posts.id = tags_ref.row_id');
        $this->db->join('tags', 'tags_ref.tag_id = tags.id');
        $this->db->where('tags_ref.table', $table);
        $this->db->where('tags.safe_tag',$safe_tag);
    }

    //if an id was supplied
    if ( $id != NULL ) {
        $this->db->where('posts.city_id',$id);
    }

    // execute query
    $query = $this->db->get('posts');
    ...

Here is the query with profiling on:

SELECT *
FROM (`posts`)
INNER JOIN `tags_ref` ON `posts`.`id` = `tags_ref`.`row_id`
INNER JOIN `tags` ON `tags_ref`.`tag_id` = `tags`.`id`
WHERE `tags_ref`.`table` = 'posts'
AND `tags`.`safe_tag` = 'food'
AND `posts`.`city_id` = '2' 

Can someone have a look? I think I need a fresh set of eyes on it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:12:05+00:00Added an answer on May 23, 2026 at 2:12 am

    Your forgot to actually run the query inside your first if{}

    if($safe_tag != NULL){
            echo $safe_tag;//debugging
            $table = 'posts';
            $this->db->join('tags_ref', 'posts.id = tags_ref.row_id');
            $this->db->join('tags', 'tags_ref.tag_id = tags.id');
            $this->db->where('tags_ref.table', $table);
            $this->db->where('tags.safe_tag',$safe_tag);
            $this->db->get(); // here 
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple app that uses an SQL Express 2005 database. When the
I have an app that uses a database. At startup I check to see
I have an app that uses a SQLite database. When the app updates at
I have an app that uses an sqlite database. Currently I have a database
I have an app that uses this library (actually a direct port to D
I have an app that uses the iPod Library API to access the song
I have an app that uses an SQLite database. With the advent of iCloud
We have an app that uses simple one way binding with a GridView to
I have an app that uses EF. To test my stuff I generally wrap
I have an app that uses the python/c api and I was wondering what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.