Jump to content

Php And Reading From Other Files ?


Lustat
 Share

Recommended Posts

ok I have to admit I'm completely lost. I'm going through tutorials and trying to learn php without much luck at the moment, hopefully that will change soon tho :) I'm trying to create a website that will have 3 main layouts. I've already done a css file for each layout so thats all done. I'm just stuck on the php part. basicly I'll have 1 layout shared for all states but the menus and body info changes depending on which state you chose. from there, you can go in one of 2 other sub categories (layouts) for that state. and depending on which one you select that one will have it's own menu and information. is there a way to create this ? and if so how ? I'm figuring I'll have one huge html file that each state will pull different sections of and insert it into either menu or body ? any and all help is appreciated... it's 3:30am and I'm just stuck... partly continuing on tutorials, partly ripping the last three hairs out of my head

Link to comment
Share on other sites

A simple if >> array would do. Array should contain d states... Depending on d state selected... You can include() d state's menu & info... So each state, a different menu & info... Hpe i helped!! Pls rply... :-)
problem is that the menu is in a different div.so if I understand this correctly. the main index.php file will be.
<div id="body"><?php @ require_once ("states.php"); ?></div><div id="menu"><?php @ require_once ("statemenu.php"); ?></div>

the file called would be

<?php if ($state="NY")$state = file_get_contents ("/statesindex.txt", "r" $ny=array("$nyBody"));echo $ny;if ($state="GA")$state = file_get_contents ("/statesindex.txt", "r" $ga=array("$gaBody"));echo $ga;// and so on for each state?>

and the statesindex.txt will have

$ny=array("blah blah blah about 3 to 5 paragraphs fit in here")$ga=array("blah blah blah about 3-5 paragraphs fit in here">

I think it's safe to say I've been reading too many tutorials and I'm still stuck :) and I just cant grasp php to save my life

Link to comment
Share on other sites

Your site could benefit from using a database, if you could store all of the information about each state in the database then you could have one page to display the information about whatever state you tell it to.You could also set up a series of include files. I'm not quite clear about the layout of your site, but you could have one main page which would include other files based on what state was chosen.How much stuff changes? Does each state have a separate menu structure? What parts are shared between all of the states?

Link to comment
Share on other sites

Your site could benefit from using a database, if you could store all of the information about each state in the database then you could have one page to display the information about whatever state you tell it to.You could also set up a series of include files. I'm not quite clear about the layout of your site, but you could have one main page which would include other files based on what state was chosen.How much stuff changes? Does each state have a separate menu structure? What parts are shared between all of the states?
thanks for the reply, my provider offers mySQL but that is already configured for wordpress. I think I can split it and use it for this as well tho. this is the code I have for the states page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Performance Shop Reviews.com</title><meta http-equiv="Content-Language" content="English" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link rel="stylesheet" type="text/css" href="states.css" media="screen" /></head><body><div id="wrap"><div id="header"></div><div id="content"><div class="menu"> <ul><h2>Colleges</h2><ul><li><a href="Texas College List">college 1</a></li> <li><a href="Texas College Nearby Libraries">College Libraries</a></li> <li><a href="Texas College Restaurants">Restaurants</a></li> </ul></div><div class="right"> <center><h2>Texas Colleges and Universities</h2></center><div class="articles">Welcome to Texas University and College Search.<br><br>On the left you will find a list of Universities and on the right list of Colleges</div></div><div class="left"> <h2>Texas Universities</h2><ul><li><a href="Texas University">Universities</a></li> <li><a href="Texas University Libraries">Libraries</a></li> <li><a href="Texas University Restaurants">Restaurants</a></li> </ul></div><div style="clear: both;"> </div></div><div id="bottom"> </div><div id="footer">footer goes here.</div></div></body></html>

The layout is set to have a header, than 3 columns, and than a footer. if they select things from the university tab, than it'll go to another css file that will change the header, drop the college menu, and extend the body to fill in the space where colleges were at.and if they pick colleges, it drops the uni menu, changes header, and again extends body to fill in the space where uni menu was at.I hope this makes some sense ?

Link to comment
Share on other sites

So you could have the 2 menus in different include files, and then the page would need a way to figure out which of the files to include.You could use a couple variables in the querystring for that. You could have one for the state, and one for the section. You would probably need another for the item in the section. So the link to the Texas page may look like this:<a href="states.php?state=tx">Since there's no section, then it would just show the general Texas page and include the other two menus. Your link to a college may look like this:<a href="states.php?state=tx&section=college&item=college_1">college 1</a>So notice they all link to the same page, but the variables you pass in the URL tell that page what to show. You could have a series of include files which you include based on the querystring variables, or you could use those to look up information in a database. If you're just starting, a series of include files may be easier to set up, but more difficult to maintain in the long run. Databases are generally easier to maintain.To get the values in the querystring, you can use code like this to check the $_GET array:

$state = isset($_GET['state']) ? $_GET['state'] : '';$section = isset($_GET['section']) ? $_GET['section'] : '';$item = isset($_GET['item']) ? $_GET['item'] : '';

A line like this:$state = isset($_GET['state']) ? $_GET['state'] : '';is a shortcut for this:

if (isset($_GET['state']))  $state = $_GET['state'];else  $state = '';

Link to comment
Share on other sites

this is the code I have right now for selecting a state using a quick form.

<html><head><script language="JavaScript">function loadPage(list) {  location.href=list.options[list.selectedIndex].value}</script></head> <body><?php $state = isset($_GET['state']) ? $_GET['state'] : '';$section = isset($_GET['section']) ? $_GET['section'] : '';$item = isset($_GET['item']) ? $_GET['item'] : '';											   												  ?><form><select name="file" size="1"  onchange="loadPage(this.form.elements[0])"  target="_parent._top"  onmouseclick="this.focus()"  style="background-color:#ffffff">	  <option value>Select State</option>	  <option value> </option>	  <option value="http://mysite.com/states.php?state=al">Alabama</option>	  <option value="http://mysite.com/states.php?state=ak">Alaska</option>	  <option value="http://mysite.com/states.php?state=az">Arizona</option>	</select></form></html>

so if I comprehend this correctly, I can just use another file that will have an array for lets say all the text for the body of a state ?so than

$state = isset($_GET['state']) ? $_GET['state'] : '';

that will look for a file called state and within it it will look for the array that is =ak for alaska for example ? I dont really see how

<a href="states.php?state=ak&section=college&item=college_1">colleges</a>

would work...would the section=college call another array to change the body to my list of colleges ? and than item=college_1 would be to change the menu ? from what I'm reading isset basicly looks to see if a state has been selected, if it has than it'll just keep the current page, while if it wasnt set than it'll set it to whatever the link info said ?I hope I'm not too confused... I'm just a true beginner that understands nothing. I'm still wondering how I managed to understand css

Link to comment
Share on other sites

This is all just data, nothing is going to happen automatically unless you make it happen.For the code you have for your form, you've generally got things in the right places. Your PHP code there is getting the values from the URL. So, it creates variables called $state, $section, and $item. The first time you load the form, all three of those variables are set to an empty string (a string with no characters). If you pick a state, say Alabama, then it would redirect to this URL:http://mysite.com/states.php?state=alSo when the page loads that time, the $state variable will be set to "al" and the other two variables will still be empty. So now the state variable is set to what they picked, so you can use that information to hide parts of the page (you could hide the form with the dropdown, for example), or include content for that state, or whatever else you want to do. You could hide the form, for example, with an if statement to only show the form if the state is empty:

<?php $state = isset($_GET['state']) ? $_GET['state'] : '';$section = isset($_GET['section']) ? $_GET['section'] : '';$item = isset($_GET['item']) ? $_GET['item'] : '';											   if ($state == ''){												  ?><form><select name="file" size="1"  onchange="loadPage(this.form.elements[0])"  target="_parent._top"  onmouseclick="this.focus()"  style="background-color:#ffffff">	  <option value>Select State</option>	  <option value> </option>	  <option value="http://mysite.com/states.php?state=al">Alabama</option>	  <option value="http://mysite.com/states.php?state=ak">Alaska</option>	  <option value="http://mysite.com/states.php?state=az">Arizona</option>	</select></form><?php}else{  echo 'You selected ' . $state;}?>

Or you could use that variable to include a file with the content for that state on it, e.g.:include 'states/content_' . $state . '.php';So that would include a file called states/content_al.php, for example. You could have other code on the include file to check for the section or item variables and include other files, like if the section was 'colleges' then the state include file could include the Alabama colleges include file.So it's not going to do anything automatically, this is just passing data around to your pages so that your pages can figure out what to do based on that data.

Link to comment
Share on other sites

This is all just data, nothing is going to happen automatically unless you make it happen.For the code you have for your form, you've generally got things in the right places. Your PHP code there is getting the values from the URL. So, it creates variables called $state, $section, and $item. The first time you load the form, all three of those variables are set to an empty string (a string with no characters). If you pick a state, say Alabama, then it would redirect to this URL:http://mysite.com/states.php?state=alSo when the page loads that time, the $state variable will be set to "al" and the other two variables will still be empty. So now the state variable is set to what they picked, so you can use that information to hide parts of the page (you could hide the form with the dropdown, for example), or include content for that state, or whatever else you want to do. You could hide the form, for example, with an if statement to only show the form if the state is empty:
<?php $state = isset($_GET['state']) ? $_GET['state'] : '';$section = isset($_GET['section']) ? $_GET['section'] : '';$item = isset($_GET['item']) ? $_GET['item'] : '';											   if ($state == ''){												  ?><form><select name="file" size="1"  onchange="loadPage(this.form.elements[0])"  target="_parent._top"  onmouseclick="this.focus()"  style="background-color:#ffffff">	  <option value>Select State</option>	  <option value> </option>	  <option value="http://mysite.com/states.php?state=al">Alabama</option>	  <option value="http://mysite.com/states.php?state=ak">Alaska</option>	  <option value="http://mysite.com/states.php?state=az">Arizona</option>	</select></form><?php}else{  echo 'You selected ' . $state;}?>

Or you could use that variable to include a file with the content for that state on it, e.g.:include 'states/content_' . $state . '.php';So that would include a file called states/content_al.php, for example. You could have other code on the include file to check for the section or item variables and include other files, like if the section was 'colleges' then the state include file could include the Alabama colleges include file.So it's not going to do anything automatically, this is just passing data around to your pages so that your pages can figure out what to do based on that data.

Justsomeguy thank you, You're a HUGE help :)I'm still doing tutorials from w3schools and also tizag. between those two and your replies, I think I'm starting to understand this. hopefully I wont have any questions at least for a day or two, I'll be keeping busy with the tutorials and trying to learn as much as I can. again, THANK YOU
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...