PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
m0u53m4t
Forum Contributor
Posts: 101 Joined: Wed Apr 19, 2006 7:47 am
Location: Wales
Post
by m0u53m4t » Sun Jun 18, 2006 9:54 am
This is my php code designed to take a number of hours, convert them into days and hours, then write to time.php in this format: "days:hours" but I'm getting this error:
Parse error: parse error, unexpected T_IF on line 5
Code: Select all
<?php
$filename = "time.php";
$hours = $_GET["hours"]
if $hours > 24 then {
if $hours > 48 then {
if $hours > 72 then {
$days = 3;
$hours = $hours - 72;
}
$days = 2;
$hours = $hours - 48;
}
$days = 1;
$hours = $hours - 24;
}
if (!$handle = fopen($filename, 'a')) {
die ("Error while connecting, please try again.");
} else {
if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n" .) === FALSE){
die("Error while writing.");
}
}
echo "Successful";
fclose($handle);
} else {
echo "Error: No time was specified";
?>
bdlang
Forum Contributor
Posts: 395 Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US
Post
by bdlang » Sun Jun 18, 2006 10:12 am
You're missing a statement ending semicolon at the end of your $hours assignment, and there is no 'if...then' in PHP.
Code: Select all
<?php
$filename = "time.php";
$hours = $_GET["hours"];
if ($hours > 24) {
if ($hours > 48) {
if ($hours > 72) {
m0u53m4t
Forum Contributor
Posts: 101 Joined: Wed Apr 19, 2006 7:47 am
Location: Wales
Post
by m0u53m4t » Sun Jun 18, 2006 10:18 am
I updated the code to this:
Code: Select all
<?php
$filename = "time.php";
$hours = $_GET["hours"];
if ($hours > 24) {
if ($hours > 48) {
if ($hours > 72) {
$days = 3;
$hours = $hours - 72;
}
$days = 2;
$hours = $hours - 48;
}
$days = 1;
$hours = $hours - 24;
}
if (!$handle = fopen($filename, 'a')) {
die ("Error while connecting, please try again.");
} else {
if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n" .) === FALSE){
die("Error while writing.");
}
}
echo "You've been online for $days days and $hours hours this week.;
fclose($handle);
} else {
echo "Error: No time was specified";
?>
but am now getting this error:
Parse error: parse error, unexpected ')' on line 24
phpCCore Brad
Forum Commoner
Posts: 47 Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:
Post
by phpCCore Brad » Sun Jun 18, 2006 10:23 am
Line 24 has an extra . which is telling php you should have something else after it, but you don't. Simply remove it and you get:
Code: Select all
//Before:
if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n" .) === FALSE){
//After:
if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n") === FALSE){
m0u53m4t
Forum Contributor
Posts: 101 Joined: Wed Apr 19, 2006 7:47 am
Location: Wales
Post
by m0u53m4t » Sun Jun 18, 2006 10:30 am
That fixed that. Still another bug. Here's the code:
Code: Select all
<?php
$filename = "time.php";
$hours = $_GET["hours"];
if ($hours > 24) {
if ($hours > 48) {
if ($hours > 72) {
$days = 3;
$hours = $hours - 72;
}
$days = 2;
$hours = $hours - 48;
}
$days = 1;
$hours = $hours - 24;
}
if (!$handle = fopen($filename, 'a')) {
die ("Error while connecting, please try again.");
} else {
if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n") === FALSE){
die("Error while writing.");
}
}
echo "You've been online for $days days and $hours hours this week.";
fclose($handle);
} else {
echo "Error: No time was specified";
}
?>
And here's the error I'm getting:
Parse error: parse error, unexpected '}' on line 30
phpCCore Brad
Forum Commoner
Posts: 47 Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:
Post
by phpCCore Brad » Sun Jun 18, 2006 10:37 am
I retabbed your document... I could make an assumuption to what that else belongs to, but maybe with the retabs you will figure out your own mistake
Code: Select all
<?php
$filename = "time.php";
$hours = $_GET["hours"];
if ($hours > 24) {
if ($hours > 48) {
if ($hours > 72) {
$days = 3;
$hours = $hours - 72;
}
$days = 2;
$hours = $hours - 48;
}
$days = 1;
$hours = $hours - 24;
}
if (!$handle = fopen($filename, 'a')) {
die ("Error while connecting, please try again.");
} else {
if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n") === FALSE){
die("Error while writing.");
}
}
echo "You've been online for $days days and $hours hours this week.";
fclose($handle);
} else {
echo "Error: No time was specified";
}
?>
Edit: I retabbed some that were spaces instead of tabs making them not appear on the same line
m0u53m4t
Forum Contributor
Posts: 101 Joined: Wed Apr 19, 2006 7:47 am
Location: Wales
Post
by m0u53m4t » Sun Jun 18, 2006 10:47 am
I'm not sure. I adapted it from this script:
Code: Select all
<?php
$filename = "logfile.txt";
if (isset($_GET["comments"])){
if (!$handle = fopen($filename, 'a')) {
die ("Error while connecting, please try again.");
} else {
if (fwrite($handle, "\r\n" . $_GET["comments"]."\r\n" ) === FALSE){
die("Error while writing.");
}
}
echo "Comments successful";
fclose($handle);
} else {
echo "Error: no comments entered";
}
?>
phpCCore Brad
Forum Commoner
Posts: 47 Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:
Post
by phpCCore Brad » Sun Jun 18, 2006 10:56 am
Then you should probably remove it since you removed the if statement it belongs to.
m0u53m4t
Forum Contributor
Posts: 101 Joined: Wed Apr 19, 2006 7:47 am
Location: Wales
Post
by m0u53m4t » Sun Jun 18, 2006 10:59 am
Ah. So it should be:
Code: Select all
<?php
$filename = "time.php";
$hours = $_GET["hours"];
if ($hours > 24) {
if ($hours > 48) {
if ($hours > 72) {
$days = 3;
$hours = $hours - 72;
}
$days = 2;
$hours = $hours - 48;
}
$days = 1;
$hours = $hours - 24;
}
if (!$handle = fopen($filename, 'a')) {
die ("Error while connecting, please try again.");
} else {
if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n") === FALSE){
die("Error while writing.");
}
}
echo "You've been online for $days days and $hours hours this week.";
fclose($handle);
?>
I'm just getting this error:
Warning: Wrong parameter count for fwrite() on line 25
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Sun Jun 18, 2006 11:37 am
Try it like this:
Code: Select all
<?php
$filename = "time.php";
$hours = $_GET["hours"];
if ($hours > 24) {
if ($hours > 48) {
if ($hours > 72) {
$days = 3;
$hours = $hours - 72;
}
$days = 2;
$hours = $hours - 48;
}
$days = 1;
$hours = $hours - 24;
}
if (!$handle = fopen($filename, 'a')) {
die ("Error while connecting, please try again.");
} else {
$write = "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n";
if (fwrite($handle, $write) === FALSE){
die("Error while writing.");
}
}
echo "You've been online for $days days and $hours hours this week.";
fclose($handle);
?>
m0u53m4t
Forum Contributor
Posts: 101 Joined: Wed Apr 19, 2006 7:47 am
Location: Wales
Post
by m0u53m4t » Sun Jun 18, 2006 12:09 pm
Still more errors
Parse error: parse error, unexpected ',' in /home/freehost/t35.com/j/u/juniorfiles/sig/updatetime.php on line 23
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Sun Jun 18, 2006 3:25 pm
opps I made an error.
Try this:
Code: Select all
<?php
$filename = "time.php";
$hours = $_GET["hours"];
if ($hours > 24) {
if ($hours > 48) {
if ($hours > 72) {
$days = 3;
$hours = $hours - 72;
}
$days = 2;
$hours = $hours - 48;
}
$days = 1;
$hours = $hours - 24;
}
if (!$handle = fopen($filename, 'a')) {
die ("Error while connecting, please try again.");
} else {
$write = "\r\n" . $days. "\r\n" . ":" . $hours. "\r\n";
if (fwrite($handle, $write) === FALSE){
die("Error while writing.");
}
}
echo "You've been online for $days days and $hours hours this week.";
fclose($handle);
?>
I put commas by accident instead of periods.
SORRY!!!
m0u53m4t
Forum Contributor
Posts: 101 Joined: Wed Apr 19, 2006 7:47 am
Location: Wales
Post
by m0u53m4t » Mon Jun 19, 2006 11:26 am
Thanks. What does /R/N do?
bdlang
Forum Contributor
Posts: 395 Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US
Post
by bdlang » Mon Jun 19, 2006 1:28 pm
m0u53m4t wrote: Thanks. What does /R/N do?
That does nothing; however, \r\n are
escape characters that create a return and a newline, respectively. Essentially, two ways to create a new line in your output.
If you're dealing with a UNIX based file, use "\n" to create a newline in the file.
PHP output to a file on Windows should use "\r\n".
Output on a Mac just "\r".
Please note these need to be wrapped in double quotes to be effective. Single quotes will output a literal '\n'.