Welcome Guest ( Log In | Register )




                Web Hosting Guide

 
Reply to this topicNew Topic
Display Text If Line Not Empty In Config File
nightfox
post Feb 5 2006, 04:17 AM
Post #1


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584


I have been working on a new template and I would like it so that if in the global configuration file, I have a variable for a global site announcement that would go on every page.

The line in the global configuration file is this:
CODE
$announcement = "ANNOUNCEMENT";

In my template file, I could easily add
CODE
<?php echo $announcement; ?>

but that would leave a blank space and with the announcement style (similar to the Invision Power Board error box).

Is there some sort of script that could be put in the template page so I could have the global config file look something like this:
CODE

//Turn on announcement; 1 means on, 0 means off
$announcementOn = 1

//Announcement Text:
$announcement = "Blah"


Basically, if there is a "1" on $announcementOn, it enables code that echos the stylesheet class and displays the announcement in $announcement so when any page is displayed on the site, the announcement is shown.

Thanks for any help!

[N]F
Go to the top of the page
 
+Quote Post
abhiram
post Feb 5 2006, 04:24 AM
Post #2


Hedonist at large
Group Icon

Group: Members
Posts: 610
Joined: 30-July 05
From: another realm
Member No.: 7,524


You could try:

CODE


<?php
if(isset($Announcement))
echo $Announcement;
?>



That should be sufficient, you don't need to add another variable.
Go to the top of the page
 
+Quote Post
mastercomputers
post Feb 5 2006, 10:06 AM
Post #3


Making IT Happen
Group Icon

Group: Members
Posts: 678
Joined: 1-September 04
From: Auckland, New Zealand
Member No.: 27
myCENTs:69.15


Is it possible you could show how your output looks?

I'm trying to understand what you want in your last request:

CODE

<?php
$announcementOn = true;
$announcement = 'blah';
?>

<?php
if($announcementOn)
   echo $announcement;
else{
...do whatever you want...
}
?>


I don't think I fully understood what you wanted, or why you have a mysterious blank, is this because $announcement is empty/null?

Cheers,


MC
Go to the top of the page
 
+Quote Post
nightfox
post Feb 5 2006, 05:41 PM
Post #4


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584


QUOTE(mastercomputers @ Feb 5 2006, 06:06 AM)

I don't think I fully understood what you wanted, or why you have a mysterious blank, is this because $announcement is empty/null?

Cheers,
MC
[right][snapback]68900[/snapback][/right]

I don't have a mysterious blank...

However, I think what abhiram suggested *MIGHT* work. I think this is how I would set it up on my template page:
CODE

<?php
if(isset($announcement))
echo "<p class=\"announcement\">$announcement</p>";
?>

Does that look correct?

[N]F
Go to the top of the page
 
+Quote Post
minnieadkins
post Feb 6 2006, 08:33 PM
Post #5


Premium Member
Group Icon

Group: Members
Posts: 292
Joined: 15-December 04
Member No.: 1,768


Looks like it would work to me. If $announcement is not set then you don't display anything, if it is you do. You could also use
CODE

if(!empty($announcement))

or
CODE

if($announcement)

an item is considered set in the following circumstance
CODE

$announcement = "";

Kinda good to know I s'pose.
Go to the top of the page
 
+Quote Post
abhiram
post Feb 7 2006, 02:14 AM
Post #6


Hedonist at large
Group Icon

Group: Members
Posts: 610
Joined: 30-July 05
From: another realm
Member No.: 7,524


I guess minnieadkins is right. Thanks ... that's something I didnt' know. I thought 'empty=not set'. smile.gif
Go to the top of the page
 
+Quote Post
mastercomputers
post Feb 8 2006, 01:51 AM
Post #7


Making IT Happen
Group Icon

Group: Members
Posts: 678
Joined: 1-September 04
From: Auckland, New Zealand
Member No.: 27
myCENTs:69.15


If that's supposedly correct then this would be your method:

CODE

<?php
if( isset($announcement) && !empty($announcement) ){
   echo '<p class="announcement">' . $announcement . '</p>';
}
?>


isset, checks if the variable is created, does not check it's contents, !empty checks for content.

!empty, checks if the variable has information, if it didn't pass the isset it will not do this expression, so the variable must exist first.

You should not do if($announcement), as you need to check if the variable exists before performing anything on it. This will result in a warning if the variable does not exist.

Cheers,

MC
Go to the top of the page
 
+Quote Post
CrazyPensil
post Mar 21 2006, 09:29 AM
Post #8


Member [ Level 1 ]
Group Icon

Group: Members
Posts: 42
Joined: 17-March 06
From: Russia, St.Petersburg
Member No.: 12,058


simply,
CODE
<?php
include("config.cfg");
if($announcementOn) {
  if($announcement) {
   print $announcement;
  }
  else {
   print "No announcement!";
  }
}
?>


This post has been edited by CrazyPensil: Mar 21 2006, 09:30 AM
Go to the top of the page
 
+Quote Post
Quatrux
post Mar 21 2006, 03:12 PM
Post #9


the Q
Group Icon

Group: [HOSTED]
Posts: 1,355
Joined: 13-July 05
From: Lithuania, Vilnius
Member No.: 7,059
myCENTs:82.09


Well, I would do like mastercomputers suggested it, because it is not recommended to do it like you showed above. But why use empty and isset at the same variable (a comment for mastercomputers) I saw people do it like this too, they said that this is just their programming style.. empty also checks if the variable is set (or better saying, that it does not throw a warning error if the variable is not set) and checks if it is == "" or 0 or "0" or NULL or FALSE or array()

so I would do it:

CODE

<?php
if ( !empty($announcement) ) {
   echo '<p class="announcement">' . $announcement . '</p>';
}
?>


everything clearly is written here: http://php.net/empty but anyway, I think this is to simple, to discuss it smile.gif
Go to the top of the page
 
+Quote Post

Reply to this topicNew Topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   3 PerfecTiion 588 4th March 2010 - 10:57 AM
Last post by: yordan
No new   37 snutz411 9,282 3rd March 2010 - 10:20 PM
Last post by: John Heinl
No New Posts   18 vicky99 7,576 24th February 2010 - 08:16 PM
Last post by: iG-ivmai
No New Posts   11 szupie 3,842 23rd February 2010 - 10:22 PM
Last post by: HannahI
No New Posts 7 FirefoxRocks 5,032 23rd February 2010 - 03:16 PM
Last post by: iG-Patfreeze
No New Posts   17 kc8ual 3,028 23rd February 2010 - 06:29 AM
Last post by: Quatrux
No New Posts   17 Jimmy89 4,818 23rd February 2010 - 12:36 AM
Last post by: iG-David
No New Posts   5 jackpinerepublic 4,390 19th February 2010 - 02:04 AM
Last post by: iG-Simon Varley
No New Posts   7 Eggie 161 15th February 2010 - 10:34 PM
Last post by: Nelson Blogs
No New Posts   17 rapco 9,923 12th February 2010 - 06:48 AM
Last post by: iG-Sanjaya
No new   22 victorhu 7,816 11th February 2010 - 05:13 AM
Last post by: iG-Shane
No New Posts   15 PureHeart 11,910 10th February 2010 - 08:02 PM
Last post by: iG-khadeer
No New Posts   14 jedipi 7,765 10th February 2010 - 05:47 PM
Last post by: iG-keith
No New Posts   12 Nqon 4,595 9th February 2010 - 02:46 PM
Last post by: iG-
No New Posts   10 tansqrx 8,283 9th February 2010 - 12:50 PM
Last post by: iG-gagan deep singh


Web Hosting Powered by ComputingHost.com.
HONESTY ROCKS! truth rules.
Creative Commons License