TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Including.. :S (http://www.talkphp.com/absolute-beginners/2563-including-s.html)

Tanax 04-01-2008 03:38 PM

Including.. :S
 
Ye, something's.. wrong >.<

My config.php lies in:
website/includes/config.php

My admin files lies in:
website/admin/*.php

It works fine to include the config from there with this:
PHP Code:

include('../includes/config.php'); 

However, I have one admin file in a subfolder to the admin.

website/admin/edit/tab.php

And I can't seem to include the config file from there :S
I tried with:
PHP Code:

include('.../includes/config.php'); 

But it didn't work :S

abiko 04-01-2008 04:05 PM

Try a path to the included file
- include '/your/path/to/folder/includes/config.php';
it'll be better if you have one file that includes all other files - your admin.php just includes other needed files - the tab.php file. And then in your admin file you make all the configuration needed to run your site.

Or if you have you config file in an array, or if it is an object you can use the global var scope.

Here are 2 examples:
- array styled config file
- object styled config file

PHP Code:

<?php
// The config file

// Array style
$config = array( 'database' => array( 'username' => 'mysql_user',
                                                                            
'pass'        => 'mysql_pass',
                                                                            
'database' => 'db'
                                                                            
),
                                 
'site_name' => 'The site',
                                 
'caching'   => '0'
                                 
);

// Object style
class siteConfig {
    protected 
$database = array( 'username' => 'mysql_user',
                                                                            
'pass'        => 'mysql_pass',
                                                                            
'database' => 'db'
                                                                            
);
    protected 
$site_name 'The site';
    protected 
$caching   '0';
}

?>

Now you structure your admin.php
PHP Code:

<?php
include '../includes/config.php';

/*
 * If you are using the array styled config file
 * your configuration is avaible trought the $config var
 * or any other you defined ($conf, $c ...)
 */
 
 // If using the object style config file
 
$config = new siteConfig();
 
 
 
// Using the object styled 
 // Example:
 
mysql_connect'localhost'$config->database['username'], $config->database['pass']);


// And here you include your files that depend on, let's say, 
// page var
$page $_GET['page'];

switch( 
$page ) {
    case 
'news':
     include 
'somefile.php';
    break;
    
    default:
        include 
'mainFile.php';
    break;
}
?>

Now when you include the file - no need for config.php to be included again and again - this way you built a platform for your included fileds.
And if you are using functions and you need to access the $config - you use the global variable scope

PHP Code:

<?php

function test() {
 global 
$config;
  
// The stuff the function does
}
?>


wGEric 04-01-2008 04:48 PM

.. goes back one directory so you need to go back two, ../../

PHP Code:

include('../../includes/config.php'); 

. is the same directory. .. is the parent directory. You can put as many of those in there as you need. Separate folders with a / .

Tanax 04-01-2008 06:07 PM

Thanks both of you :-)
I found that wGEric's method worked best for me :-)

johnN 04-03-2008 11:37 PM

To stop this issue cropping up at all, use $_SERVER['DOCUMENT_ROOT']. This will automatically go to your root dir:)

you'd just put include($_SERVER['DOCUMENT_ROOT']."/website/includes/config.php");

works in any directory

Tanax 04-04-2008 08:16 AM

Quote:

Originally Posted by johnN (Post 13140)
To stop this issue cropping up at all, use $_SERVER['DOCUMENT_ROOT']. This will automatically go to your root dir:)

you'd just put include($_SERVER['DOCUMENT_ROOT']."/website/includes/config.php");

works in any directory

Oo didn't know that.. I thought that I could use like

include('/website/includes/config.php');


Still, I didn't want to use this, since I might change the name of the folder of the website, so I needed a path FROM the current folder :-)

Salathe 04-04-2008 11:50 AM

To always reference the path to the file with the include call in it, you can use the dirname function with an argument using the constant __FILE__, the latter always giving the path to the file it is used in. So used from admin/edit/tab.php: include dirname(__FILE__).'/../../includes/config.php';

It might, or might not, prove useful to you. :-)

Tanax 04-04-2008 03:23 PM

Thanks :-D Can be useful, not in this case though, but it's always good to know ;-)


All times are GMT. The time now is 08:38 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0