Welcome to the Cumulus Support forum.

Latest Cumulus MX V4 release 4.4.2 (build 4085) - 12 March 2025

Latest Cumulus MX V3 release 3.28.6 (build 3283) - 21 March 2024

Legacy Cumulus 1 release 1.9.4 (build 1099) - 28 November 2014
(a patch is available for 1.9.4 build 1099 that extends the date range of drop-down menus to 2030)

Download the Software (Cumulus MX / Cumulus 1 and other related items) from the Wiki

If you are posting a new Topic about an error or if you need help PLEASE read this first viewtopic.php?p=164080#p164080

IF Then Else compare question

Other discussion about creating web sites for Cumulus that doesn't have a specific subforum

Moderator: daj

Post Reply
User avatar
aznetcowboy
Posts: 363
Joined: Fri 04 Jan 2013 6:03 pm
Weather Station: Davis 6153 Vantage Pro2
Operating System: Windows 10 Home 64-bit
Location: West Bend, WI USA
Contact:

IF Then Else compare question

Post by aznetcowboy »

I am looking to compare for a blank file, but as I am anything but proficient in PHP, I am not sure how to code a compare looking at a field to see if it is blank and if it isn't display it.

What I am trying to do is:
<?php
if ($wx[field]<>"") // is the $wx[field] blank?
echo $wx[field]; // If not, display it
}
?>
I have been trying to find the correct to do the above, but everything I find on the subject is comparing for numeric value comparisons, not text comparisons. I could use some advice from a more skilled PHP coder than me. which isn't saying a lot for me. :?
Tom Wills
Ridge Run Weather Station: http://www.ridgerun.us
Image
BCJKiwi
Posts: 1259
Joined: Mon 09 Jul 2012 8:40 pm
Weather Station: Davis VP2 Cabled
Operating System: Windows 10 Pro
Location: Auckland, New Zealand
Contact:

Re: IF Then Else compare question

Post by BCJKiwi »

The best test may depend on what you expect to find in the variable.
There are differences between =, == and ===
This table may be of interest;
isset is_null ===null ==null empty
null | F | T | T | T | T |
unset | F | T | T | T | T |
"" | T | F | F | T | T |
[] | T | F | F | T | T |
0 | T | F | F | T | T |
false | T | F | F | T | T |
true | T | F | F | F | F |
1 | T | F | F | F | F |
\0 | T | F | F | F | F |

Note all the 'unexpected?' T results for isset.
Check these with a query on the internet for php isset and/or php null or similar to get the full explanation.
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: IF Then Else compare question

Post by beteljuice »

Code: Select all

<?php

echo ($wx[field] ? $wx[field] : "");

?>
Which is the same as:

Code: Select all

<?php
if($wx[field]){
    echo $wx[field];
}
Which is the same as:

Code: Select all

<?php
if($wx[field] != ""){
    echo $wx[field];
}
ALL of the above are simply looking for $wx[field] to have ANY kind of value. ie. TRUE

EDIT: The first two examples are unsafe ! ... because a value of 0 (zero) would be treated as FALSE.

So for completeness:

Code: Select all

<?php

echo ($wx[field] != "" ? $wx[field] : ""); // you could also say != null

?>
Image
......................Imagine, what you will KNOW tomorrow !
User avatar
SpaceWalker
Posts: 67
Joined: Sun 04 Mar 2012 2:54 am
Weather Station: Davis Vantage
Operating System: Windows XP
Location: Eastern-Canada
Contact:

Re: IF Then Else compare question

Post by SpaceWalker »

I would suggest the use of the PHP function 'empty()' rather than 'isset()' as, not only does it check if the variable is false (or empty), it also checks if the variable exists - the function 'empty()' does not generate a warning if the variable does not exist.

So, for your example, you could use:

Code: Select all

if ( empty ( $wx[field] )) {
  // display a message indicating the variable is empty
}
else {
  echo $wx[field]; // if the variable in not empty
}
Note: The following things/values are considered to be empty:
  • "" (an empty string)
    •0 (0 as an integer)
    •0.0 (0 as a float)
    •"0" (0 as a string)
    •NULL
    •FALSE
    •array() (an empty array)
    •$var; (a variable declared, but without a value)
User avatar
aznetcowboy
Posts: 363
Joined: Fri 04 Jan 2013 6:03 pm
Weather Station: Davis 6153 Vantage Pro2
Operating System: Windows 10 Home 64-bit
Location: West Bend, WI USA
Contact:

Re: IF Then Else compare question

Post by aznetcowboy »

What I am trying to do is make use of the field "currcond" generated by Cumulus' software. It is the field you can enter in some comment like "Today is supposed to be a really nice day!" and it will be displayed on whatever page you put in the pointer to is. For example I am putting it in my index.php page (Saratoga templates).

If I do the following, no problems at all except when I leave the line in the Cumulus page blank, then it displays a blank line.

<font color="darkred">
<?php echo $WX['currcond']?>
</font>


This line then displays between the header and the radar views on my webpage, no problem, except a blank line displays if currcond is blnk.

The currcond in the CUtag.txttmp file shows as follows:

With nothing entered:
currcond|:|:

With "THIS IS A TEST ENTRY - PLEASE IGNORE!" entered:
currcond|Today is supposed to be a really nice day!:|:


What I want to do is have currcond display when it is not blank. So far I have had no luck making it work for me.
Tom Wills
Ridge Run Weather Station: http://www.ridgerun.us
Image
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: IF Then Else compare question

Post by beteljuice »

aznetcowboy wrote:.....
If I do the following, no problems at all except when I leave the line in the Cumulus page blank, then it displays a blank line.

<font color="darkred">
<?php echo $WX['currcond']?>
</font>
If you mean you get a line space which you don't want ....

Code: Select all

<font color="darkred"></font>
Although it maybe the script above it leaves a paragraph 'space'.

Code: Select all

<?php
if($wx['currcond'] != ""){
    echo '<font color="darkred">' .$wx['currcond']. '</font><br />';
}
?>
Image
......................Imagine, what you will KNOW tomorrow !
User avatar
aznetcowboy
Posts: 363
Joined: Fri 04 Jan 2013 6:03 pm
Weather Station: Davis 6153 Vantage Pro2
Operating System: Windows 10 Home 64-bit
Location: West Bend, WI USA
Contact:

Re: IF Then Else compare question

Post by aznetcowboy »

So far, not good... I am getting nowhere. Below is a chunk of code from my Saratoga index.php:

Code: Select all

<body>

<?php
############################################################################
include("header.php");
############################################################################
include("menubar.php");
############################################################################
//
$useTopWarning = true;  // set to true to use only the rss-top-warning script
//                         set to false to use the rss-advisory script instead
?>

<div id="main-copy">

    <?php // insert desired warning box at top of page

  if(isset($SITE['NWSalertsCodes']) and count($SITE['NWSalertsCodes']) > 0) {
	// Add nws-alerts alert box cache file
	include_once("nws-alerts-config.php");
	include($cacheFileDir.$aboxFileName);
	// Insert nws-alerts alert box
	echo $alertBox;

?>
<script type="text/javascript" src="nws-alertmap.js"></script>

<?php
	  
  } else { // use atom scripts of choice
	if ($useTopWarning) {
	  include_once("atom-top-warning.php");
	} else {
	 print "      <div class=\"advisoryBox\">\n";
	 $_REQUEST['inc'] = 'y';
	 $_REQUEST['summary'] = 'Y';
	 include_once("atom-advisory.php");
	 print "      </div>\n";
	}
  }
?>

<div class="column-dark">

<div align="center">

<?php echo $WX['currcond']?>                                       <!-- THIS CODE WORKS -->

<?php
echo ($wx['currcond'] ? $wx['currcond'] : "");                    // THIS CODE DOES NOT WORK //
?>

    <table width="100%" style="border: none">
  <tr><td align="center">
      <img src="http://icons.wunderground.com/data/640x480/<?php echo $SITE['WUregion']; ?>_rd_anim.gif" alt="Regional Radar" width="320" height="240" style="margin: 0px; padding: 0px; border: none" />
  </td>
  <td align="center">
    <img src="http://icons.wunderground.com/data/640x480/<?php echo $SITE['WUregion']; ?>_ir_anim.gif" alt="Regional Infrared Satellite"  
      width="320" height="240" style="margin: 0px; padding: 0px; border: none" />
  </td>
  </tr>
  <tr><td colspan="2" align="center"><small>Radar/Satellite images courtesy of <a href="http://www.weatherunderground.com">Weather Underground</a>.</small></td></tr>
  </table>
  	<img src="<?php echo $SITE['imagesDir']; ?>spacer.gif" alt="spacer"
	height="2" width="620" style="padding:0; margin:0; border: none" />
	<div align="center">

	<?php if(isset($SITE['ajaxDashboard']) and file_exists($SITE['ajaxDashboard']))
	 { include_once($SITE['ajaxDashboard']);
	   } else {
		print "<p>&nbsp;</p>\n";
		print "<p>&nbsp;</p>\n";
		print "<p>Note: ajax-dashboard not included since weather station not yet specified.</p>\n";
        for ($i=0;$i<5;$i++) { print "<p>&nbsp;</p>\n"; }
	}?>
The line where I have <!-- THIS CODE WORKS --> is what will display something when there is a non-space value in $wx[currcond] or a blank line if $wx[currcond] is blank! Where I have // THIS CODE DOES NOT WORK // displays nothing, period. I'm hoping this code can give a better picture of what I am talking about.
Tom Wills
Ridge Run Weather Station: http://www.ridgerun.us
Image
User avatar
PaulMy
Posts: 4355
Joined: Sun 28 Sep 2008 11:54 pm
Weather Station: Davis VP2 Plus 24-Hour FARS
Operating System: Windows8 and Windows10
Location: Komoka, ON Canada
Contact:

Re: IF Then Else compare question

Post by PaulMy »

<?php echo $WX['currcond']?> <!-- THIS CODE WORKS -->

<?php
echo ($wx['currcond'] ? $wx['currcond'] : ""); // THIS CODE DOES NOT WORK //
Totally unfamiliar with it but would there be any issue with your first line having $WX in capitals and the second line $wx in lower case?

Paul
VP2+
C1 www.komokaweather.com/komokaweather-ca
MX https://komokaweather.com/cumulusmx/index.htm /index.html /index.php
MX https://komokaweather.com/cumulusmxwll/index.htm /index.html /index.php
MX https:// komokaweather.com/cumulusmx4/index.htm
Image
User avatar
aznetcowboy
Posts: 363
Joined: Fri 04 Jan 2013 6:03 pm
Weather Station: Davis 6153 Vantage Pro2
Operating System: Windows 10 Home 64-bit
Location: West Bend, WI USA
Contact:

Re: IF Then Else compare question

Post by aznetcowboy »

Thanks a bunch! That was the problem. It was right in front of my nose, but too obvious I guess. :groan:

Changing
echo ($wx['currcond'] ? $wx['currcond'] : "")
to
echo ($WX['currcond'] ? $WX['currcond'] : "")
made all the difference. Thanks again for seeing this! :clap:
Tom Wills
Ridge Run Weather Station: http://www.ridgerun.us
Image
Post Reply