I am beginning to develop a website as a personal project, to discover a little bit web technologies. My question is more about "how to make the things clean" than a technical question.
Here is the thing:
Let's say that an index.php page will include an other menu.php, which will permit to navigate the website.
My index.php look like this, basically:
include("header");
include("menu");
include("DEPENDING ON WHICH LINK AS BEEN CLICKED FROM THE MENU");
include("bottom");
To avoid the POST method, and have a lot of stuff on my URL, I would like to do it an other way, but I do not know what is the best one.
I thought about declaring a variable like $page, when a link is clicked, I can do something like "if $page == home", then I include the home.php page... etc...
I do not know if my question is clear... I know that it will appear as a very easy and beginner question but I don't even know where to look...
Do you know if I can find any "open source website" so I can study the code and see the best practices about it?
P.S.: Sorry for my english which is probably not perfect at all, I am working on it.
You can have a menu like
Home
About
Then on your PHP code
include $_GET["view"] . ".php";
Note that I am not validating, so any parameter passed on the url would be able to include any file.
The $_GET returns the values passed to the page through the URL.
The $_POST returns values posted.
The $_REQUEST returns both $_GET and $_POST values.
A good place to study many languages is W3Schools, you could check there sometime.
Make a page which will be common redirect page.
Every post will come to that page and based on the page parameter it will redirect.
So action of every page is same, but based on page paramter redirect to which ever page you want
You can switch case and
use header to redirect
i think you want to avoid GET method and avoid lot stuff in url
For learning
I thinks this is the simple website for learner.
http://www.w3schools.com/php/default.asp
http://www.plus2net.com/php_tutorial/site_map.php
http://www.tizag.com/phpT/
actually most of all these websites are same.
i hope you know the basic website PHP.net
Then,,.. no one is low level ..every low level will be in a top level one day.. just like you am also trying :)
Don't do what you are trying to do. The whole point of having pages is to handle things with different files. That is, you will have some commonality between files (handled by auto prepend and include path, potentially) such as your header and footer. Each file should include this on its own and print it out directly.
That is, you should not handle everything on one page and then conditionally include a file. Just send users to a different page.
Finally, I recommend not splitting up the header/footer files at all. Instead create a decorator that wraps the main content and displays it all at once. Something like:
$page = <<<HTML
<html><head><title></title></head>
<body>
<div id="top nav"></div>
{CONTENT}
</body>
</html>
HTML;
Then you go through and build your page content. Then you add it to CONTENT in the decorator and print it. PHPTAL is a great way to have this handled externally.
Hi you should please ask one question at a time:
I think this basic tutorial will give you a good idea on how and what to use the include(); func.
http://www.w3schools.com/php/php_includes.asp
I started with:
http://www.solitude.dk/filethingie/
Very simple .php file administrator.
You should definetly check out sourceforge, giant colletion of open source projects just filter by php (search for literally anything).
Just wanted to mention that you can download the full code of more complex pages (that are based on php) like
wordpress (blogging platform) - very easy to install and configure
identi.ca (twitter open source alternave)
You can now download reddit´s source code - quite easy to.
Maybe you wont be able to modify them immeditelly but theyll help you to get the picture
Related
I don't really know how to describe my problem properly, but anyway, here goes:
What I want to to is prevent some of my url from changing when linking between pages.
Fx:
localhost/intro/index.php
Now the "index.php" part is what I want to rename, or hide, throughout all of my pages, if possible.
An example of what I want to do:
www.cvkweb.dk/wi/doku.php
Try clicking at some of the green links at the page, and you'll notice that the "doku.php" part in the url doesen't change..
But how do I do it? .htacces?
Thank y'all in advance!
In the shown example, they don't change the requested page, because they use one and the same page... Imagine this kind of structure and code:
index.php
<p>Hello world</p>
Go to 1
Go to 2 <!-- all the anchors are referring to self -->
<?php if(isset($_GET['id']) && $_GET['id'] == '1'): ?>
<p> You have opened link number one </p>
<?php endif; ?>
<?php if(isset($_GET['id']) && $_GET['id'] == '2'): ?>
<p> You have opened link number two </p>
<?php endif; ?>
Conditionally by what is requested via the querystring, the response is controlled.
Which I would tell is bad practice, and you will end up with one single page which is full of code. Even if you do includes, and so on.
You way to go might be url rewritting, but not the way you wanted, but some routing which will map certain words from the url to certain pages. However, it will be not so hard to reverse what site.com/page/id is referring to page.php?id=1
So completely not knowing what is the requested page to the end user is nearly impossible. I would suggest to drop this desire. And maybe google about single page applications
You don't need to touch the server configuration.
Just link to the URL you want to link to.
Then write code in index.php to read $_GET and return different content based on the values of your query string.
URL rewriting can't help you there. The browser will always display the URL of the page you're currently on in the address bar. Your server will return data based on the requested URL. Therefore: page == URL. You can't display different pages with identical URLs.
However, you can change the content of a page client side without needing to load a new page. This can be done by embedding HTML frames/iframes in the page, or by using Javascript to get content from different URLs and placing it in the current page.
The page in your example links to completely different URLs, it distinguishes pages based on the query parameter (?...). That's a different URL. The PHP script returns different content based on it. No URL rewriting involved at all.
You should read about $_GET method.
PHP $_GET Method
You can pass a parameter via URL and based on what parameter you pass, display a certain content.
Your question is very confused. 1Index.php` is a special case - it's usually defined as an option in the DirectoryIndex config on an Apache installation. But you are not asking about index.php. You don't say what webserver you are using. Looking at the page you linked, it is clearly a Dokuwiki installation.
There are very detailled instructions on how to implement URL re-writing in the Dokuwiki manual
I want to achieve something like Facebook, where the top bar stays there and only the content underneath it changes for different pages. At the same time the URL at the top also changes, for example /messages or /events.
I'm not completely sure if this is how Facebook works but I'm trying to achieve something like this... Right now I can't figure out how this could be done...
Does anyone know how this could be achieved or if there is a name for it?
You could do this with a fixed header that is on all of your current pages, so that whenever a new page loads it appears the header is still there, or you could do the more complex way of putting all your pages in separate divs on one page, and hiding/showing the appropriate divs when a link is clicked. I'd recommend the first way personally, but those are some options.
I'm sure there are other ways to go about this.
If that isn't what you want then I apologize.
Edit: Also, my first suggestion will load a whole new page, so if you want the seamless transition effect then the second option will be the better route to go
Templates perhaps? You can't change urls without navigating to a different page as far as I know. If you want consistent elements on the page with varying content that basically is the function of templates.
If you're looking for dynamic elements to show up on the page then you should look into AJAX and DOM manipulation. jQuery is the defacto library to help with both of those. Take a look at http://api.jquery.com/ and maybe search for a template engine. Most web frameworks include a template system to create consistent styles and allow code reuse.
You can use #anchors, that won't reload the page. For newer browsers, you can use https://developer.mozilla.org/en-US/docs/Web/Guide/DOM/Manipulating_the_browser_history to update the url without reloading the page
I'm starting with "PrestaShop" and I just can't figure out, how to put a link in template to custom page I created in CMS module... I thought, there might be some easy way, as there is in WordPress, like "get_permalink(ID)", but there's nothing like this and I can't find anything about this anywhere and it just drives me mad.
So, here's the deal, I've got a custom template, and there are some top links, like "About Us". I've created this page in CMS and it has ID "6".
How do I make this bloody "PrestaShop" to generate a link to this page in my template file?
About
I think you're looking for SMARTY template tags and custom variables defined for Prestashop specifically. The one the you probably need is {$base_dir} which will be translated to http://www.yoursite.com/ obviously with appropriate protocol (non-secure HTTP or secured HTTPS).
After that, you only need to include page URL, which you can get from Admin->Tools->CMS section.
If I find any specific tags that you can use to call the content, I will update my post here.
Your Text
the "WHAT goes here" is just the url you want your link drives the client to when clicking on that link.
You need to understand a little minimum of HTML for this I guess. Check html tag a meaning .
Prestashop has a fairly good and extensive documentation. Two and half seconds googling drives me here, just like answering your question it looks like.
One of my client has an old osCommerce website and while working on it I have to implement what I would call "custom php page", i.e. a page which query a MySQL table, not related to osCommerce, and list the result. I'm not sure of the version, this trick I have seen a lot didn't gave me any result : http://www.clubosc.com/how-to-know-what-version-of-oscommerce-you-are-using.html . And I'm having a hard time doing this seemingly simple task, since osCommerce doesn't allow any php code in the page creation, and I didn't find any module giving me this possibility (not that it is easy to search in this mess : http://addons.oscommerce.com/). At this point I figured it would be easier to just hack'n slash through the code and come up with a custom page :
I copied the index.php (the entry point in the application) :
<?php
require('includes/application_top.php');
if(!$smarty->is_cached($sContentPage, $sCachingGroup)) {
//we switch on the content recognition
require('includes/pages/' . $sContentClass . '.php');
}
$smarty->display($sContentPage, $sCachingGroup);
require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
Here I gave a specific value to $sContentClass (with or without the if makes no difference) and customize the corresponding PHP file so it show my custom content but also initialize the same variable than those other PHP file in the pages/ folder. But alas, all of this curious and dubious code simply return me the home page. So here I am, is there an osCommerce Guru around here, or would anyone has a better idea (oh and I also posted on the osCommerce forum, but I'm still waiting for a response...)? Thanks a lot in advance.
I'm not sure, that I understand You correctly, but can't You just put Your code in this file and call it?
At the beginning I suggest leave requireing application_top and application_bootom, just put Your code here and let now if it displays Your content. Maybe in application_top is code that redirect to index.php if it not found... something specific somewhere :) It's hard to say without view of Your application_top.php
I commonly run into a scenario where "the powers that be" want an exact copy of a page in multiple places on a website. Rather than actually duplicate the content, all I do is override the section in the nav that is highlighted, and then include the page. The final page looks something like this:
<?php
$top_nav_item_id = 'teen';
include('../interests/teacher-resources.php');
?>
This typically works. I am trying to duplicate this for a blog category, done in wordpress. All I seem to get is a blank page, no matter what I do. I've tried all of the following lines:
<?php
include('../blog/index.php');
include('../blog/type/teen/index.php');
include('../blog/type/teen/');
include('../blog/');
?>
Does anyone have any ideas? Is this a URL rewriting thing? Do I have to include the template file for that particular category?
Any help is appreciated.
PHP include expects files, not URLs, so it doesn't have access to the URL namespace exposed by WordPress. Those files don't exist on-disk; mod_rewrite first turns the pretty URLs into an internal request to index.php, WordPress figures out what you really wanted based on the original URL, fetches a bunch of stuff from the database, then produces the page.
This is a pretty complicated topic, and one that isn't very apparent from the start. This page should help you get started. The key is to include the WordPress blog header - explained on the linked page. You'll probably also want to check out the WordPress Codex for resources on using the WordPress engine's API.
ini_set('display_errors', true);
error_reporting(E_ALL);
No idea what's going wrong, but it does. Maybe Wordpress can't find it's environment, maybe some variables are being overrided... Actually it's a bad idea to include solutions like wordpress, because you never know, what global variables, functions, classes will intersect.
PS: And, by the way, include uses file system paths but not URLs.
For similar issues I use iframes to include the copy of the content. You can write the original page to look for an "?embed=1" flag in the url, and only include the embeddable content in the main page when the embed flag is present (so you can leave out toolbars and frames that would be redundant.) So the iframe src url would use the ?embed=1 tag to embed the content.
This solution is a bit of a hack, but then, the problem is a bit of a hack to begin with.
I received a good explanation of why I couldn't include the blog page, but not any alternatives that would work for me.
My final solution was to modify the category template for that page directly. As stated originally, I use $top_nav_item_id to control which menu item is highlighted in the nav, to give the appearance of the page belonging to that section. Rather than override this, I simply made it conditional on a query string. As long as the user is following legit links on my site, they will get the correct query string and have no problems.
$_POST is disabled in Wordpress. $query_string (built into WP) uses some sort of caching, and would always display as it was first loaded.
Final solution:
if(strtolower($_SERVER['QUERY_STRING'])=='display=teen') {
$top_nav_item_id = 'teen';
} else {
$top_nav_item_id = 'programs';
}
Thanks to all who tried to help.