how to change the path dynamically according to? |
Post Reply | Page 123> |
Author | |
djju1029
Senior Member Joined: Aug 02 2016 Location: the US Status: Offline Points: 126 |
Post Options
Thanks(0)
Posted: Feb 01 2017 at 7:45pm |
Hi OE Team,
I create mydb.php include it into the header of BaseLayer. The path of db is written as: <?php $db = new PDO('sqlite:../Data/Local_previs_DB/nmu.db'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); However, some page could not load the db because its location is different depths under index.php with db path. For example, If test2 page location will be localhost/test/test2.php, then db path should be $db = new PDO('sqlite:../../Data/Local_previs_DB/nmu.db');,but I have no idea how to change the path dynamically according to current page location. I saw your source. The including_once = "the path of OEDB.php" is changed dynamically according to page location but I couldn't figure out how to do that.... Could you let me know your secret of this? Many thanks, DJ
|
|
Dmit OE
Admin Group Joined: May 31 2012 Status: Offline Points: 5283 |
Post Options
Thanks(1)
|
Hi.
Where is this code written exactly ($db = ....)? |
|
djju1029
Senior Member Joined: Aug 02 2016 Location: the US Status: Offline Points: 126 |
Post Options
Thanks(0)
|
The current directory path in my pc is
C:\Users\djoo\Desktop\PortableGit\Project\NMU_mockup (:here is home, index.php) | |--Templates (:here has BaseLayer.php,$db=) | |--test | |--manual_scripting (:here has test.php) |
|--examples (:here has test2.php) Every php page is based on BaseLayer.php. BaseLayer includes db.php in same location which acts db open and changes icons according to queried db parameters. And test pages also select db and update the value to db. Therefore, BaseLayer uses <include_once 'db.php'> in header. Thanks a lot. |
|
Dmit OE
Admin Group Joined: May 31 2012 Status: Offline Points: 5283 |
Post Options
Thanks(1)
|
So, db.php is at some fixed location.
In PHP, you can use the special constant __DIR__ to get the physical path of the script you write it in (no matter what script it is included in). If the path from the location of db.php to the database is ../Data/Local_previs_DB/nmu.db you can get the absolute path in db.php as: $dbPath = __DIR__ . '/../Data/Local_previs_DB/nmu.db'; $db = new PDO('sqlite:' . $dbPath); |
|
djju1029
Senior Member Joined: Aug 02 2016 Location: the US Status: Offline Points: 126 |
Post Options
Thanks(0)
|
If I use __DIR__, I can get absolute path of DB file to dbPath.
But if I include_once this db.php in BaseLayer in the 'start of document', it path is fixed. right? As I see your source, the your path of include_once for OEDB.php is relatively changed as page location, For example, 1) /www/index.php <?php require_once "index(var).php"; include_once "WEFiles/Server/DB/OEDB.php"; ?> 2) /www/templates/BaseLayer.php <?php require_once "BaseLayer(var).php"; include_once "../WEFiles/Server/DB/OEDB.php"; ?> 3) /www/test/manual_scripting/Copy-test.php <?php require_once "Copy-test(var).php"; include_once "../../WEFiles/Server/DB/OEDB.php"; ?> I found your path is relatively changed as following php's location. This is my requesting what I want to learn from you. Thank you for your support. I always appreciate your kind reply. In the beginning of thr project, I'm very beginner of PHP and JS. I am improving to follow your guidance. Edited by djju1029 - Feb 02 2017 at 4:34pm |
|
Dmit OE
Admin Group Joined: May 31 2012 Status: Offline Points: 5283 |
Post Options
Thanks(1)
|
__DIR__ corresponds to where the db.php file is, not where it is called from. So you will always have the same path. You can do echo __DIR__; if you want to see it for yourself.
|
|
djju1029
Senior Member Joined: Aug 02 2016 Location: the US Status: Offline Points: 126 |
Post Options
Thanks(0)
|
I tested $_SERVER["DOCUMENT_ROOT"], but it didn't work as following message:
I couldn't figure out why it didn't work... <code> $test = $_SERVER["DOCUMENT_ROOT"]; echo $test, "</br>"; <result> Notice: Undefined index: DOCUMENT_ROOT in C:\Users\djoo\Desktop\PortableGit\Project\NMU_mockup\test\manual_scripting\Copy-test-BlankLayer.php on line 322 |
|
Dmit OE
Admin Group Joined: May 31 2012 Status: Offline Points: 5283 |
Post Options
Thanks(1)
|
I would not rely on this value. Otherwise, if you tested in the preview on OE local webserver, it puts names in lowercase, i.e. $_SERVER['document_root'] in this case.
|
|
djju1029
Senior Member Joined: Aug 02 2016 Location: the US Status: Offline Points: 126 |
Post Options
Thanks(0)
|
I think we have miscommunication of this topic.
As I researched, __DIR__ is equal to dirname(__FILE__) which return absolute patho of the directory of the file. Even though __DIR__ corresponds to where the db.php file is, the problem is that the Templates page includes this location. You know, if I made a layer page and this layer is used as template to pages, the layer page source will be part of page source. However, all pages are located different location and difference depth. Therefore the include should be called relatively as page's location. In your source, your include_once OEDB.db and link css in header is changed relatively and automatically point to root such as "../" or "../../". I tried to realize thif function in my source but I failed.... I think it would be simple issue on PHP expert but I have no idea how can it be changed relatively... That's my issue.....
|
|
Dmit OE
Admin Group Joined: May 31 2012 Status: Offline Points: 5283 |
Post Options
Thanks(1)
|
I think your problem is licluding db.php itself. As long as you manage to do it,,y solution allows you to create an ABSOLUTE path which will
always be the same regardless of where and how db.php is included, as
long as db.php file itself stays in the same place.
There are several solutions possible, and it depends on whether all your concerned pages can be put into one or two specific subfolders (located in the website root), to use names of those subfolders as a beacon to find the physical root path of the website. If you can't do this, tell me and I will give you a solution using certain Packs auxiliary functionality. |
|
Post Reply | Page 123> |
Tweet
|
Forum Jump | Forum Permissions You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |