TinyPortal

Development => Block Codes => Topic started by: rjackson on April 22, 2006, 03:24:48 PM

Title: Greeting Block
Post by: rjackson on April 22, 2006, 03:24:48 PM
I found this little bloc on a site, you can set a greeting message to display based on the time of day it is.

--------------------------------------------------------
<?php

$hour = date('G');

if($hour >= 0 && $hour <= 7)
{
        echo 'Very early, isnt it?';
}
else if($hour <= 8 && $hour <= 12)
{
        echo 'Good morning!';
}
else if($hour >= 13 && $hour <= 16)
{
        echo 'Good afternoon!';
}
else if($hour >= 17 && $hour <= 23)
{
        echo 'Good evening!';
}

?>
Title: Re: Greeting Block
Post by: Crip on April 22, 2006, 03:30:54 PM
Cool..
been around awhile, used that years ago. ;)
Title: Re: Greeting Block
Post by: rjackson on April 22, 2006, 03:33:19 PM
found it kinda neat, and since I know nothing of coding, figure I stick whatever I find up for others...if its something old. I'm sorry its new to me :)

Title: Re: Greeting Block
Post by: G6Cad on April 22, 2006, 03:47:31 PM
That was a rather fun snippet :)

Thanks for sharing your findings  :)
Title: Re: Greeting Block
Post by: RoarinRow on April 22, 2006, 04:14:04 PM
Thanks for sharing.  I appreciate any kind of snippet since I'm not a coder by any means.   :uglystupid2: 
Title: Re: Greeting Block
Post by: Crip on April 22, 2006, 04:58:25 PM
Quote from: rjackson on April 22, 2006, 03:33:19 PM
found it kinda neat, and since I know nothing of coding, figure I stick whatever I find up for others...if its something old. I'm sorry its new to me :)

It is an old one, but I stil see it being used all over the net,  so thanks for sharing the snippet. :)
Title: Re: Greeting Block
Post by: HaxXxoR on April 22, 2006, 06:03:38 PM
Is there a way to add this to the User info block? Instead of it saying "Hey user" say Good morning User????
Title: Re: Greeting Block
Post by: sburke930 on August 13, 2006, 11:05:09 PM
I put this code into a block and added the {$MemberName} code but I cannot get it to work.  I've made sure it was a php block, I've not deleted anything but I cannot for the life of me understand why it will not work.  Does someone have a quick answer they could give me?
Title: Re: Greeting Block
Post by: rebelrose on August 14, 2006, 05:43:46 AM
Quote from: rjackson on April 22, 2006, 03:33:19 PM
found it kinda neat, and since I know nothing of coding, figure I stick whatever I find up for others...if its something old. I'm sorry its new to me :)

Thanks, all the code anyone can give me is great, as I know little of code myself. New or old.


Title: Re: Greeting Block
Post by: Thurnok on August 14, 2006, 07:19:31 AM
Quote from: sburke930 on August 13, 2006, 11:05:09 PM
I put this code into a block and added the {$MemberName} code but I cannot get it to work.  I've made sure it was a php block, I've not deleted anything but I cannot for the life of me understand why it will not work.  Does someone have a quick answer they could give me?

First you need to define the variable you need access to as a global, then you can use it.  To do what you want, add this to the top of your snippet block:
global $user_info;

Then, you can add to the greetings at the end of the echo statement like thus:
echo 'Good evening ' . $user_info['username'] . '!';
That would produce the output with the user's username like:
Good evening Thurnok!
Title: Re: Greeting Block
Post by: sburke930 on August 14, 2006, 08:56:04 PM
I am still not able to get this block working and for the life of me I cannot figure this out.  I have it set as a phpbox, and all I get is a parse error on line 1.  Help, please, someone....Thank you!

<?php
  
global $user_info;

$hour date('G'); 

if(
$hour >= && $hour <= 5

        echo 
'You are up early' $user_info['username'] . '!';
}
else if(
$hour >= && $hour <= 8

        echo 
'Grab your coffee yet' $user_info['username'] . '!';

else if(
$hour <= && $hour <= 12

        echo 
'Good morning' $user_info['username'] . '!';

else if(
$hour >= 13 && $hour <= 16

        echo 
'Good afternoon' $user_info['username'] . '!'

else if(
$hour >= 17 && $hour <= 23

        echo 
'Good evening' $user_info['username'] . '!'


?>
Title: Re: Greeting Block
Post by: JPDeni on August 14, 2006, 08:58:24 PM
Don't use the

<?php

and

?>

in a php box.
Title: Re: Greeting Block
Post by: sburke930 on August 14, 2006, 09:07:13 PM
OMG...thank you so very very much...I am ecstatic...this block has been bugging me since yesterday morning!

~Play~
Title: Re: Greeting Block
Post by: JPDeni on August 14, 2006, 10:05:46 PM
:) Glad I could help. It's one of those things that is right there in front of you, but if you don't know about it, you'll never figure it out.

One other little thing you can do, if you want it to reflect the time of day for the user instead of wherever your server is ...

Instead of
$hour = date('G');

use

$hour = date('G') + $user_info['time_offset'];
  if ($hour > 23) { $hour = $hour - 24; }
  elseif ($hour < 0) { $hour = $hour + 24; }
Title: Re: Greeting Block
Post by: rbh on August 15, 2006, 01:57:57 AM
ok, how do you get this to center inside a block? i am using it inside of a center block, and i want it to be centered within that block rather then set to the left.
Title: Re: Greeting Block
Post by: JPDeni on August 15, 2006, 02:07:01 AM
After the $hour definition line (whether you have it use the server time or the user's local time) add

echo '<center>';

After the last } add

echo '</center>';


Or, you can use

echo '<span style="text-align:center;">';

and

echo '</span>';

Use whichever flavor you prefer.
Title: Re: Greeting Block
Post by: rbh on August 15, 2006, 04:19:21 AM
perfect! thanks! :up:
Title: Re: Greeting Block
Post by: sburke930 on August 15, 2006, 06:14:15 AM
Wow so cool...Now I know how to change the font size and color too which was my next question.  Thank you very much for the help.  It is totally appreciated!
Title: Re: Greeting Block
Post by: JPDeni on August 15, 2006, 03:23:02 PM
You're very welcome. Those <span> tags come in really handy, and are a nice way to ease into using css. :)
Title: Re: Greeting Block
Post by: sburke930 on August 15, 2006, 07:47:18 PM
Houston I have a problem.  the bloc was working perfectly until today.  We installed the karma moed and now I cannot get the block to work at all.  It's just a blank box now.  any suggestions besides the obvious?
Title: Re: Greeting Block
Post by: rctxtreme on August 16, 2006, 12:00:08 PM
$hour = date('G') + $user_info['time_offset'];
  if ($hour > 23) { $hour = $hour - 24; }
  elseif ($hour < 0) { $hour = $hour + 24;

if($hour >= 0 && $hour <= 7)
{
        echo 'Very early, isnt it,' . $user_info['username'] . '?';
}
else if($hour <= 8 && $hour <= 12)
{
        echo 'Good morning,' . $user_info['username'] . '!';
}
else if($hour >= 13 && $hour <= 16)
{
        echo 'Good afternoon,' . $user_info['username'] . '!';
}
else if($hour >= 17 && $hour <= 23)
{
        echo 'Good evening,' . $user_info['username'] . '!';
}

Ain't working, this code.
Title: Re: Greeting Block
Post by: JPDeni on August 16, 2006, 01:54:37 PM
You need to add global $user_info; at the beginning and a } at the end of line 3. You'll also want to add a space after each of your commas so that your text looks right. Here's the correct code, all put together:

global $user_info;

$hour = date('G') + $user_info['time_offset'];
  if ($hour > 23) { $hour = $hour - 24; }
  elseif ($hour < 0) { $hour = $hour + 24; }

if($hour >= 0 && $hour <= 7)
{
        echo 'Very early, isnt it, ' . $user_info['username'] . '?';
}
else if($hour <= 8 && $hour <= 12)
{
        echo 'Good morning, ' . $user_info['username'] . '!';
}
else if($hour >= 13 && $hour <= 16)
{
        echo 'Good afternoon, ' . $user_info['username'] . '!';
}
else if($hour >= 17 && $hour <= 23)
{
        echo 'Good evening, ' . $user_info['username'] . '!';
}


This was copied directly from a block on my site that works just fine. If it doesn't work on your system, you've probably got something else conflicting with it.
Title: Re: Greeting Block
Post by: Crip on August 16, 2006, 02:00:52 PM
The above looks good.. thanks for the space after comma info. 8)
Title: Re: Greeting Block
Post by: rctxtreme on August 16, 2006, 11:42:29 PM
Great! Thanks. If only it looked like this to an admin:
Good morning, rctxtreme
PM: 1
Show unread
Show replies
Show own posts

Modules
Downloads

Tools
Upload file
Manage settings
Manage blocks
Manage articles
Manage Downloads
Title: Re: Greeting Block
Post by: JPDeni on August 16, 2006, 11:55:33 PM
It can, but you have to edit the TPortalBlocks.template.php file.

Here's what I did. Down at the bottom of the file, I added
function greeting()
{
  global $user_info;
  $return = '';
  $hour = date('G') + $user_info['time_offset'];
  if ($hour > 23) { $hour = $hour - 24; }
  elseif ($hour < 0) { $hour = $hour + 24; }
  $name = $user_info['username'];
  if($hour >= 0 && $hour <= 7)
  {
        $return = "Very early, isnt it, <b>$name</b>?";
  }
  else if($hour <= 8 && $hour <= 12)
  {
        $return =  "Good morning, <b>$name</b>!";
  }
  else if($hour >= 13 && $hour <= 16)
  {
        $return =  "Good afternoon, <b>$name</b>!";
  }
  else if($hour >= 17 && $hour <= 23)
  {
        $return =  "Good evening, <b>$name</b>!";
  }
  return $return;
}


Then under function userbox(), find

                echo '<span class="normaltext">
                                                        ', $txt['hello_member'], ' <b>', $context['user']['name'], '</b></span>';


Change it to

                echo '<span class="normaltext">
                                                        ', greeting(), '</b></span>';


It works for me.
Title: Re: Greeting Block
Post by: rctxtreme on August 17, 2006, 07:22:02 AM
Seems to me that it doesn't contain the modules, tools sections. Does it? If it does perhaps you should show me an example
Title: Re: Greeting Block
Post by: JPDeni on August 17, 2006, 01:03:46 PM
The userbox is what contains all the other stuff in the box.

Here's a screenshot of mine from this morning.

(I don't have the download module active, so that's not part of the box.)
Title: Re: Greeting Block
Post by: rctxtreme on August 18, 2006, 09:11:04 AM
Thanks!
Title: Re: Greeting Block
Post by: Bobbyplant on March 06, 2008, 03:30:12 PM
Quote from: rjackson on April 22, 2006, 03:24:48 PM
I found this little bloc on a site, you can set a greeting message to display based on the time of day it is.

--------------------------------------------------------
<?php

$hour = date('G');

if($hour >= 0 && $hour <= 7)
{
        echo 'Very early, isnt it?';
}
else if($hour <= 8 && $hour <= 12)
{
        echo 'Good morning!';
}
else if($hour >= 13 && $hour <= 16)
{
        echo 'Good afternoon!';
}
else if($hour >= 17 && $hour <= 23)
{
        echo 'Good evening!';
}

?>

Hi sorry to sound silly but as im not clued up on adding blocks or anything to the front page. How do i add this?? Please excuse my ignorance.
Title: Re: Greeting Block
Post by: JPDeni on March 06, 2008, 06:48:29 PM
Check out the Documentation about Blocks (http://www.tinyportal.net/index.php?board=118.0).
Title: Re: Greeting Block
Post by: Bobbyplant on March 06, 2008, 09:17:25 PM
Hi thanks for the reply. The docs page offers examples but the links dont work??

Im struggling to get anything to appear :laugh:
Title: Re: Greeting Block
Post by: JPDeni on March 06, 2008, 10:20:10 PM
Do you have TinyPortal installed? If not, install it before going any further.

Go to your admin interface. Look for a section labeled TinyPortal. Under that will be a link labeled Block Manager. Click it.

From there, you can create a new block. You'll want a php block. Paste the code into the block. Save it. Then, on the main list of blocks be sure to activate it -- click the green button by the block.

If you have problems with any of this, you can ask questions, but you will need to be specific with what you have done and what the problem is.
Title: Re: Greeting Block
Post by: lOOmis on March 06, 2008, 10:22:56 PM
I can't seem to find the file...

"It can, but you have to edit the TPortalBlocks.template.php file."

Is it accessable from the Current Theme?

Thanks,
lOOmis
Title: Re: Greeting Block
Post by: Bobbyplant on March 06, 2008, 10:26:17 PM
Quote from: JPDeni on March 06, 2008, 10:20:10 PM
Do you have TinyPortal installed? If not, install it before going any further.

Go to your admin interface. Look for a section labeled TinyPortal. Under that will be a link labeled Block Manager. Click it.

From there, you can create a new block. You'll want a php block. Paste the code into the block. Save it. Then, on the main list of blocks be sure to activate it -- click the green button by the block.

If you have problems with any of this, you can ask questions, but you will need to be specific with what you have done and what the problem is.

Thanks for your help, got it sorted!
Title: Re: Greeting Block
Post by: JPDeni on March 06, 2008, 11:43:31 PM
lOOmis, what file are you talking about? You don't need to find a file to create a block. You just create it.

Bobbyplant, Cool!! :)
Title: Re: Greeting Block
Post by: lOOmis on March 06, 2008, 11:51:50 PM
This is what I was asking about. Is it part of the Template CSS? I got the origianl code inserted into a php block. Worked great. I would like to add the code below.

Quote from: JPDeni on August 16, 2006, 11:55:33 PM
It can, but you have to edit the TPortalBlocks.template.php file.

Here's what I did. Down at the bottom of the file, I added
function greeting()
{
  global $user_info;
  $return = '';
  $hour = date('G') + $user_info['time_offset'];
  if ($hour > 23) { $hour = $hour - 24; }
  elseif ($hour < 0) { $hour = $hour + 24; }
  $name = $user_info['username'];
  if($hour >= 0 && $hour <= 7)
  {
        $return = "Very early, isnt it, <b>$name</b>?";
  }
  else if($hour <= 8 && $hour <= 12)
  {
        $return =  "Good morning, <b>$name</b>!";
  }
  else if($hour >= 13 && $hour <= 16)
  {
        $return =  "Good afternoon, <b>$name</b>!";
  }
  else if($hour >= 17 && $hour <= 23)
  {
        $return =  "Good evening, <b>$name</b>!";
  }
  return $return;
}


Then under function userbox(), find

                echo '<span class="normaltext">
                                                        ', $txt['hello_member'], ' <b>', $context['user']['name'], '</b></span>';


Change it to

                echo '<span class="normaltext">
                                                        ', greeting(), '</b></span>';


It works for me.
Title: Re: Greeting Block
Post by: ApplianceJunk on March 07, 2008, 12:02:14 AM
Cool!

Works great for us, thanks!
Title: Re: Greeting Block
Post by: JPDeni on March 07, 2008, 12:28:15 AM
lOOmis, you'll have to edit the TPortalBlocks.template.php in a text editor.
Title: Re: Greeting Block
Post by: lOOmis on March 07, 2008, 02:27:24 AM
OK, Thankyou. I was looking everywhere in the Admin Section to get to it....oh well. Not ready to tackle that.

Thanks again for the help.

lOOmis
Title: Re: Greeting Block
Post by: Solomon on March 09, 2008, 02:08:19 PM
JPDeni
Thanks, this is a neat and friendly piece of coding :)
Title: Re: Greeting Block
Post by: JPDeni on March 09, 2008, 02:10:43 PM
Cool! I'm glad you like it. :D