First, I want to thank Shrike and mtimdog for their generous help. I really appreciate your responses to my questions.
Second, mtimdog, the last post you made worked. After I removed the \$ from the variable definition, everything worked great.
Third, Shrike, the script you posted--the one with the functions--has totally opened my eyes. I can't thank you enough.
Fourth, this is a pretty long post from this point on. It's just me walking through Shrike's script to make sure I actually understand how it works; so, if you don't want to read any further, don't bother.
<?php
/
All of the following code came from Shrike
on the phpbuilder.com Coding
message board on 24 Feb 04.
Remember, the rest of this is pretty long. All of my comments/remarks
are in the comment lines peppered around the code.
I'm writing this because I just want to make sure I understand
exactly what Shrike accomplished. If you bother to read through
all these comments, my only question is, Am I understanding this stuff?
Okay, first, up to this point, I had never really understood
how functions work, but now, looking at Shrike's code, I think I
finally get it. Well, let's see.
/
/
So, this if/elseif/else statement is saying
"If the browser is reading this page, and there's a variable
named numchapters attached to the URL, and that variable
has a value of 1, then do everything in the getChapterTitles() function.
If there's no numchapters variable appended to the URL but there
is a displayresults variable, and the displayresults variable has
a value of 1, then do everything in the displayResults()
function. Finally, if the browser comes to this page and there
are no variables appended to the URL, just do everything in the
getChapterTitles() function."
How am I doing?
*/
if(isset($POST['numchapters'])){
getChapterTitles();
}elseif(isset($POST['displayresults'])){
displayResults();
}else{
getNumChapters();
}
/
This getNumChapters() is the function that comes in to play the first time
a user comes to "test1.php". If I understand correctly,
the form's action could submit to "fredwilmaanddino.php" or
whatever name I want to give the page, as long as every
form on this page refers to the same name, right?
In getNumChapters() the hidden form field is there simply
to post a value of 1 to the URL so that the if/elseif/else
statement, above, can figure out whether or not a value
has been entered into the book_numofchapters text field.
/
function getNumChapters(){
print("<form method=\"POST\" action=\"test1.php\">\n
Enter number of chapters: <input type=\"text\" name=\"book_numofchapters\">\n
<input type=\"hidden\" name=\"numchapters\" value=\"1\"><br>\n
<input type=\"submit\" name=\"submit\">
");
}
/
The getChapterTitles() function comes in to play only
if the numchapters value of 1 has been appended to the
URL.
*/
function getChapterTitles(){
print("<form method=\"POST\" action=\"test1.php\">\n");
/*
* The $_POST['book_numofchapters'] part of this for loop says
* "Okay, go look in the URL for the value stored in book_numofchapters."
* Then the loop begins and runs, starting at 1 and continuing for the
* value in $_POST['book_numofchapters']. So, if the value
* in $_POST['book_numofchapters'] was equal to 4, the loop would
* run 4 times. I know, I know. Duh. But writing through it helps me get it.
*/
for($i = 1; $i <= $_POST['book_numofchapters']; $i++){
/* The $i on this next line simply says,
* "Whatever $i is equal to at this point in the loop,
* put that value here."
*
* When the name attribute is set to book_chaptertitle[]
* those brackets say, "Put all the stuff you're about to
* get into an array because somebody, somewhere is going
* to want all this data!" I had no idea setting up an array
* was that easy. This was an epiphany for me.
*/
print("Enter chapter title $i: <input type=\"text\" name=\"book_chaptertitle[]\"><br>\n");
}
/*
* The hidden value displayresults is the one that tells
* the form's action page, "Hey! There's data in this form so
* come and get it!" So if diplayresults is in the URL,
* then the diplayResults() function will execute.
*/
print("<input type=\"hidden\" name=\"displayresults\" value=\"1\"><br>\n
<input type=\"submit\" name=\"submit\">\n
</form>
");
}
/
Okay, so now the displayResults() function.
This one opens up a session.
It then takes the value from $POST['book_chaptertitle']
and puts it in a session named $SESSION['book_numofchapters'].
If I understand this correctly, the ENTIRE array of chaptertitles
is now stored in ONE session variable named $SESSION['book_numofchapters'].
So, if the book had 30 chapters, all 30 chapter titles would be stored
in one session variable, right? The way I was doing it would
have created a single session variable for each chapter title.
That now seems like a resource-hog-way of doing things. This
way--the way Shrike has done it with an array--makes a lot more
sense.
The count($SESSION['book_numofchapters']) just counts however
many elements there are in the array, right?
Finally, the foreach loop is essentially saying, "Break up the array stored
in $SESSION['book_numofchapters'], give each element a variable name of $title,
and then print each $title--each one of the array elements--to the page."
When I'm ready to add these chapter titles to the database, I could
then use a similar foreach loop, but instead of putting a print line
inside the loop, I would just put the appropriate SQL Insert line.
Am I understanding this correctly? The foreach loop seems pretty
powerful in this way. All new to me.
*/
function displayResults(){
session_start();
$SESSION['book_numofchapters'] = $POST['book_chaptertitle'];
print("The book has ".count($SESSION['book_numofchapters'])." chapters. They are:<br><br>");
foreach($_SESSION['book_numofchapters'] as $title){
print($title."<br>");
}
print("<br>Thanks for listening");
}
?>
Well, as Shrike said in that last print line,
Thanks for listening. And if you got this far,
I'd email you a beer, but, alas, no one has figured
out how to make such crucial strides in electronics.
They're all caught up with "cell phones" and "PDAs."
Whose working on the real conundrums like
emailing beer?
--Vince