Jump to content

PHP to create HTML pages and populate key words?


paulmo

Recommended Posts

I've got a website with a dropdown form that links to 3 states, and I want to expand the site to all 50 states without having to manually create each page, and without manually editing things like the header name--Vermont, Oregon etc. and certain paragraph text that is exclusive to each state. For example, "Cactii grows in Arizona." "Pineapples grow in Hawaii." I have a feeling there is a way to do this in PHP. Direction please? Thanks in advance.

Link to comment
Share on other sites

Absolutely. You'll need a kind of template page that contains spots where you echo the information appropriate for each state. I suggest designing it like a plain HTML page, with the actual data for one particular state. When it looks good, pull out the real data and replace it with some PHP. The main PHP script might work best coming before your HTML. Collect your data there and assign it to global variables that you can simply echo inside very small PHP scripts embedded in the HTML below.The other half of the package is deciding how to store your data. If you know SQL already, then simply build a database. If you don't then you have to decide whether you want to learn that skillset at the same time you're learning PHP.The alternative is to store your data in a "flat file" -- Each state's data would be written into a unique line of a text file. A line here is defined as a bunch of text terminated by a newline character. Individual fields (state name, plant, motto, etc) should be separated by a unique character, like a comma or a tab. If any of your text fields might contain a comma, then a tab is probably the way to go.To access this data you'll need to get a little familiar with file system functions. The easiest is file($path_to_file), which reads the file into an array of strings that were originally separated by a newline character. See how it all fits together? Then you can search for the correct line, explode it into an array of fields, and plug each array element where it needs to go.

Link to comment
Share on other sites

Thanks DD, I'll go with the template/MySQL option. I already use basic MySQL insert and select statements. How to make actual pages for each state from the "master" file, and then coordinate those pages with the data inserts? This doesn't have anything to do with OO does it?

Link to comment
Share on other sites

Thanks DD, I'll go with the template/MySQL option. I already use basic MySQL insert and select statements. How to make actual pages for each state from the "master" file, and then coordinate those pages with the data inserts? This doesn't have anything to do with OO does it?
No OO required. I would make all 50 states link to the same page and just send the state name via POST or GET and use that to retrieve relevant data to print on the page.
Link to comment
Share on other sites

not really. When DD suggested you make the master file for one state, it would show you where the template would eventually need to be customized per state. For instance, you may want the header to have the States name, a paragraph to be the description, and then some statistics about the state in a column on the right (like size, population, state bird, flag, etc). Anything that would change from state to state is what you would want to have PHP 'plug-in' as opposed to being hardcoded. So at the top of your page, you would query the DB based on user input (say they selected, oh, Rhode Island), your query would search the DB for a row in the state table whose state name is equal to 'Rhode Island', and then get all the information you've saved for it. Then you could assign all that data to 'pluggable' variables to use within the HTML part of the page, i.e.

$state = mysql_real_escape_string($_POST['state']);  //in this case 'state' = "Rhode Island"$stateInfo = mysql_query("SELECT * FROM states WHERE stateName='$state'"); while($info = mysql_fetch_array($stateInfo)){   $state_capital = $info['capital'];   $description = $info['description'];   $state_nick = $info['nickname'];   $state_bird = $info['bird'];   //etc};[code]and then in your HTML, you would have something like this:[code]//Typical HTML tags to start a page, i.e. <html><head></head><body><h1> <?php echo $state . " - " $state_nick ?> </h1><p><?php echo $description ?></p>...

this is just an example, you may have your DB setup a little differently, but that's the gist of it. And just to clarify, this would all be one page. It's just easier to put the PHP at the beginning for maintenance sake, and then just litter the variables throughout the content part of the document (your HTML), which will make it more readable. And if you have to make any changes to the DB or what have you, all you have to do is change the stuff at the beginning, and how the variables are assigned, and then you don't have to go combing through the HTML document looking for changes you may have to make based on those changes.

Link to comment
Share on other sites

Now how to create the 50 .php pages without opening 50 new files in my text editor, saving, and uploading all 50 to host??
Umm...No need for 50 pages.
I would make all 50 states link to the same page and just send the state name via POST or GET and use that to retrieve relevant data to print on the page.
$state = mysql_real_escape_string($_POST['state']);.....

.....And just to clarify, this would all be one page......

Just use POST or GET like thescientist did in his code he provided (he used POST) and submit it to a single PHP script.
Link to comment
Share on other sites

OK I think I got it. So the form (all states in dropdown) action goes to index.php and then do the db query based on the state name variable, calling all that's state's data like Scientist shows in his code, then plug in the variables as needed in the page. That is just too cool. Thanks guys.

Link to comment
Share on other sites

OK I think I got it. So the form (all states in dropdown) action goes to index.php and then do the db query based on the state name variable, calling all that's state's data like Scientist shows in his code, then plug in the variables as needed in the page. That is just too cool. Thanks guys.
pretty much, although it would probably be more descriptive to have the form link to a page called show_state.php, or something like that.
Link to comment
Share on other sites

You do see all 50 states in a pull down on complicated forms that require a lot of data. For this application, I think users would find that annoying. Unless you really need a form (because you need additional data) it might be more friendly to present an alphabetized list of states as <a> elements, and the href could have a query string embedded in it, as in<a href="show_state.php?s=florida>Florida</a>That would let users hotlink to your process also.

Link to comment
Share on other sites

If you mean that you want to store the text of an img tag, yes you can do that. You probably know about escaping quotes and stuff?Each state gets a row, and the fields are data, that's the way I'd do it.Be sure to create your field with an appropriately-sized data type. I'm sure some of this stuff will exceed 255 characters.

Link to comment
Share on other sites

Thanks DD, the state is varchar and descrip. is text. I'm running into a problem with the dropdown form, which I want to use for the time being. When selecting, I'm getting Object not found, URL not found on this server. The option value is working as selected value is in my browser, like http://127.0.0.1/Maine. But obviously that's not returning to index.php. You can see below the form needs to do the OnChange (don't want a submit button), but that's messed up now that I'm not using static files it seems:

<form name="link" form style="display:inline" action="index.php"><select name="statelink" OnChange="location.href=link.statelink.options[selectedIndex].value">	 <option selected>State</option>	 <option value="Alabama">Alabama</option>	 <option value="Alaska">Alaska</option>etc...

Link to comment
Share on other sites

Getting undefined variables on "state" and "description." I first tried Scientist's code straight up, now experimenting a bit, trying to troubleshoot. Please have a look:

<form name="link" form style="display:inline" action="index.php"><select name="statelink"> <OnChange="location.href=this.options[this.selectedIndex].value">	 <option selected>State</option>	 <option value="Alabama">Alabama</option>	 <option value="Alaska">Alaska</option>	 <option value="Arizona">Arizona</option>	 <option value="Maine">Maine<option></select></form><?$state = mysql_real_escape_string($_POST['statelink']); echo "You live in {$state}.";$state_descrip = mysql_query("SELECT description FROM states WHERE state ='$state'"); $n=0;while($info = mysql_fetch_assoc($state_descrip)){$n++;    //$description = $info['description'];   echo $info;};?>

Link to comment
Share on other sites

1. a good start at debugging this stuff is var_dump($_POST); -- this will show you what your script received as post data.2. You have this written as 2 separate tags:

<select name="statelink"><OnChange="location.href=this.options[this.selectedIndex].value">

It needs to look more like this:

<select name="statelink" OnChange="location.href = this.options[this.selectedIndex].value">

But that's not quite it either, since the href string will just be the name of a state, not an actual filename with or without a query string.

Link to comment
Share on other sites

just an FYI, we can't see links to your localhost (127.0.0.1), that resides only on your computer.I think DD was giving you a heads up with this line:

<select name="statelink" OnChange="location.href = this.options[this.selectedIndex].value">

which is why you are only getting 127.0.0.1/state. Try modifying it so it includes the page you are trying to link to in your form, i.e.

<select name="statelink" OnChange="location.href =index.php?statelink=this.options[this.selectedIndex].value">

Link to comment
Share on other sites

thescientist probably wanted quotes around the literal part of that statement:

<select name="statelink" onchange="location.href = 'index.php?statelink=' + this.options[this.selectedIndex].value">

Link to comment
Share on other sites

Still at it...I know you can't see my local browser...it's being automatically hyperlinked here. Anyhow I copied code above and now getting this in browser http://127.0.0.1/index.php?statelink=Arizona, which isn't going anywhere. Page seems to reload onchange though, but does not generate the $state or $description echoes. array(0) { } from var_dump and "unknown" state, description variables , and "undefined Index" for statelink. My db is working fine with SQL inserts etc and all the fields are there, so that does not seem to be the problem. Problem is somewhere with declaring the variables and getting the form to interface with the variables.

Link to comment
Share on other sites

It seems like a lot of changes have happened to your document. Can you post the new HTML? And if you have made any changes to the relevant PHP, better post that too. I suspect we are very close, and all you need is a small tweak.

Link to comment
Share on other sites

Looking at it from the db backend, as a test, empty values are filling the inserts in "states" rows, so the variables are definitely not generating anything. At least the behavior is more interesting as the "You live in: {$state} line is now echoing "Array" for every blank, form-triggered state row (not the actual state names that I inserted to build table): .ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray

<form name="link" form style="display:inline" ><select name="statelink" onchange="location.href = 'index.php?statelink=' + this.options[this.selectedIndex].value"><!--<select name="statelink" OnChange="this.form.submit();">-->	 <!--<option selected>State</option>-->	 <option value="Alabama">Alabama</option>	 <option value="Alaska">Alaska</option>	 <option value="Arizona">Arizona</option>	 <option value="Maine">Maine<option></select></form><?phpvar_dump($_POST);$state = mysql_real_escape_string($_POST['statelink']); echo "You live in {$state}.";mysql_query("INSERT INTO states (state) VALUES ('$state')") or die(mysql_error());$state_descrip = mysql_query("SELECT description FROM states WHERE state ='$state'");$n=0;while($info = mysql_fetch_assoc($state_descrip)){$n++; 	  echo $info;};?>

Link to comment
Share on other sites

mysql_real_escape_string needs a active connection as parameter. otherwise it will show up the blank. i can guess $state is not assighing the value from $_post['statelink']

Link to comment
Share on other sites

By active connection, if you mean my db connect script, that's working. But you're right that the form is not submitting the value to the page. Before I started on this new approach, I just <href> every option value to an .html page. EASY. Of course, that seems an archaic way of creating 50 pages with some custom content on each. For some reason now, the form is not submitting value to the page. The "reload" behavior leads me to believe it is submitting, or trying to submit SOMEWHERE.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...