Bluey Zarzoff Posted April 10, 2009 Share Posted April 10, 2009 Hello all. I am a relative newbie at php as the following thread will prove. I am trying to set up a small, simple website without using a database, about 10-15 pages. I want to set it up so that the page with the layout is basically a common file throughout the site. The way I thought of achieving this was to set up the layout and when it came to the main content, I would simply call the following: <?php include('inc/content.php'); ?> I am then looking at a file with the content to load depending on the page selected in the nav menu. So, in inc/content.php I have tried the following bit of code <?php if ('page2.php') { include ('inc/page2_content.php'); } elseif ('page3.php') { include ('inc/page3_content.php'); } else { include ('inc/index_content.php'); } ?> page2.php and page3.php are the anchors for the nav area of course. Obviously this is wrong. All I get is the result for page 2 no matter which page I go to, so I guess my if/elseif statement is not working. I have also toyed with the idea of using the $_GET statement but could not find anything that would help me with this in the book I have. I have looked at a couple of scripts for code examples but they all referred to getting info from a database rather than a flat file reference. If anyone could steer me in the right direction, or suggest a good tutorial for this it would be much appreciated. One last thing if you have the time. I also toyed with the idea of creating the content within a variable and then calling that variable on the template page. This idea would help in creating different meta data for each page which could then be written on the content pages. For instance, create the following variables within the inc/_content.php files:- $page_title, $page_description, $page_content, etc. This variable would have to be the same for all pages and the content of that variable would change depending upon which inc/_content.php file is called. Is this possible. I know it is way above my very basic knowledge at the moment. Another Thought. Would it be easier to set up a template and then just create the content files calling the template? How would this be done. Sorry for the silly questions. I am only just learning coding. Thanks all. Quote Link to comment https://forums.phpfreaks.com/topic/153418-solved-content-depending-on-page-selected/ Share on other sites More sharing options...
R4nk3d Posted April 10, 2009 Share Posted April 10, 2009 use if($_GET["page"] == "page2.php") include ('inc/page2_content.php'); do the same for the rest but on the last one use: if(!isset($_GET["page"])) include('inc/index_content.php'); hope i helped Quote Link to comment https://forums.phpfreaks.com/topic/153418-solved-content-depending-on-page-selected/#findComment-806048 Share on other sites More sharing options...
.josh Posted April 10, 2009 Share Posted April 10, 2009 all you have in your conditions are strings. So the only thing that is being evaluated is that it is a string. It evaluates true because it's just there. You would actually want to compare the string to something. Like for instance, if you have a link that passes a var=value, you would compare it to that. For example: <a href="somepage.php?id=blah">blah</a> and then when you click on it, on sompage.php, you would do: if ($_GET['id'] == 'blah') { // do something } Quote Link to comment https://forums.phpfreaks.com/topic/153418-solved-content-depending-on-page-selected/#findComment-806049 Share on other sites More sharing options...
Bluey Zarzoff Posted April 10, 2009 Author Share Posted April 10, 2009 Thanks for the quick replies. Tried R4nk3d's suggestion and got the index page content throughout but with syntax errors for the earlier bit of code: 'Undefined index: page in in test/content.php on line 3'. Crayon Violent - thanks also. Were you referring to my lame attempt or a suggestion for R4nk3d. Will take this in and see how it all goes. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/153418-solved-content-depending-on-page-selected/#findComment-806067 Share on other sites More sharing options...
Bluey Zarzoff Posted April 10, 2009 Author Share Posted April 10, 2009 <a href="somepage.php?id=blah">blah</a> and then when you click on it, on sompage.php, you would do: if ($_GET['id'] == 'blah') { // do something } Thanks mate. That is working fine. So, in effect, I only need 1 file with the layout. Perfect. The only thing that I can see having a problem with at the moment is the fact the all pages will have a common title, description and keywords. Will have to look into using a variable that will show the correct content when a particular file is called. If anyone has any suggestions it would be much appreciated. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/153418-solved-content-depending-on-page-selected/#findComment-806093 Share on other sites More sharing options...
.josh Posted April 10, 2009 Share Posted April 10, 2009 Crayon Violent - thanks also. Were you referring to my lame attempt or a suggestion for R4nk3d. R4nk3d posted his suggestion when I was typing. His suggestion was along the same lines of mine, but I went ahead and posted anyway because I felt mine explained it a bit further. As far as your pages having similar titles, etc... that really depends on how you have it setup. Is it currently hardcoded onto each page, and you're wanting to be able to swap it out, depending on which page is loaded? What you would do is have a list of the info saved somewhere with the pagename (or an id) associated with each piece of data, and assign it to the var, based on that. You can store the information in an array hardcoded on your controller page (the page that is loading this stuff up), store it in a flatfile, or put the info into a database. Harcoding it on your controller page is the easiest to implement/explain, so I'll use that as an example for swapping your title out. But first, in order to make this efficient, you're gonna wanna back up to the actual page loading and write it a bit differently. Let's say you have 3 "pages" for your site: Home, About, and Contact. For the links, you would still pass a val=var, but you will want to pass an id specific to each page. You can make it anything you want, as long as its unique for each page. For simplicity, we'll just assign the name of the pages as the id's. So your navbar links would look something like this: <a href="somepage.php?id=home">Home</a> <a href="somepage.php?id=about">About</a> <a href="somepage.php?id=contact">Contact</a> somepage.php is your controller page. You can hardcode it, make the pathing relative, just leave it blank with the href saying "?id=3", or use $_SERVER['PHP_SELF'] (the goal is to reload the same page but pass the id so it knows what content to display). On somepage.php, you would look for the passed id, displaying relevant content (or displaying default content, if no id exists or if someone were to try and pass something other than an existing id number). So overall, your code would look something like this: <?php // listing of pages $allowed = array('home','about','contact'); // default page to display $default = $allowed[0]; // listing of content $content['home'] = array('title' => 'HOME PAGE','description' => 'some description about the home page'); $content['about'] = array('title' => 'ABOUT US','description' => 'some description about the about page'); $content['contact'] = array('title' => 'CONTACT US','description' => 'some description about the contact page'); // check to see if the id being passed by the link is in the list. If it is, assign it to $id. // if it is not (because it doesn't exist (first time page load), user changed it, etc..) , assign the default $id = (in_array($_GET['id'],$allowed))? $_GET['id'] : $default; // include the page include ($id . ".php"); ?> And then on for instance, the home.php that you are including: <?php echo "Title: " . $content[$id]['title'] . "<br/>"; echo "description: " . $content[$id]['description']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/153418-solved-content-depending-on-page-selected/#findComment-806570 Share on other sites More sharing options...
Bluey Zarzoff Posted April 11, 2009 Author Share Posted April 11, 2009 Thanks for all the time spent Violent Crayon. I was working on this section too. My book actually has some stuff about that. And I have found some code examples. I am going with flat files in this instance. NOW. I am still having problems with the $_GET. Everything works fine when I click on a link that has been anchored, particularly when going to links and contact us in the code example below. The problem I am getting is when I want to specify a default file to show (particularly when the site is first visited and no file is specified, you know, http://www.mysite.com/). The text in the default file is showing but I am getting an error message for each line of the $_GET variables above it. I have tried R4nk3d's (thanks by the way) suggestion for the default file, and also I tried another way as per a script I found that is using the exact same method to get the content files. This is the message I get " Notice: Undefined index: page in C:\wamp\www\test\inc\content.php on line 3 Notice: Undefined index: page in C:\wamp\www\test\inc\content.php on line 7 This will be the new home page file. Hope like heck this works ok. " The last line is the contents of the home.php file This is the content.php file that I have created <?php if($_GET['page'] == 'links') { include ('content/links.php'); } else if($_GET['page'] == 'contact_us') { include ('content/contact_us.php'); } else if(!isset($_GET["page"])) {include('content/home.php');} ?> <!-- else { include ('content/home.php'); } --> This was my latest attempt. I have tried a few other ideas as well in the last statement, including just using else, removing parentesises, etc. The commented out bit of code is what the other script used that worked for them. The lines in the error message relates to the if and else if statements. What stupid mistake am I making. I have been searching for an answer, promise. To recap what I am doing at the moment. This is in the index.php file <div id="centre"> <?php include ('inc/content.php'); ?> </div> This is how I am setting up the anchors <a href="<?php echo $site_url; ?>index.php?page=links">Links</a> (The trailing slash is in the $site_url variable) The code in the inc/content.php file is what is showing above, and the content files are simply a bit of text. Sorry to be a pain in the bum. I am stuck at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/153418-solved-content-depending-on-page-selected/#findComment-806966 Share on other sites More sharing options...
.josh Posted April 11, 2009 Share Posted April 11, 2009 The problem is that you have your error reporting setting set to show every error message, even notices. Notices don't really break your code. All it's saying is that you are trying to make use of a variable that hasn't been previously assigned or declared. PHP is okay with that, but it tells you nonetheless, just as an FYI. The best way to remove them is to reorder your condition. Something like this: if (!isset($_GET['page']) { include('content/home.php'); } elseif ($_GET['page'] == 'links') { etc... That way the other conditions will only get evaluated if it's actually set. Quote Link to comment https://forums.phpfreaks.com/topic/153418-solved-content-depending-on-page-selected/#findComment-807003 Share on other sites More sharing options...
Bluey Zarzoff Posted April 11, 2009 Author Share Posted April 11, 2009 Damn. I didn't realise about the error reporting. Don't usually have it this way. Have just upgraded my test server to the latest version and it must have been set by default. Now all I have to do is remember how to change it, think it is in php.ini. I guess that your latest suggestion is the strictest method so I might as well go with that. Oh well. Thanks so much again. Sorry for taking up your time. Quote Link to comment https://forums.phpfreaks.com/topic/153418-solved-content-depending-on-page-selected/#findComment-807017 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.