View Full Version : php dynamic page hyperlinks nightmare
MCForty
09-21-2012, 08:38 PM
Recently decided to change my website platform from html to php mysql (got fed up with having to manually update the site).
After studying tutorials on building dynamic websites, I came across one which I thought I would use as a framework, thus: I have an index.php in the root which calls upon a header.php, a "content/.$page.".php" (for displaying the body content of the respective individual pages, then finally the footer.php - all these files are also in the root.
A folder called 'content' which is in the root contains the respective individual pages: about; news; contact; home etc. and when I run them from the index.php, everything's fine. They all display their respective content within the context of the header and footer as they should.
My problem comes when i try to display an iframe link on the 'news' page to an image scroller with is in a subfolder located in 'content/slides/multifocus/index.php' It's not displaying in the news page, yet it displays on it's own outside of the news page if I view it in live view - and it displays in the news page when I browse the news page but then I don't have the header, footer or the styles that emanate from the header.
When I tried to transfer the relative files from my local host to the remote server to test it out in a real situation it emitted the 404 error, page not found.
I don't know how to resolve this although I have an inkling it has something to do with Absolute or Relative paths. I'm hoping someone will be able to help me because the way my html website was set up I had loads of subdirectories within subdirectories and if I can't replicate the html site in php, then I'm really going to be stumped - I've even thought about creating respective pages instead of having the 'includes' functionality but that would just defeat the whole purpose.
djr33
09-21-2012, 09:54 PM
That sounds complicated and like something I'd need to look at in detail to figure out.
But here's somewhere to start. If you're including the same file possibly into many files, then that can cause conflicts with paths.
My solution of choice is the following. It takes a little more time to type, but it works more smoothly.
include(dirname(__FILE__).'/relative/path/to/my/file.php');
The __FILE__ magic constant contains the absolute path and name of the current file, even inside an included page. That will always be accurate.
dirname() just gets the directory name from __FILE__, so it's basically a way to get the current location.
Then you can add any relative path to it and never worry about where it's included from.
Note that __DIR__ is equivalent to dirname(__FILE__), but only in PHP 5.3+.
In short, this does use absolute paths, but it allows you to use relative paths. (It also makes it portable later if you want to move it [the whole group of files] to another location on your server, or to move everything to a new server.)
Does that help at all?
MCForty
09-21-2012, 10:41 PM
I'm trying it now but I'm having trouble in putting that example file path that you gave '/relative/path/to/my/file.php' into an iframe.
Imagine that the file I was trying to connect to was an image file, how would I write the img src into the string. Would it be include(dirname(__FILE__).'<img src="relative/path/to/my/file.jpg"'>; ? I tried that and it didn't work because I think the string is php and not html and I must have the syntax wrong, I don't know.
But just so you know, the file that I'm trying to connect to is not a document file, it will either be an image or some sort of scrolling gallery that has to be displayed in an 'img src' tag or an iframe.
djr33
09-22-2012, 02:19 AM
Ok, I think I misunderstood. You're talking about URLs here, not paths to include the files themselves?
If that's the case, then you can give us a link to the page and we can look at the source code to help you figure it out (but not if it's something inside PHP, like includes).
My solution above is specifically for include() within the server, not for anything related to URLs for the visitors. For that, the easiest thing to do consistently is to use the "/" shortcut, so if you type "/myfolder" that will always go to mysite.com/myfolder, regardless of where you start.
MCForty
09-22-2012, 04:43 AM
Hi djr33
Thanks for all your help. I finally figured it out after nearly 24 hours, I was simply missing out a prefix from the path to the url. I was typing <iframe = slides/multifocus/index.php when it should have been <iframe = content/slides/multifocus/index.php.
This is twice now in the last week that an omitted word has cost me 24 hours, previously I was missing and 'else' statement on an isset function. However, everytime I resolve a problem another crops up.
Essentially I'm structuring a news page which kind of mimics a blog site. The current blog or article being displayed in a primary position on the page, with an archive and a recent posts column on the same page. When someone clicks either the recent posts or the archive posts, they will replace the previously displayed story - and that previously displayed story will remain on the page in the recent posts position.
Now I did have this working like a dream but as soon as I resolved my earlier issue, it's stopped working. Instead of a recent post being displayed on the same page replacing the previous one, it now creates it's own proprietary link to a new page altogether - I'm so frustrated right now I can't even tell you.
I don't think it's going to work in the framework of php includes (which is a shame because I quite liked the idea of having to only update one header) but it seems now that I'm going to have to go retro and create seperate pages for the php site. The existing html site already has over 15 pages and growing - and as well as manually updating that static site with news I have to change the header on each page if I make a change - or manually link the 15-or-so pages if I create a new page. You can view the html site at http://www.radiosandwell.co.uk If you go on to the news or sports page you can get an idea of what I'm trying to achieve. You'll see stories that are out of date because I physically am not able to be updating a story, taking the one that was there before and creating a secondary spot for it in an archive - that's why I wanted to go the php database route so that when I update the story, the previous ones are still displaying, first in the recent posts, then remain indelibly in the archive.
But thanks for your responses anyway, I do appreciate it.
djr33
09-22-2012, 05:16 AM
Hm. I would recommend working out how to use PHP for this. There are several ways to approach it, such as including the header into each page, or using a template (and including the page into the template), or using some sort of CMS (content management system) that will do all of this for you (with less control over certain things)-- some examples include wordpress and joomla.
As you're learning all of this, it might not be productive to use it on a real/active site (although that can also be a good motivator), but it's certainly something that can work out for what you're describing.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.