/    Sign up×
Community /Pin to ProfileBookmark

PHP For The WWW

Good morning everyone. I am seeking to expand my knowledge of PHP. This morning I am working on a lesson for files and directories in Larry Ulman’s book, “PHP For The World Wide Web!”

The script is as follows:

<html>
<head>
<title>Using Files</title>
</head>

<body >

<?php
function WriteToFile ($URL, $Description) {
/* Function WriteToFile takes two arguments–URL and Description–which wil be written to an external file. */

$TheFile = “data.txt”;
$Open = fopen($TheFile, “a”);
if ($Open) {
fwrite($Open, “$URLt$Descriptionn”);
fclose($Open);
$Worked = TRUE;
} else {
$Worked = FALSE;
}
return $Worked;
}

// End of WriteToFile Function

function ReadFromFile() {
/*Function ReadFromFile displays all the information stored in an external file. */

$TheFile = “data.txt”;
$Open = fopen ($TheFile, “r”);
if ($Open) {
print (“URLs currently listed in the data file:<p>n”);
$Data = file($TheFile);
for ($n = 0; $n < count($Data); $n++){
$GetLine = explode(“t, $Data[$n]”);
print (“$GetLine[0]<br>n$GetLine[1]<p>n”);
}
fclose($Open);
print (“<HR><p>n”);
} else {
print (“Unable to read from data.txt!<br>n”);
}
} // End of ReadFromFile function

function CreateForm () {
// Function CreateForm will display the HTML form.

print (“Add a URL to the data file:n”);
print (“<form action=”urls.php” method=post>n”);
print (“URL <input type-text name=”Array[URL]” size=60><br>n”);
print (“Description <textarea name=”Array[Description]” rows=5 cols=40></textarea><br>n”);
print (“<input type=hidden name=”BeenSubmitted” value=”TRUE”>n”);
print (“<input type=submit name=”submit” value=”Submit!”> </form>n”);

function HandleForm () {
global $Array;
$Pattern = “([url]http://[/url])?([^[:space:]]+)([[:alnum:].,-_?/&=])”;
if (eregi($Pattern, $Array[“URL”]))
{
$Replace = “<a href=”http://23″ target=_new”>23</a>”;
$Array[“URL”]=eregi_replace ($Pattern, $Replace, $Array[“URL”]);
$CallFunction = WriteToFile ($Array[“URL”],$Array[“Description”]);

if ($CallFunction)
{
print (“Your submission-$Array[URL]-has been received!<p><hr><p>n”);
} else {
print (“Your submission was not processed due to a system error!<br>n”);
}
} else {
print (“Please enter a valid Web address!n”);
}
}
// End of the HandleForm Function

/* this next conditional determines whether to handle the form, depending upon whether or not $BeenSubmitted is TRUE. */

if ($BeenSubmitted) {
HandleForm();
}
ReadFromFile();
CreateForm();
?>

</body>

</html>
————————————————————

When I tested the script on my server, I received the following error:

Parse error: parse error in /home/iwdtests/public_html/FilesDir/urls.php on line 88

I don’t understand because line 88 is the </html>. Can someone please explain this to me?

I’ve checked and rechecked my script against that against the book and I haven’t find anything different.

As usual, your wisdom and knowledge will be greatly appreciated.

Magikey

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@solavarJun 15.2004 — When you get a parse error pointing to the very last line, the reason is usually due to an unmatched closing tag, or an unmatched quotation mark.

In other words, PHP finds a brace, for example, but goes all the way to the end, and there is no closing brace to match the opening one. So the parse error points to the last line.

Of course, it isn't the last line. It could be anywhere!

Sorry, can't really go through the code at the moment. I'm watching the soccer, Euro2004, Czech Rep vs Latvia, live.


*** EDIT ***

Try quoting all your elements...

You've got
[code=php]
print ("<form action="urls.php" method=post>n");
[/code]


...must be
[code=php]
print ("<form action="urls.php" method="post">n");
[/code]
Copy linkTweet thisAlerts:
@MstrBobJun 15.2004 — Your CreateForm() function is still open. I'm not sure where you need to close it, but you need to close it. This is often why I find that when writing scripts, it often helps to indent functions, conditionals, and loops. Such as:

[code=php]
/*Script Here*/

if(condition)
{
/*More Script Here*/

while(condition)
{
//Yet More Script//
}
}
[/code]


The indentation allows you to easily tell what is still open and where your scripting is. Just my 2 cents. :p
Copy linkTweet thisAlerts:
@MagikeyauthorJun 15.2004 — Thanks guys. I found the missing bracket place. Now when I try to execute the script, I get the following warning:

Warning: Wrong parameter count for explode() in /home/iwdtests/public_html/FilesDir/urls.php on line 39

13 times before it shows my form. I checked the lines around 39. The script for that area is as follows:


function ReadFromFile() {

// Function ReadFromFile displays all the information stored in an external file.

$TheFile = "data.txt";

$Open = fopen ($TheFile, "r");

if ($Open) {

print ("URLs currently listed in the data file:<p>n");

$Data = file($TheFile);

for ($n = 0; $n < count($Data); $n++){

[COLOR=red]$GetLine = explode("t, $Data[$n]");[/COLOR]

print ("$GetLine[0]<br>n$GetLine[1]<p>n");

}

fclose($Open);

print ("<HR><p>n");

} else {

print ("Unable to read from data.txt!<br>n");

}

} // End of ReadFromFile function


Thanks again for you help.

Magikey
Copy linkTweet thisAlerts:
@MagikeyauthorJun 15.2004 — I hate sounding like the boy who cried wolf, but I was able to fix the problem. The killing problem is that is was such a simple issue. In the line:

[COLOR=blue]$GetLine = explode("t, $Data[$n]");[/COLOR]

I have the quotation marks in the wrong place. It should be:

[COLOR=red]$GetLine = explode("t", $Data[$n]);[/COLOR]

I guess I was looking too hard for the answer. I hope this won't stop anyone from offering me their assistance in the near future. The very near future if I know me.

Magikey
×

Success!

Help @Magikey spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...