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 9032429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:58:49+00:00 2026-06-16T07:58:49+00:00

I’m new to CodeIgniter and I have this function which I use in more

  • 0

I’m new to CodeIgniter and I have this function which I use in more than one model.

// Returns TRUE if the array of values supplied
// each correspond to a column in the database table.
// Else returns the value of the bad column.
private function _columns_exist( $columns, $table ) {

    foreach( $columns as $key => $value ) {

        if( !$this->db->field_exists( $key, $table ) ) {

            return $key;

        }

    }

    return TRUE;

}

What is considered the correct way to write the function once, and reuse it from many different files?

Do I want to create a whole library just for this one function?

  • 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-16T07:58:50+00:00Added an answer on June 16, 2026 at 7:58 am

    The point of CodeIgniter is that there are basically two types of custom functions. Ones that live in Libraries as library methods and ones that live in Helpers as simple functions.

    The difference between them is minimal and it is for you to decide where to keep them, but the best practice is that helper functions are mostly standalone and library methods are the ones that use CI libraries like database, sessions, your other models.

    For your function it’s best to make a library and keep it there, but you can, as well, make a helper as you’re not overusing any database querying. If it was me, I would make a helper:

    Make a file in the helpers directory named db_helper.php and add an element db to helper array in autoload.php file. Note that you don’t need to write the full name db_helper, only db.

    Put this code in db_helper.php file you’ve created:

    <?php
    if(!function_exists('_columns_exist')) {
        function _columns_exist( $columns, $table ) {
            $CI =& get_instance();
            foreach( $columns as $key => $value ) {
                if( !$CI->db->field_exists( $key, $table ) ) {
                    return $key;
                }
            }
            return TRUE;
        }
    }
    

    Now you can use this function anywhere – in your controllers, models, even view files as you would if it was a simple function:

    _columns_exist($columns, $table)
    

    Note that you don’t use $this in helpers or libraries, you must get an instance of CodeIgniter singleton object first, then you manipulate it. You can find more info on this in the user guide or on StackOverflow.

    Also see my comments to darkdad‘s answer, that is another great approach I would go for.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an array which has BIG numbers and small numbers in it. I
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary

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.