Print Page | Close Window

how to change the path dynamically according to?

Printed From: Forum openElement
Category:

openElement


Forum Name: Dynamic OE - Databases, etc
Forum Description: openElement's database management system and Element Packs
URL: http://forum.openelement.com/fr/forum_posts.asp?TID=962
Printed Date: 08 Dec 2019 at 06:17
Software Version: Web Wiz Forums 10.11 - http://www.webwizforums.com


Topic: how to change the path dynamically according to?
Posted By: djju1029
Subject: how to change the path dynamically according to?
Date Posted: 01 Feb 2017 at 20:45
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



Replies:
Posted By: Dmit OE
Date Posted: 02 Feb 2017 at 09:54
Hi.

Where is this code written exactly ($db = ....)?


Posted By: djju1029
Date Posted: 02 Feb 2017 at 15:15
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.



Posted By: Dmit OE
Date Posted: 02 Feb 2017 at 16:35
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);


Posted By: djju1029
Date Posted: 02 Feb 2017 at 17:28
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.



Posted By: Dmit OE
Date Posted: 02 Feb 2017 at 17:37
__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.


Posted By: djju1029
Date Posted: 02 Feb 2017 at 19:36
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



Posted By: Dmit OE
Date Posted: 02 Feb 2017 at 21:01
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.


Posted By: djju1029
Date Posted: 02 Feb 2017 at 23:25
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


Posted By: Dmit OE
Date Posted: 03 Feb 2017 at 10:32
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.


Posted By: Dmit OE
Date Posted: 03 Feb 2017 at 10:33
P.S. If you your case the site is always located in a folder that's named in a certain way and it won't change, you can use that as well.


Posted By: Dmit OE
Date Posted: 03 Feb 2017 at 10:42
Or, to not bother about all this, here is a universal solution, given that you do not have any index.php or index.htm in subfolders of your project somewhere. Insert this into a Code Block Source type PHP in your baselayer or anywhere else:

function findSiteRootPath()    {
   
    $path = __DIR__; // page or script's absolute path, somewhere in the project
   
    // find the folder where index.php or index.htm is, starting from the current folder:
    while ((strpos($path, '/') || strpos($path, '\\')) && !file_exists("$path/index.php") && !file_exists("$path/index.htm")) {
        $path = dirname($path);
    }
   
    return $path; //
}

$root = findSiteRootPath();
// Test:
echo "<br><br>Path to the Files/Other folder: $root/Files/Other";


Posted By: djju1029
Date Posted: 03 Feb 2017 at 15:44
I need your help, a solution using certain Packs auxiliary functionality. I couldn't do a beacon method to find the physical root path of the website....Cry

Many many thanks,

DJ


Posted By: Dmit OE
Date Posted: 03 Feb 2017 at 17:14
Check my latest pos, it should suit you


Posted By: djju1029
Date Posted: 03 Feb 2017 at 19:34
DmitOE,

I found that $_SERVER["PHP_SELF"] returns same result from PC or my Raspberry Pi such as /test/manual_scripting (not \ from pc)

Therefore, I wrote simple function that this path to relative dotted path as following:

function changeCurrentPath2RelativeDot(){
$testpath = explode("/", dirname($_SERVER['PHP_SELF']));
$no_double_dot = count($testpath) - 1;
$testpath = str_repeat('../', $no_double_dot);
return $testpath;
}
$relative_dot = changeCurrentPath2RelativeDot();

// Test:
echo "<br><br>RelativePath to the Files/Other folder: ",$relative_dot,"Files/Other"; //result is ../../Files/Other

I checked the $relative_dot gives corrent number of dot path such as ../../../ where page is.
I put this function into Start of Document of BaseLayer.
But I got another problem.
I need to use $relative_dot on CSS link in the header like this:

<Absolute>
<link href="C:/Users/djoo/Desktop/PortableGit/project/NMU_mockup/WEFiles/Other/jquery-ui.css" rel="stylesheet">

<Relative>
<link href="../../WEFiles/Other/jquery-ui.css" rel="stylesheet">

when $relative_dot is ../../

How can I change this absolute to relative path in HTML using the result of my function?


Posted By: Dmit OE
Date Posted: 03 Feb 2017 at 21:08
I don't understand where you are blocked; you seem to have found an effcient function to get the path "back" to the root. By the way it is relative to the page location, not an included script. Why not simply prepend it to "/WEFiles/Other/jquery-ui.css"?


Posted By: djju1029
Date Posted: 03 Feb 2017 at 21:47
Cry Where I am blocked is prepend my result of "$relative_dot" to link in the header.

This question comes from when I look your source.

When I look into original source view of page, I found the link of header is relative dot to back as following:

It is original source view of page and link of css file in header:
<link id="openElement" rel="stylesheet" type="text/css" href="../../../WEFiles/Css/v02/openElement.css?v=50491101600" />

I just want to copy your way in my source, so I found how to change current absolute dirpath to relative ../ to get back to root.
But I have no idea how to prepend it to link path commonly.

If I put $relative_dot into the link, but it don't work....

When I look at source view of page, I found:
var WEInfoPage = {"PHPVersion":"phpOK","OEVersion":"1-56-0","PagePath":"test/examples/test_folder/Copy-BlankLayer","Culture":"DEFAULT","LanguageCode":"EN-US","RelativePath":"../../../","RenderMode":"Export","PageAssociatePath":"test/examples/test_folder/Copy-BlankLayer","EditorTexts":null};

See, there is "RelativePath":"../../../" 
Eureka! I think it has some secret on your source that change path relatively to the root.
I just want to figure out how you realize this way and to copy it on my source.....Cry


Posted By: Dmit OE
Date Posted: 03 Feb 2017 at 21:57
This is calculated inside OE, who knows where each page is exactly. There's a way to get it in PHP version if you use Packs on the page, but it is not necessary I think. Your code allows to get the same value (is it?), ex. "../../../", so you just do something like:

echo "<link href='{$relative_dot}Files/Other/jquery-ui.css' rel='stylesheet'>";

Why woud this not work? Just not WEFiles/Other but Files/Other (and place the file jquery-ui.css accordingly).

I have a n impression that you are missing something rather obvious. If nothing works, send me your project via a private message with the code that does not work, I will look why when I have time.


Posted By: djju1029
Date Posted: 03 Feb 2017 at 23:06
Many many thanks to DmitOE. Finally, I did it like ths,

<include, by PHP at start of document of BaseLayer.php>

function changeCurrentPath2RelativeDot(){
$testpath = explode("/", dirname($_SERVER['PHP_SELF']));
$no_double_dot = count($testpath) - 1;
$testpath = str_repeat('../', $no_double_dot);
return $testpath;
}
$relative_dot = changeCurrentPath2RelativeDot();

include_once($relative_dot."Templates/includes/nmudb.php");
require_once($relative_dot."Templates/includes/php_serial.class.php");

<jquery_ui, by HTML at head of BaseLayer.php>

<?php echo "<link href='{$relative_dot}WEFiles/Other/jquery-ui.css' rel='stylesheet'>"; ?>
<br>
<?php echo "<script type='text/javascript' src='{$relative_dot}WEFiles/Other/jquery-ui.js'></script>"; ?>

every page woks! Thank you, my mentor!



Posted By: djju1029
Date Posted: 07 Feb 2017 at 15:37
DmitOE, I found an error in my code.

As OE at PC local server, dirname($_SERVER['PHP_SELF']); is "\". Therefore,
explode("/", dirname($_SERVER['PHP_SELF']));  is array(1) { [0]=> string(1) "\" }

BUT, AS webserver at RPi, dirname($_SERVER['PHP_SELF']); is "/". Therefore, 
explode("/", dirname($_SERVER['PHP_SELF']));  is array(2) { [0]=> string(0) "" [1]=> string(0) "" }

Consequently, my source never point to home root where PHP_SELF is home.

Finally, I debug my code as following;

function changeCurrentPath2RelativeDot(){
$current_path = dirname($_SERVER['PHP_SELF']);
if ($current_path == "/") {
$testpath = "";
return $testpath;
} else {
$testpath = explode("/", $current_path);
$no_double_dot = count($testpath) - 1;
$testpath = str_repeat('../', $no_double_dot);
return $testpath;
}
}
$relative_dot = changeCurrentPath2RelativeDot();

include_once "{$relative_dot}Templates/includes/nmudb.php";
require_once "{$relative_dot}Templates/includes/php_serial.class.php";

Sorry for making your confusion.


Posted By: Dmit OE
Date Posted: 07 Feb 2017 at 16:03
Ok thanks for the details



Print Page | Close Window

Forum Software by Web Wiz Forums® version 10.11 - http://www.webwizforums.com
Copyright ©2001-2012 Web Wiz Ltd. - http://www.webwiz.co.uk