Jump to content


Photo

Need some help with PHP code


  • Please log in to reply
35 replies to this topic

#1 Chrypetex

Chrypetex

    Young Padawan

  • Members
  • Pip
  • 121 posts
  • Gender:Male
  • Location:Sweden
  • Interests:Photos

Posted 27 October 2006 - 11:36 AM

Hello!
I'm training some PHP, and need some help ^^
I have this code:
<?php
$d=date("H");
if ($d=="18")
  echo "Good Evening!";
elseif ($d=="19")
  echo "Something";  
else
  echo "Something went wrong :/ "; 
?>

It should say different things depending on the time, if we say that the clock is 18, the code should say "Good Evening". If it's 19, it should say something else. Do you understand? ^^

Now, the code don't work, and i wonder if you fellows know what could be wrong?

#2 Matt L

Matt L

    Young Padawan

  • Members
  • Pip
  • 272 posts
  • Gender:Male
  • Location:Newcastle

Posted 27 October 2006 - 11:42 AM

Try this
<?php
$d=time("H"); // Use the time() function instead; best I know, date() doesn't do time.
if ($d > "18") // If the time is past 18:00
  echo "Good Evening!";
elseif ($d > "19") // If the time is past 19:00
  echo "Something";  
else
  echo "Something went wrong :/ "; 
?>

Edited by Matt L, 27 October 2006 - 11:43 AM.


#3 Chrypetex

Chrypetex

    Young Padawan

  • Members
  • Pip
  • 121 posts
  • Gender:Male
  • Location:Sweden
  • Interests:Photos

Posted 27 October 2006 - 11:47 AM

It worked! Thank you very, very much!

#4 Chrypetex

Chrypetex

    Young Padawan

  • Members
  • Pip
  • 121 posts
  • Gender:Male
  • Location:Sweden
  • Interests:Photos

Posted 27 October 2006 - 12:53 PM

Now something is wrong again :/

<?php
$d=time("H"); 
if ($d > "06") 
  echo "Good Morning!";
elseif ($d > "12") 
  echo "Good Day!";
elseif ($d > "15") 
  echo "Good Afternoon!";
elseif ($d > "18") 
  echo "Good Evening!";  
elseif ($d > "22") 
  echo "Good Night!"; 
else
  echo "Something went wrong :/ ";
?>

Can someone see whats wrong? :/

#5 Matt L

Matt L

    Young Padawan

  • Members
  • Pip
  • 272 posts
  • Gender:Male
  • Location:Newcastle

Posted 27 October 2006 - 01:13 PM

<?php
$d=time("H"); 
if ($d > "06" && $d < "12") {
  echo "Good Morning!";
  }
elseif ($d > "12" && $d < "15") {
  echo "Good Day!";
  }
elseif ($d > "15" && $d < "18") {
  echo "Good Afternoon!";
  }
elseif ($d > "18" && $d < "22") {
  echo "Good Evening!";  
  }
elseif ($d > "22" && $d < "06")  {
  echo "Good Night!"; 
  }
else {
  echo "Something went wrong :/ ";
  }
?>
You forgot the curley brackets ( { and } )

Edited by Matt L, 28 October 2006 - 05:06 AM.


#6 Chrypetex

Chrypetex

    Young Padawan

  • Members
  • Pip
  • 121 posts
  • Gender:Male
  • Location:Sweden
  • Interests:Photos

Posted 27 October 2006 - 01:44 PM

It still says "Good morning" :S
http://www.artorb.com/max/ao.php

#7 Matt L

Matt L

    Young Padawan

  • Members
  • Pip
  • 272 posts
  • Gender:Male
  • Location:Newcastle

Posted 27 October 2006 - 02:22 PM

Changed the code above. Should work now.

#8 Chrypetex

Chrypetex

    Young Padawan

  • Members
  • Pip
  • 121 posts
  • Gender:Male
  • Location:Sweden
  • Interests:Photos

Posted 27 October 2006 - 03:04 PM

It's really strange!

Now it shows this: Parse error: parse error, unexpected ')' in /home/artorb5/public_html/max/ao.php on line 72

And on line 72 i have this:

72. elseif ($d > "12" && $d < "15")) {
73. echo "Good Day!";
74. }

etc..

#9 Chaos King

Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 27 October 2006 - 03:29 PM

PHP is all about logic. If you don't have logic, you will never get anywhere.

$d = date ( 'H' );

if ( $d > 22 )
	echo 'Good Night!';
else if ( $d > 18 )
	echo 'Good Evening!';
else if ( $d > 15 )
	echo 'Good Afternoon!';
else if ( $d > 12 )
	echo 'Good Day!';
else if ( $d > 6 )
	echo 'Good Morning';
else
	echo 'Hello';


#10 coolaid

coolaid

    P2L Jedi Master

  • Members
  • PipPipPipPip
  • 1,435 posts
  • Gender:Male
  • Interests:i wonder..

Posted 27 October 2006 - 04:25 PM

PHP is all about logic. If you don't have logic, you will never get anywhere.


yep, so basically what your error, this one:
( Parse error: parse error, unexpected ')' in /home/artorb5/public_html/max/ao.php on line 72 )

means that there was something wrong with the syntax itself, the actual way you wrote the code. and unexpected ')' meant that you had a close-parenthesis character that shouldn't have been there!

just to help you boost your learning curve :)

#11 Chrypetex

Chrypetex

    Young Padawan

  • Members
  • Pip
  • 121 posts
  • Gender:Male
  • Location:Sweden
  • Interests:Photos

Posted 28 October 2006 - 06:51 AM

Chaos King: Your code didn't work :S :/
It just says Hello :/

Edited by Chrypetex, 28 October 2006 - 06:52 AM.


#12 meadow

meadow

    Young Padawan

  • Members
  • Pip
  • 224 posts
  • Location:Devon, England
  • Interests:Php, Hockey, mysql, web design.

Posted 28 October 2006 - 01:59 PM

I just tried this and it worked for me:

<?php
$d = date ( 'H' );

if ( $d > 22 ){
	echo 'Good Night!';
}elseif ( $d > 18 ){
	echo 'Good Evening!';
}elseif ( $d > 15 ){
	echo 'Good Afternoon!';
}elseif ( $d > 12 ){
	echo 'Good Day!';
}elseif ( $d > 6 ){
	echo 'Good Morning';
}else{
	echo 'Hello';
	}
	?>

All I did was add the circly brackets "{}", I don't know why Chaos Kings code didn't work though.

Edited by meadow, 28 October 2006 - 02:00 PM.


#13 Chrypetex

Chrypetex

    Young Padawan

  • Members
  • Pip
  • 121 posts
  • Gender:Male
  • Location:Sweden
  • Interests:Photos

Posted 29 October 2006 - 06:06 AM

I'm really going nuts!
It says hello, but it's 12 now!
I can't understand what wrong :S

#14 Matt L

Matt L

    Young Padawan

  • Members
  • Pip
  • 272 posts
  • Gender:Male
  • Location:Newcastle

Posted 29 October 2006 - 10:37 AM

<?php
$d = date ( 'H' );

if ( $d => 22 && $d < 6 ) {
	echo 'Good Night!';
	}
elseif ( $d => 18 && $d < 22 ) {
	echo 'Good Evening!';
	}
elseif ( $d => 15 && $d < 18) {
	echo 'Good Afternoon!';
	}
elseif ( $d => 12 && $d < 15) {
	echo 'Good Day!';
	}
elseif ( $d => 6 && $d < 12) {
	echo 'Good Morning';
	}
else {
	echo 'Hello';
	}
	?>

This is what I've got.

Edited by Matt L, 29 October 2006 - 10:40 AM.


#15 Chrypetex

Chrypetex

    Young Padawan

  • Members
  • Pip
  • 121 posts
  • Gender:Male
  • Location:Sweden
  • Interests:Photos

Posted 29 October 2006 - 10:45 AM

Now i get parse error :/

http://www.artorb.com/max/ao.php

On line 70 i have:

if ( $d => 22 && $d < 6 ) {

Edited by Chrypetex, 29 October 2006 - 10:45 AM.


#16 cheerio

cheerio

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Gender:Male

Posted 29 October 2006 - 11:21 AM

if ( $d >= 22 && $d < 6 ) {

BTW...that will always return false

Edited by cheerio, 29 October 2006 - 11:22 AM.


#17 Chrypetex

Chrypetex

    Young Padawan

  • Members
  • Pip
  • 121 posts
  • Gender:Male
  • Location:Sweden
  • Interests:Photos

Posted 29 October 2006 - 11:55 AM

What do you mean? That the code never will work?

#18 influct

influct

    Young Padawan

  • Members
  • Pip
  • 63 posts

Posted 29 October 2006 - 03:15 PM

<?php
$d = date ( 'H' );
if ((( $d > "22") or ($d =="22")) && ($d < "6" )) {
	echo 'Good Night!';
	}
elseif ((( $d > "18") or  ($d =="18")) && ($d < "22" )) {
	echo 'Good Evening!';
	}
elseif ((( $d > "15") or  ($d =="15")) && ($d < "18" )) {
	echo 'Good Afternoon!';
	}
elseif ((( $d > "12") or  ($d =="12")) && ($d < "15" )) {
	echo 'Good Day!';
	}
elseif ((( $d > "6") or  ($d =="6")) && ($d < "12" )) {
	echo 'Good Morning!';
	}
else {
	echo 'Hello';
	}
	?>

I tested this, trust me when i say it works:-)

remember, your time is different from your server time!

if you want to find out your server time just put echo$d; just before ?> and it will tell you server time.

I hope you find this helpful.

#19 cheerio

cheerio

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Gender:Male

Posted 29 October 2006 - 03:28 PM

You can't have a number greater than 22 AND less than 6

#20 influct

influct

    Young Padawan

  • Members
  • Pip
  • 63 posts

Posted 29 October 2006 - 03:31 PM

:huh:
its hours, it goes 1,2,3,4,5,6,7,8 etc... right up to 24 then goes back on itself, so its a never ending sequence. so yes, you can have a number thats greater than 22 and less than 6.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users