Jump to content
xisto Community
Sign in to follow this  
nightfox1405241487

Display Text If Line Not Empty In Config File

Recommended Posts

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:

$announcement = "ANNOUNCEMENT";
In my template file, I could easily add
<?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:
//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

Share this post


Link to post
Share on other sites

You could try:

<?phpif(isset($Announcement))echo $Announcement;?>

That should be sufficient, you don't need to add another variable.

Share this post


Link to post
Share on other sites

Is it possible you could show how your output looks?

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

<?php$announcementOn = true;$announcement = 'blah';?><?phpif($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

Share this post


Link to post
Share on other sites

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

1064337543[/snapback]

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:

<?phpif(isset($announcement))echo "<p class=\"announcement\">$announcement</p>";?>
Does that look correct?

 

[N]F

Share this post


Link to post
Share on other sites

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

if(!empty($announcement))
or
if($announcement)
an item is considered set in the following circumstance
$announcement = "";
Kinda good to know I s'pose.

Share this post


Link to post
Share on other sites

I guess minnieadkins is right. Thanks ... that's something I didnt' know. I thought 'empty=not set'. :)

Share this post


Link to post
Share on other sites

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

<?phpif( 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

Share this post


Link to post
Share on other sites

simply,

<?phpinclude("config.cfg");if($announcementOn) {  if($announcement) {   print $announcement;  }  else {   print "No announcement!";  }}?>

Edited by CrazyPensil (see edit history)

Share this post


Link to post
Share on other sites

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:

 

<?phpif ( !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 :o

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.