Backtrace question

Hello, i am hoping that someone can confirm a legitimate use of backtrace. i am building a web site as follows:

outside of root
[dir] holds template files for header.php, footer.php and content pages

web root
[dir] with [dir] with [dir] with [dir] with [dir] index.php

the index.php is simply include statements calling header, content, footer outside of root.
i decided to use backtrace to display link titles based upon directory names.

the title="<?php titlefunction(); ?>" works but i am using a Windows10 machine with xampp. i can’t see a reason why this would not work on a real server on a linux machine but i am an amateur. i’d hate to replace all of my typing with auto code that stops working on a real server. can someone confirm that this works on a real server? is this method ok? i just want to use the directory name in the title attribute of a link. remember that the file is outside of the root so i use backtrace to get the dir names based upon the location of the calling file (in this case index.php).

Thank you.

Trying to understand what you mean. Backtracing is for debugging, not making an app work.

As for what you are doing, you can do that, but I would use classes for it and a class autoloader.

<?php
function DeepDirTitle($step) {
    $tracer = debug_backtrace();
    $deep1020 = dirname($tracer[1]['file']);
    $deepPath = dirname($deep1020, $step);
    if ($step > 0) {
      echo basename($deepPath);
    } else {
      echo basename($deep1020);
    }
}
?>

i use this code in the SpeciesHeader.php template.
if i don’t use backtrace, then the dir names are called from the nature folder outside of the root. i want the dir names from the directories holding the index files that include the SpeciesHeader.php template.

Birds
DeepDirTitle(3) reveals Aves as the title.
using echo basename($deep1020); i can show the scientific species name without typing it into the html.

the directory holding all bird files is named using scientific nomenclature as Aves for birds.
Thus, the title will sow Aves and i now don’t have to type the title on thousands of species pages. Thus, php codes for me.

i don’t know of a better way.
i’m not familiar with classes and i have no idea how to implement a class with my design.

here is the actual setup outside of root:

[dir named nature]
SpeciesHeader.php
SpeciesFooter.php
  [dir named Aves]
  Turdus-migratorius.php
    [dir named Genus]
    Turdidae.php

here is the actual setup in www root:

[dir named start]
  [dir named kingdoms]
    [dir named Animalia]
      [dir named Vertebrata]
        [dir named Aves]
          [dir named Turdidae]
            [dir named Turdus]
              [dir named migratorius]
               index.php loads speciesheader.php, turdusmigratorius.php and speciesfooter.php from nature directory outside of root.

php should be able to help me build pages so that i don’t have to type all of the names again. I named the folders/directories all ready.
i want to read the directory names in the www root folders from the nature folder outside of the root.
i don’t want to pass data to the files outside of root, such as query strings and i see no reason to use a form or session variables.
the backtrace method is working well for me but will it also work on a live server?
do you suggest a better and hopefully easier method?
really i think that if it works on a real server, then it is brilliant code because it saves me hours and hours of typing html. i have thousands of species pages and it is too much work to type everything.

Thank you for reading and replying.

First off, a backtrace is for debugging. If you are using it for anything else, you are using it wrong and your approach to solving a problem is wrong. As the manual clearly shows, it is an Error Handling Function.

How about telling us about the REAL problem instead of asking about your attempted approach to solving it. Seems to me you need a Database, but since I don’t know what the real problem is I cant say for sure.

I’ve decided to add a title.php file to each species folder in root. Thus, i can simply use basename etc. to acquire the titles.

the last comment really doesn’t make sense to me. The “REAL” problem? i’ve all ready explained the situation. Perhaps it is difficult to grasp or i am not wording it correctly? a database is overkill for anything other than data. I find that page titles defeats the purpose of a database. Client information and search indexes are good but html code is not a good idea to store in a database. HTML is used for webpages along with css and scripting languages. My site was designed purely using html, css and javascript (also known as dhtml since the 90s). I started studying php and trying to implement php with my current dhtml pages. This is a difficult task because dhtml requires hard coding. Thus, if i wish to show file path on each page (home -> kingdoms -> animalia -> vertebrata -> aves -> turdidae -> turdus migratorius) i will have to hard code this info. Furthermore, the actual content (images and text about Turdus migratorius) resides outside of the web root. I cannot pass this data to the templates outside of root. basename of directory does not show migratorius. basename shows the directory name for the included content.php page outside of the root. Do you understand? i cannot get the php script to read the directory names inside the root from the php template outside the root. I decided that maybe a title.php file is better than backtrace. backtrace was a bandaid but i am uncomfortable using it if it is really for debugging. I will just add a title.php page to each species directory. Then my index.php will have four includes: header.php, title.php, content.php and footer.php. I can live with this method.

Again, my site was designed as html to begin with. I cannot redesign the site as a template engine, database driven website. I don’t want that anyway. I am happy with my site for now but certain things are difficult when adding php functionality. surely you see my problem. Another example problem is relative links. I’ve had to figure out how to add the appropriate css file to each family of animalia. I am sure that a php pro will laugh at my design but it works. Alot of millionaires started out with cheap designs. To this day, MS Windows has tons of problems but they stay in business. I am not yet a php programmer but my site is working. I just have problems that require solutions from experienced programmers. I am not experienced enough with php, thus i struggle with certain aspects of site design. I still think in html.

Anyway, Thank you all for your time. I will add a title.php for now and continue trying to learn php website design.

Hello again, i’ve been thinking about this problem and i have a request for this forum:

i am injured and i just had surgery two days ago. Plus, i’ve been designing my site on a windows 10 laptop. The monitor hings burst and my monitor is blown. I have a warranty, so i am waiting for my production pc to return.

while i am waiting, perhaps it is better for me to try a php design instead of what i currently use. If i start to redesign this site with php, then i definitely need some tips.

what is the best approach to building my site with php which solves the problems that i am currently having with this site. relative links, different css files for each family, page titles, and path links on each page while still using a template? a better design might solve my stupid problems with the html design.

any suggestions?

How about providing a zip file of your site so we can see exactly what you’re working with. You can also pm it to me if you don’t want to post a public link.

If you really want to learn PHP? Something like the Yii2 Framework does most of what you are already asking for, including the breadcrumb navigation.

Thank you both for being helpful and taking time to reply. I appreciate the both of you.

I don’t mind showing you my setup, benanamen. The site is very large, so i will have to show you one small family from start to species level. However, i will need some time to extract a family. I’ve had shoulder surgery three days ago and yesterday my arm was locking. I had trouble moving my shoulder. I need to rest now. I am feeling alot of pain today.

Dear astonecipher, i think that classes are correct in this situation. I guess that i get a mental block about learning a new concept because my arm is really hurting. I’ve looked at classes and i see that they are very useful and important for a good app. I’ve all ready started playing with a class file to get a quick and dirty introduction. I realize that if i really want to learn php, then i need to learn classes. A framework lets me use someone elses work and just fill in the gaps. I told my Wife that i need to take a class about classes and be classy about it :slight_smile:

Anyway, i can send a zip to both of you once i rest my arm. I admit that i need experienced eyes to guide me in the right direction before my site leaves me feeling too big for my britches.

Sorry to hear you are in pain.

Rather than a zip, I would highly recommend you upload your application to a repository like GitHub. Not just for our sake, but for yours for version control. You can make it public or private for free. As for us, it will make it very easy for us to review your code as a whole as well as even cloning it and pushing changes to the repo for you to review without affecting your original code-base.

Hello benanamen, Thank you for the empathy and for taking time to help me. I will post at github soon. Maybe you or someone else can guide me toward a better design. One of the problems for me with nature is that the subject is very large. Taonomy is overwhelming and figuring out how to organize this info is a nightmare for me. For example, how am i supposed to structure my content in a sensible manner which also helps me code php? I decided to structure this data using Taxonomy, thus the path to the American Robin is: Animalia, Aves, Turdidae, Turdus, migratorius, index.php This way, i can separate plants, fungi and animals. I also don’tr want all bird content in the same folder because it is confusing. However, i also worry about computer issues. For example, is it true that i can only have 150 nested folders? then taxonomy could be a problem. What if i have 10,000 species of insects? i really have trouble structuring the data and linking all of it elegantly, cleanly and intelligently. I definitely need some guidance.

i will let you know when i have posted the files to github. Meantime, i need to do my physical therapy exercises. I have entered stage 1 of frozen shoulder while i have two incomplete ruptures, tendonitis and heavy inflammation. Surgery was done to help create space but i am in constant pain which makes it difficult for me to think clear enough to program php. I am struggling.

I wish for all to have a pleasant day.

You clearly need a database and perhaps also a framework to help you build this out. At the very least a router to separate your code from the URLs the user interacts with.

A very normal solution today is to route all requests through a single file

Example:
yoursite.tld/animalia/aves/turdidae/turdus/migratorius.html
yoursite.tld/animalia/aves/fringillidae/pyrrhula/pyrrhula.html

All end up at index.php
Where you load your site config, set up a db connection and start a PHP router (loads available) that take a config on routes vs functions/methods to call when that route is active.

So the ones above might be in the router as something like this (pseudo code)

(kingdom)/(class)/(family)/(genus)/(species).html
-->
AnimalController:showPage

Which will identify the currect URL as a match to this pattern and run the showPage method while automagically populating the method parameters with the data from the URL.

<?php
namespace App\Controller;

class AnimalController {

  public function showPage($kingdom, $class, $family, $genus, $species) {
    // code to load the correct species data from the database
    // then load the correct template file
  }

}

TLDR: do not worry about nesting files etc, you do not need it. You just need to separate your app logic and data from the presentation layer (URL and templates)

1 Like

@JimL is right. I also mentioned this earlier. The nested folder approach is definitely not the answer to the problem.

There are already Taxonomy databases available so it’s not like you have to build the data from scratch.

Here’s a very simple example of how a project like this could be started with a framework like Symfony.

It (and other frameworks like it) gives you the tools you need to generate the boilerplate around your code so you can focus on writing what matters to your app.

There is a lot to learn if you’re going from a hack and slash project to a full fledged framework. But I wanted to throw it out there as an example so you could have a look. Files of interest are mainly in src and templates.

Basic flow is that URLs are matched with the Route info in the controllers (AnimalController, KingdomController). The controller then use one or more repositories to fetch data, and pass the fetched data to a template file that the user then sees.

I’ve also installed an admin panel for easy editing of the db data.

Of course depending on the scope of the project, how you want it to work, etc this may not even be the correct structure for it. But I hope it still is valuable as an example.

When you’re talking about thousands of records I’d highly recommend some kind of automated job / script to insert the records into a database.

I did start writing a step by step thing while piecing this together. If you want I can post it but it got messy quickly so not sure how much value it has.

Thank you for everything, Sir Jim. I really appreciate your time and effort. I will spend time analyzing this framework that you have posted. I need to learn how it works.

Meantime, I discovered a simple answer to this backtrace problem while i was reseraching direct access to included php files. I mentioned earlier that i decided to make a title.php with the basename info for the breadcrumb links. The solution is to create he variables in the index file in the root, then use those variables in the file outside of the root. I did not know that i could do this in the included file. How stupid of me. I thought that the variables need to be in the included file, which reads directory names outside the root where the file resides. I’m surprised that noone noticed this error. I was curious, so i placed variables in an index file and included a file called title. The names of the directories now appear. Thus, the solution was to place the variables in the index.php file in the migratorius directory.

Really, i did not know that the included file could read those variables. So i assume that they are global variables by default. interesting. I have much to learn about php.

You can think of it like this

index.php

<?php
$title = 'My website';
$menu = ['Home', 'About', 'Contact'];
require 'templates/index.html.php';

templates/index.html.php

<h2><?= $title ?></h2>
<ul class="menu">
  <php foreach($menu as $item): ?>
  <li><?= $item ?></li>
  <php endforeach; ?>

Is the exact same as

$title = 'My website';
$menu = ['Home', 'About', 'Contact'];
?>
<h2><?= $title ?></h2>
<ul class="menu">
  <php foreach($menu as $item): ?>
  <li><?= $item ?></li>
  <php endforeach; ?>

PHP is executed from the top of the file to the bottom. And when including/requiring other files you are basically just inserting the content of those included files at that point in the original file.

index.php for Cardinalis cardinalis as an example of my stupidity:
i want the English names instead of taxonomical names as links with a binomial name as a title attribute

Start > Animals [Animalia] > Vertebrates [Vertebrata] > Birds [Aves] > Cardinals and Allies [Cardinalidae] > Cardinalis cardinalis - Northern Cardinal

i thought that i will read the folder names for Scientific attributes, then pass those folder names to a function with a switch to set the English name (or to similar functions for other languages)
i stored the data in title.php outside the root but the folder names were “c”, “xampp”, “nature” and “species” because my code was running in the nature folder.
i noticed that backtrace was able to read the names of the directories in htdocs where C. cardinalis index is stored but backtrace is not good programming
i didn’t think of placing the variables in the index file. i have no idea why i thought that they needed to be in the included file title.php
i should’ve checked the php manual because it clearly states this exact information.

i started tinkering with php last year.
i try to imagine how to accomplish a task, then i try to build it with php, html and css.
i really wanted one index file so all of my content links remain mysite.com but i couldn’t figure out how to do it with php
i thought that i would need forms that submit to self to change data. i’m not familiar with a controller page and php routers
all of this is new to me.

i know, TLDR material. sorry. i will stop babbling now.
Meantime, i now truly understand the concept of inclusion. Thank you very much, Sir Jim.
Also want to say Thank you to benanamen and astonecipher.

Sponsor our Newsletter | Privacy Policy | Terms of Service