-
Notifications
You must be signed in to change notification settings - Fork 0
[b][color=green]Table of Contents[/color][/b] [color=grey][i](Absent an automagic ToC feature in the CI-wiki, this is here for convenience only. Do NOT assume all the FAQs on this page are listed in this table of contents -- please read through this whole page, just in case. Ctrl-F is your friend ;)[/i][/color]
[b]Database[/b]
- [i]Be sure to also check the [url="/wiki/SQL"]SQL FAQ page[/url].[/i]
- [i]How do I see the actual raw SQL query generated by CI's Active Record[/i]
- [i]How do I find out the ID of the row I just inserted?[/i]
- [i]How do I do a COUNT('foo') using the Active Record functions?[/i]
- [i]Can I extend the core Database class?[/i]
- [i]Oracle does weird things[/i]
[b]Design[/b]
- [i]I keep repeating lumps of code - things like login-dialogs, stylesheets, headers, footers, or menus - is there some place I can define them just once?[/i]
- [i]How do I embed views within views? Nested templates? Header, Main, Footer (Blocks) design? Partials? Page Fragments? Ruby on Rails style Yield functionality?[/i]
- [i]How do I pass parameters to a controller's index() function?[/i]
- [i]How do I call one Controller's methods from a different Controller?[/i]
[b]Errors[/b]
- [i]Headers already sent[/i]
[b]Forms and Input and POST data[/b]
- [i]Is there a way to get [/i]all[i] the $this->input->post() items?[/i]
[b]Images[/b]
- [i]When I load an image in my code, it doesn't appear on the rendered page?[/i]
[b]JavaScript and AJAX and JS libraries[/b]
- [i]I'm confused[/i]
[b]Site migrations, relocations and platform differences[/b]
- [i]After moving from development to production, my site stopped working[/i]
- [i]How do I migrate my existing 'normal' PHP site to CodeIgniter?[/i]
[b]CodeIgniter and EllisLab and ExpressionEngine[/b]
- [i]When is the next version of CodeIgniter coming out? What can I expect from the next version of CodeIgniter?[/i]
- [i]I've got cool new code for CI. What is the process for getting it included in the official CI distribution?[/i]
[b]Plain old frequently asked questions[/b]
- [i]How do I find the name of the current controller and/or method?[/i]
- [i]Can I cache only certain parts of a page? [/i]
- [i]How do I call methods in one controller via another controller?[/i]
[h2]Database[/h2]
[i]Checking under the hood of queries, that kind of thing[/i]
[color=purple][b]===>[/b] Be sure to also check the [url="/wiki/SQL"]SQL FAQ page[/url]. [b]<===[/b][/color]
[h3]How do I see the actual raw SQL query generated by CI's Active Record[/h3]
[b]Answer 1[/b] Turn on [url="http://codeigniter.com/user_guide/general/profiling.html"]profiling[/url] (part of the benchmarking class) - this will show the full detail of all the SQL queries for the page.
[b]Answer 2[/b] You can do this before your query: [code] $this->db->_compile_select(); [/code] .. and then this, once you've run the query: [code] $this->db->last_query(); [/code]
[h3]How do I find out the ID of the row I just inserted?[/h3] [i][color=grey](Just so that people searching for this will be more likely to find it, we'll mention that this is comparable to the native PHP mysql_insert_id() function.)[/color][/i]
This is covered in the [url="http://codeigniter.com/user_guide/database/helpers.html"]Query Helper Functions[/url] section of the CI User Guide - the function you're looking for is: [code] $foo = $this->db->insert_id(); [/code]
[h3]How do I do a COUNT('foo') using the Active Record functions?[/h3] You need to use the SQL [b]AS[/b] feature, where you assign a new name to a piece of data. For example: [code] $this->db->select("COUNT('foo') AS foo_count", FALSE); // Run your query, and then use the foo_count variable. [/code] Refer to the CI User Manual's section on [url="/user_guide/database/active_record.html"]Active Record Class[/url] for more information.
[h3]Can I extend the core Database class?[/h3]
[b]Answer 1[/b] No, you can not. This is quite explicitly described in the [url="/user_guide/general/creating_libraries.html"]Creating Libraries[/url] section of the user guide: [quote] [color=orange] The Database classes can not be extended or replaced with your own classes, nor can the Loader class in PHP 4. All other classes are able to be replaced/extended. [/color] [/quote]
[b]Answer 2[/b] Yes, you can, using the [url="/wiki/Extending_Database_Drivers"]method described[/url] by [b]rafsoaken[/b]
[h3]Oracle does weird things[/h3] Yes, this seems to be kind of known, though so few CI users are on Oracle that it's hard to pin down the problem.
Have a read of [url="/forums/viewthread/110597"]this thread[/url], especially if you're seeing an off-by-one error on row counts.
[url="/forums/viewthread/103309"]This thread[/url] relates to CI 1.7.0 and Oracle versions 9 & 10 - not being able to connect to the database from within CI.
There are some other [url="/wiki/Oracle:Known_Issues/"]Known Issues with Oracle[/url] documented on this wiki.
[h2]Design[/h2]
[i]Architecture of your application, what goes where, design patterns, that kind of thing[/i]
[h3]I keep repeating lumps of code - things like login-dialogs, stylesheets, headers, footers, or menus - is there some place I can define them just once?[/h3]
[i]Short answer[/i] - you probably want to [url="/wiki/MY_Controller"]extend the core Controller[/url], so do a search in the forums for '[b]MY_Controller[/b]', or check out the section covering this on the [url="http://codeigniter.com/wiki/Header_and_footer_and_menu_on_every_page/"]different Approaches Guide[/url] page. This works well when you want to seamlessly introduce data into the CI superobject ($this-> ...).
[i]Less short answer[/i] - in very general terms, if you want to call a function that doesn't require access to the CI superobject, then you probably want a Helper (and you probably want to autoload it). If you want some functions that needs database access to a small number of tables, and is likely to be used through many controllers, you might be best with an autoloaded Model.
[i]Longer answer[/i] - this is also covered by the next section (on nested templates, view partials etc) which in turn contains many links into the forums - this will provide a wealth of alternative approaches.
It is also asked frequently (hence its presence here - duh!) on the forums -- about once every 36 hours on average, usually prefaced with the lie [i]'I couldn't find anything about how to do this...'[/i]. You're advised (read strongly encouraged) to use the forum search feature and [b]Search in Titles Only[/b] for the words 'header' and 'footer'.
Asking this question anew within the forums is likely to imbue a certain degree of chagrin.
[h3]How do I embed views within views? Nested templates? Header, Main, Footer (Blocks) design? Partials? Page Fragments? Ruby on Rails style Yield functionality?[/h3]
Check out the [url="http://codeigniter.com/wiki/Header_and_footer_and_menu_on_every_page/"]Different Approaches Guide[/url] to this problem.
There are several other described ways to deal with this problem:
Version 1.6 (Released January 31, 2008) has support for multiple views. See the User Guide for more details. That change to the core makes some of these approaches "old school" (as it were). This forum thread covers some good ground: [url=http://codeigniter.com/forums/viewthread/87346/]http://codeigniter.com/forums/viewthread/87346/[/url]
First, basic information on views and the optional third 'return' parameter: [url=http://www.codeigniter.com/wiki/Displaying_Multiple_Views/]http://www.codeigniter.com/wiki/Displaying_Multiple_Views/[/url] and also, [url=http://codeigniter.com/user_guide/libraries/loader.html]The CI Loader class documentation page[/url]
Coolfactor's View Library is one solution to nesting views in a tidy way. [url]http://codeigniter.com/forums/viewthread/49910/[/url]
Gyorgy Fekete's View library similar to Coolfactor's with the differences spelled out in the thread. [url]http://codeigniter.com/forums/viewthread/62521/[/url]
Another place to get advice if you don't want to add a new library is Rick Ellis' original thread on the subject (Embedding Views within Views). [url]http://codeigniter.com/forums/viewthread/44916/[/url]
Also, teamhurting has implemented a Ruby On Rails style Yield approach that uses CI's hooks system: [url=http://codeigniter.com/forums/viewthread/57902/]Yield using hooks - http://codeigniter.com/forums/viewthread/57902/[/url]
Another similar approach using a basic class rather than a class as a hook: [url=http://codeigniter.com/wiki/layout_library/]http://codeigniter.com/wiki/layout_library/[/url]
A thread where esra lists a few threads on this topic: [url=http://codeigniter.com/forums/viewthread/57965/]http://codeigniter.com/forums/viewthread/57965/[/url]
A Rails-Style ActionView library and helper called Ocular has a [url=http://codeigniter.com/wiki/Ocular_Layout_Library/]wiki page here: http://codeigniter.com/wiki/Ocular_Layout_Library/[/url] and a thread here: [url=http://codeigniter.com/forums/viewthread/65050/]http://codeigniter.com/forums/viewthread/65050/[/url]
A thread for a View library (author tested with Matchbox) [url=http://codeigniter.com/forums/viewthread/67028/]http://codeigniter.com/forums/viewthread/67028/[/url]
[h3]How do I pass parameters to a controller's index() function?[/h3]
The obvious problem is that a second parameter on the URL will be interpreted as the method name.
[b]Short answer[/b] - use a URL like this: /controller/index/param1/param2
[b]Long answer[/b] - using [b]_remap()[/b] you can work some magic. This code is supplied courtesy of [url="/forums/viewthread/135187/#668213"]Colin Williams[/url]
[code] function _remap ( $method ) { $param_offset = 2;
// Default to index if ( ! method_exists($this, $method)) { // We need one more param $param_offset = 1; $method = 'index'; }
// Since all we get is $method, load up everything else in the URI $params = array_slice($this->uri->rsegment_array(), $param_offset);
// Call the determined method with all params call_user_func_array(array($this, $method), $params); } [/code]
[h3]How do I call one Controller's methods from a different Controller?[/h3] [b]Short answer[/b]: You don't
[b]Wrong answer[/b]: Use the [b]$CI = get_instance()[/b] trick.
[b]Long answer[/b]: You shouldn't actually be trying to do this. It implies that your design isn't quite right, but on the upside it's quite easy to re-engineer the relevant bits when you first discover this problem.
It's likely that the method you want to call should simply be relocated - this might mean moving it into a [b]helper[/b], [b]library[/b], [b]model[/b] or your [b]MY_Controller[/b] - where it can be accessed by any number of controllers. You're encouraged to consult the user guide regarding helpers v. libraries, and elsewhere on this page and wiki for information about MY_Controller.
[h2]Errors[/h2]
[i]Frequently Seen Error messages, that kind of thing[/i]
[h3]Headers already sent ...[/h3] 9 times out of 10, this error is caused by one of:
- whitespace after a closing ?> tag,
- whitespace before a <?php opening tag at the start of one of your files
- residual debugging echo (print_r, var_dump, etc) in your non-view files
The solution(s) are easy - do not use closing ?> tags anywhere, ever, in your libraries, models, controllers and helpers. Check those files for any whitespace at the start of the file. Check for echo/print_r/var_dump/etc functions - usually you'll see the output of those on-screen before the error message, in any case.
[h2]Forms and Input and POST data[/h2]
[i]Handling incoming form data, ->input->post .v. $_POST, that kind of thing[/i]
[h3]Is there a way to cycle $this->input->post() items?[/h3] There are no CodeIgniter functions to do this, but you can accomplish this easily with a construct such as this one. Note that [b]$foo[/b] is never used, its presence just allows us get to [b]$key[/b]. The result of this function is a [b]$safe_post_array[/b] that contains all posted data that has passed your own [url="/user_guide/libraries/input.html"]Input Class[/url] rules. [code] foreach ($_POST as $key=>$foo) { // $foo is never actually used $safe_post_array[$key] = $this->input->post($key); } [/code]
You can also do something neat like this, if you want to pull all the data out of $_POST into your own array, cleaning it as it comes across: [code] $data['fields'] = xss_clean($_POST); [/code]
[h2]Images[/h2]
[i]Embedding graphic images, that kind of thing[/i]
[h3]When I load an image in my code, it doesn't appear on the rendered page?[/h3]
This is a hugely common problem, and 99 times out of ten it's because you are confused about paths. This is very much a PHP / HTML problem, not a CodeIgniter one, but the following hints might help you resolve this.
[b]First[/b] - look at the page source in your browser - this is the [b][i]most important and most useful thing[/i][/b] you can examine. It'll give you very strong hints immediately as to what's going wrong. You can cut-n-paste the img src component into another browser, and then start modifying it until you find a path that works.
[b]Second[/b] - utilise the CI helpers, if you're not already. For example, if you keep your images at [b][i]/assets/images[/i][/b] then you can pull [b][i]foo.jpg[/i][/b] in with this code (modify to taste) in your view: [code] <?php $foovar = "foo.jpg"; $img_array = array ("src"=> "/assets/images/". $foovar, "border"=>"0"); echo anchor (img($img_array)); ?> [/code]
[h2]JavaScript and AJAX and JS libraries[/h2] [i]I'm confused[/i]
Yes, but you needn't be. JS / AJAX works the same in CI as it does in [i]conventional[/i] PHP - you still need to write your JS, ship it out, have URL's that can respond sensibly to JS queries and the like.
Do a search on the Intergoogle for some tutorials - there are bucketloads of the things out there. [url="http://choosedaily.com/1052/9-ways-to-integrate-ajax-with-codeigniter/"]This page shows 9 ways to integrate AJAX with CodeIgniter[/url] for example (assuming the link is still alive).
A neat trick suggested by [b][url="/forums/viewthread/135024/#666385"]eoinmcg[/url][/b] is to put this function into your [b]MY_Controller[/b]: [code] function _is_ajax() { return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')); } [/code]
You can then call [b]$this->_is_ajax()[/b] in your controllers if you want different courses of action based on the type of query for that particular method.
There are a couple of CodeIgniter wrappers or libraries for simplifying (or at least standardising) the way you implement Java_Script: [b]o[/b] [url="/wiki/Carabiner/"]Carabiner[/url], by [url="/forums/member/83507/"]tonydewan[/url] [b]o[/b] [url="/forums/viewthread/101236/"]External library[/url] by [url="/forums/member/72587/"]darkhouse[/url]
[h2]Site migrations, relocations and platform differences[/h2]
[i]Relocating from GNU/Linux to Windows, changing databases engines, that kind of thing[/i]
[h3]After moving from development to production, my site stopped working[/h3]
There are several [b]sub-categories[/b] to this problem, depending what you mean by 'doesn't work'[/h4]
[color=red][i]Please note that you should have done [b]ALL[/b] of the following easy steps before posting to the forums:[/i][/color] [b]o[/b] use [b]phpinfo.php[/b] and confirm [b]mod_rewrite[/b] is available [b]o[/b] cycled through the [b]$config['uri_protocol'][/b] options - trying ALL of the FIVE choices there [b]o[/b] checked / changed your [b].htaccess[/b] for host-specific paths
[b][i][u]Subcategory[/u] - my index.php isn't being hidden anymore[/i][/b] If your .htaccess mod_rewrite is no longer working -- check that you have [b]AllowOverride All[/b] in your [b]host's[/b] config file for the public web directory of your website.
[b][i][u]Subcategory[/u] - my library/model/etc isn't loading/running/etc[/i][/b] If this has bitten you as you've gone from MS-Windows to a GNU/Linux platform, please verify the case of your libraries and models. Note this is not a bug with GNU/Linux - rather an unfortunate side effect of NTFS being case-insensitive and consequently hiding any failure to abide by the documented standards for class and file names.
Consult the user guide for specifics, but briefly - Model classes should be a capitalised (first character only) with file names entirely lower case. Library classes are capitalised (first character only) with file names capitalised similarly. Core class extensions start with MY_ (unless you've changed the prefix in config.php) for both filename and class name.
[h3]How do I migrate my existing 'normal' PHP site to CodeIgniter?[/h3]
[b]Answer 1 - provided by [url="http://codeigniter.com/forums/member/87583/"]n0xie[/url][/b] How we migrated a rather big site:
Put the whole site in a subfolder of your root web folder, and use .htaccess to rewrite all requests to the subfolder (we named it ‘oldsite’ but any name will do). Be wary of absolute file paths (do a find on the entire codebase to look for any file that uses the filesystem). Make sure everything works before you continue any further.
Now setup your CI site in the root of the webfolder. Make sure the .htaccess still routes all traffic to the old site.
Now overwrite the Router class with a MY_Router. Let CI check if there is a controller present which matches against the url (the way it normally does). If not, redirect to the oldsite folder. Remove the .htaccess rule that redirects all traffic to the old site.
Now every request will go through the CI index.php. The Router Class will test if a controller exists for the requested url. (just like it would normally so you can user your routes in your config folder). If it won’ t find one, normally CI would return a 404. Instead it now redirects the request to the old site.
Now you can slowly migrate parts of the website. Since the CI controllers take precedent over the old website, you can slowly replace parts of the old websites, and add new features as you would a normal CI site.
One word of caution. This will seriously impact any pagerank google has given to your website, since google doesn’t like redirects. To counter this you can use your .htaccess to rewrite old urls to the ‘oldsite’ subfolder, although the list could become quite large.
[b]Answer 2[/b] provided by [url="http://codeigniter.com/forums/member/39899/"]sophistry[/url][/b] The [url="/wiki/site_migrate/"]Site Migrate[/url] wiki page provides a controller that lets you "drop in" an entire existing web site. Just place it in your "views" directory and CI can serve it up just like before (this can even be accomplished with no URI changes). No messing around with .htaccess and redirects. Uses CI's _remap() function.
[h2]CodeIgniter and EllisLab and ExpressionEngine[/h2]
[i]Release schedules, contributions, that kind of thing[/i]
[h3]When is the next version of CodeIgniter coming out? What can I expect from the next version of CodeIgniter?[/h3]
CodeIgniter is a product of EllisLab. New releases and new features in the framework are made available when it is possible to release them, and may always be a surprise: [url]http://codeigniter.com/forums/viewthread/53619/P15/#262381[/url]
You can glean some insight from reading the [url="http://codeigniter.com/user_guide/changelog.html"]Changelog[/url], which shows the historical frequency of releases and the types and scale of changes.
[h3]I've got cool new code for CI. What is the process for getting it included in the official CI distribution?[/h3]
New code is welcome, but not all code can be included. To maximize the chances of your code making it into CI, test (PHP 4 and 5) and document the code. All decisions about incorporating it fall to EllisLab. Basically, if you want to satisfy your lust for abiding fame you have to make an effort to promulgate your code to as many CI developers as possible who put it through an informal 'vetting' process. As part of this process you should create a 'manual page' to help people learn.
[h2]Plain old frequently asked questions[/h2]
[i]Almost a 'misc' category, but categorically [b]not[/b] a dumping ground - these are simply uncategorised questions ... for things that aren't any obvious or particular kind of thing yet[/i]
[h3]How do I find the name of the current controller and/or method?[/h3]
There are standard PHP function that's good for this kind of thing, and you're encouraged to consult the PHP User Guide.
[b]get_class()[/b] will return the current class's name.
[b]get_class_methods()[/b] will return the full list of methods for a given class.
Alternatively, consult the [url="http://codeigniter.com/user_guide/libraries/uri.html"]URI Class[/url] in the user manual, paying particular attention to the [b]rsegment()[/b] function - as you may be happy with pulling segment(1) for your class, and segment(2) for your method.
Alternatively, you can query the CI Routing class directly, using these functions: [code] $this->Router->class; $this->Router->method; [/code]
[h3]Can I cache only certain parts of a page? [/h3]
A. This is related to the question above about nested templates and partials. Basically, CI cache library (1.5.4) only supports full page caching - it's all or nothing. There are several contributions that can help.
[url=http://codeigniter.com/forums/viewthread/56456/]The Sparks library[/url] is one approach. (NOTE: [url=http://codeigniter.com/forums/viewthread/56456/P20/]the Sparks object caching library is currently an orphan[/url] (as of 20070925) as the developer has moved on to Zend Framework - anyone want to step up and carry it on?)
In case you have questions to ask on the forum, please review this general information on caches. There are many levels of caching and they can be broken down into a few categories and approaches:
PHP code itself: php opcode of some kind
The following can be classed as session-specific or global caches depending on the approach: DB cache: db query, db object serialization HTML output cache: partial or full page caching
Browser cache is always (by definition) session-specific: Browser cache: using headers to control cache, JS and CSS architecture to optimize browser cache-ability
[h3]How do I call methods in one controller via another controller?[/h3]
[i]Often this question disguises a requirement for some shared methods - consult the earlier question on [b]View Partials[/b] and [b]Header/Footer/Menu common views[/b] first, and confirm your question isn't better answered there.[/i]
Modular Extensions (ME) aka the HMVC library allows you to do this. [url=http://codeigniter.com/wiki/Modular_Extensions_-_HMVC/]ME/HMVC[/url]You don't. See [url=http://codeigniter.com/forums/viewthread/55212/]http://codeigniter.com/forums/viewthread/55212/[/url] for more discussion.