Logo F2FInterview

Codelgnitor Interview Questions

Q   |   QA

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.

There are no CodeIgniter functions to do this, but you can accomplish this easily with a construct such as this one. The result of this function is a $safe_post_array that contains all posted data that has passed your own Input Class rules.

foreach (array_keys($_POST) as $key) {
   $safe_post_array[$key] = $this->input->post($key);
   }

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:

$data['fields'] = xss_clean($_POST);  

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.

First - look at the page source in your browser - this is the most important and most useful thing 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.

Second - utilise the CI helpers, if you’re not already. For example, if you keep your images at /assets/images then you can pull foo.jpg in with this code (modify to taste) in your view:

<?php
    $foovar = "foo.jpg";
    $img_array = array ("src"=> "/assets/images/". $foovar, "border"=>"0");
    echo anchor (img($img_array));
?>  

Answer 1 - provided by n0xie
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.

Answer 2 - provided by sophistry
The Site Migrate 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.

The HTML Table Class is a convenient way of generating HTML tables quickly and easily. However it does have its shortcomings, and consequently there are regular questions in the Forums about how to introduce dynamic href links into rows, implementing special CSS markup, and so on.

Because of this - and also because generating HTML within the Controller goes a little against the usual MVC separation of responsibilities - most of the cool kids will recommend that you write your own view partial that generates your <table>...</table> string. You can make this as specific or as generic as you need, of course.

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: