Welcome Guest, Not a member yet? Register   Sign In
Problems with include path
#1

I'm building a CI website and I want to include a file with some variables. I'm using the standard PHP approach:

Code:
include ('includes/bookdata.php');

This works fine for the home view but I have some views in a folder called 'books' and CI throws an error (failed to open stream: No such file or directory). I assumed it was a relative path problem so I did:

Code:
include ('../includes/bookdata.php');

but I still get the error.

Can anyone auggest what I'm doing wrong?
Reply
#2

Why would you use include to load some variables?
CI has a great option for this.
Inside your controller, you put:
Code:
$this->config->load('bookdata');
This loads the file bookdata.php from your application/config folder.

Take a look at the documentation about the Config class.

If you insist on including a file, try this:
Code:
include ( APPPATH . 'includes/bookdata.php' );
Reply
#3

Well I am aware of the config array and am using it but for the database usage there are many lines of code (long foreach loop) required to extract book data from a large array and I wanted to extract all that to another file and 'include' it in the view where needed, However that doesn't seem to be possible. I tried your suggestion of APPPATH but it didn't work so I am now resigned to including all the foreach code in the view, which does at least work.

I rather hoped there might be a function like $this->load->include('filename');

Thanks for your interest.
Reply
#4

From your description, it sounds like that foreach loop should be running in your model or controller.

  Once you have extracted the data from the first array and written it to Yet-Another-Array, that YAA array is simply passed to the view as data in the normal MVC process.  The User Guide describes this process in detail.

  The idea is to remove all data processing from the view.  While it does sometimes happen that data manipulation is performed in the view code, it is not strictly MVC compliant.  (The MVC Police are ever vigilant and have wet noodles ready to chastise you for this...  That is a joke in case you don't get the idiom. You can do what you want in CI, it is very forgiving.)

  If the original data array (the large one) is static data, then certainly it could be typed, once, into the config array per the first comment by Wouter60.  If it is NOT static data, then the same process which creates it may also create the secondary YAA array.  Again, not in the view, but in the model or controller.

  If you will show us your array code, we can help you move that processing out of the view.
CI 3.1 Kubuntu 19.04 Apache 5.x  Mysql 5.x PHP 5.x PHP 7.x
Remember: Obfuscation is a bad thing.
Clarity is desirable over Brevity every time.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB