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

  • SEARCH
  • Home
  • 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 8014191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:51:25+00:00 2026-06-04T19:51:25+00:00

One problem that I am facing is having many queries with similar select statements

  • 0

One problem that I am facing is having many queries with similar select statements but different join/where statements.

Below is an example of a code that I am working on via CodeIgniter. What I normally do is make one function, get(), that accepts an array of random keys/values. Depending on what keys/values are passed, it will generate and run the appropriate query. Now, I am wondering is this a good way of doing things? Because as you can see, this function becomes more and more complex. Initially, I had a bunch of functions such as get_all(), get_only_lessons(), etc but it becomes kinda annoying having to repeat the same set of code with one or two lines that are different.

My question is what is the best way with dealing with this problem.

function get($param = NULL)
{
    /*
    SELECT  m.id AS id, CAST(m.order_number AS SIGNED) AS order_number, m.name AS name, m.permalink as permalink, 
        m.suplesson_id as suplesson_id, CAST(sm.order_number AS SIGNED) AS suplesson_order_number
    FROM    lessons m
    JOIN    courses c ON m.course_id = c.id
    LEFT JOIN lessons sm ON m.suplesson_id = sm.id
    WHERE   [various]
    */

    $select =   'm.id AS id, CAST(m.order_number AS SIGNED) AS order_number, m.name AS name, m.permalink as permalink, ';
    $select .= ' m.suplesson_id as suplesson_id';

    if (isset($param['id']) || isset($param['suplesson_order_number']) || isset($param['permalink']))   
        $select .= ', CAST(sm.order_number AS SIGNED) AS suplesson_order_number ';

    $this->db->select($select);
    $this->db->from($this->table_name.' m');
    $this->db->join($this->courses_table_name.' c', 'm.course_id = c.id');

    if (isset($param['id']) || isset($param['suplesson_order_number']) || isset($param['permalink']))
        $this->db->join($this->table_name.' sm', 'm.suplesson_id = sm.id', 'left');

    // where clauses
    if (isset($param['course_id'])) 
        $this->db->where(array('c.id' => $param['course_id']));
    if (isset($param['id'])) 
        $this->db->where(array('m.id' => $param['id']));
    if (isset($param['order_number'])) 
        $this->db->where(array('m.order_number' => $param['order_number']));
    if (isset($param['permalink'])) 
        $this->db->like('m.permalink', $param['permalink'], 'none');
    if (isset($param['suplesson_id'])) 
        $this->db->where(array('m.suplesson_id' => $param['suplesson_id']));
    if (isset($param['suplesson_order_number'])) 
        $this->db->where(array('sm.order_number' => $param['suplesson_order_number']));
    if (isset($param['NULL'])) 
        $this->db->where('m.'.$param['NULL'].' IS NULL');
    if (isset($param['NOT NULL'])) 
        $this->db->where('m.'.$param['NOT NULL'].' IS NOT NULL');

    $this->db->order_by('order_number');

    // filter based on num_rows/offset
    if (isset($param['id']) || isset($param['permalink']))  
        $this->db->limit(1);
    if (isset($param['num_rows']) && isset($param['offset'])) 
        $this->db->limit($param['num_rows'], $param['offset']);

    $query = $this->db->get();

    // return row if expecting 1 result 
    if (isset($param['id']) || isset($param['suplesson_order_number']) || isset($param['permalink']))   
        return ($query->num_rows() == 1) ? $query->row_array() : NULL;

    return ($query->num_rows() > 0) ? $query->result_array() : NULL;
}
  • 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-06-04T19:51:26+00:00Added an answer on June 4, 2026 at 7:51 pm

    The usual way of running DB queries is to structure your model code to have multiple function calls, each one relating to one SQL statement, for example:

    function get_user($userId)
    {
        $this->db->get_where('user', array('userId' => $userId))
        //...
        //...        
    }
    
    function delete_user($userId)
    {
        $this->db->delete('user',array('userId' => $userId))
    }
    

    You might create a model class called User_model which contains all of the functions you need for reading/updating the user table, so in your controller you call down to the specific model function

    $user = $this->User_model->get_user($userId)
    

    It looks like you’re trying to construct one giant model function which checks various parameters in order to determine what SQL statement to run. This isn’t good design and doesn’t fit well with Codeignitors MVC model.
    Instead, create a separate model for each table, then in each model create separate functions for each SQL operation you wish to run. Call these models from your controllers to get/update/delete data in your tables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been optimizing my website but the one problem that stands in my
I'm almost finished with my program, but I have one problem that I can't
I have made a swings application but there is one problem in that which
I am trying to include different pages on one page. However the problem that
My problem is one that you would think is quite common, but I haven't
We link our app with numerous different static libs, the problem is that one
Evening, I'm facing a small problem which I can't get fixed. I'm having one
I am facing a problem that one of my fields need to be shown
I am facing one problem from last one week. I have one list having
all I am facing one problem the problem is that I want to hit

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.