BullGuard Antivirus Sale - 60% Off with openElement !
Forum Home Forum Home >

openElement

> Dynamic OE - Databases, etc
  New Posts New Posts RSS Feed - how to change the path dynamically according to?
  FAQ FAQ  Forum Search   Register Register  Login Login

how to change the path dynamically according to?

 Post Reply Post Reply Page  123>
Author
Message
djju1029 View Drop Down
Senior Member
Senior Member
Avatar

Joined: Aug 02 2016
Location: the US
Status: Offline
Points: 126
Post Options Post Options   Thanks (0) Thanks(0)   Quote djju1029 Quote  Post ReplyReply Direct Link To This Post Topic: how to change the path dynamically according to?
    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
Back to Top
Dmit OE View Drop Down
Admin Group
Admin Group


Joined: May 31 2012
Status: Offline
Points: 5283
Post Options Post Options   Thanks (1) Thanks(1)   Quote Dmit OE Quote  Post ReplyReply Direct Link To This Post Posted: Feb 02 2017 at 8:54am
Hi.

Where is this code written exactly ($db = ....)?
Back to Top
djju1029 View Drop Down
Senior Member
Senior Member
Avatar

Joined: Aug 02 2016
Location: the US
Status: Offline
Points: 126
Post Options Post Options   Thanks (0) Thanks(0)   Quote djju1029 Quote  Post ReplyReply Direct Link To This Post Posted: Feb 02 2017 at 2:15pm
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.

Back to Top
Dmit OE View Drop Down
Admin Group
Admin Group


Joined: May 31 2012
Status: Offline
Points: 5283
Post Options Post Options   Thanks (1) Thanks(1)   Quote Dmit OE Quote  Post ReplyReply Direct Link To This Post Posted: Feb 02 2017 at 3:35pm
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);
Back to Top
djju1029 View Drop Down
Senior Member
Senior Member
Avatar

Joined: Aug 02 2016
Location: the US
Status: Offline
Points: 126
Post Options Post Options   Thanks (0) Thanks(0)   Quote djju1029 Quote  Post ReplyReply Direct Link To This Post Posted: Feb 02 2017 at 4:28pm
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
Back to Top
Dmit OE View Drop Down
Admin Group
Admin Group


Joined: May 31 2012
Status: Offline
Points: 5283
Post Options Post Options   Thanks (1) Thanks(1)   Quote Dmit OE Quote  Post ReplyReply Direct Link To This Post Posted: Feb 02 2017 at 4:37pm
__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.
Back to Top
djju1029 View Drop Down
Senior Member
Senior Member
Avatar

Joined: Aug 02 2016
Location: the US
Status: Offline
Points: 126
Post Options Post Options   Thanks (0) Thanks(0)   Quote djju1029 Quote  Post ReplyReply Direct Link To This Post Posted: Feb 02 2017 at 6:36pm
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

Back to Top
Dmit OE View Drop Down
Admin Group
Admin Group


Joined: May 31 2012
Status: Offline
Points: 5283
Post Options Post Options   Thanks (1) Thanks(1)   Quote Dmit OE Quote  Post ReplyReply Direct Link To This Post Posted: Feb 02 2017 at 8:01pm
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.
Back to Top
djju1029 View Drop Down
Senior Member
Senior Member
Avatar

Joined: Aug 02 2016
Location: the US
Status: Offline
Points: 126
Post Options Post Options   Thanks (0) Thanks(0)   Quote djju1029 Quote  Post ReplyReply Direct Link To This Post Posted: Feb 02 2017 at 10:25pm
CryI 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.....CryCryCry
Back to Top
Dmit OE View Drop Down
Admin Group
Admin Group


Joined: May 31 2012
Status: Offline
Points: 5283
Post Options Post Options   Thanks (1) Thanks(1)   Quote Dmit OE Quote  Post ReplyReply Direct Link To This Post Posted: Feb 03 2017 at 9:32am
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.
Back to Top
 Post Reply Post Reply Page  123>
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.078 seconds.