If statement question

I’m trying to figure out how to make the code below work.
<?php
$var = $fn_category = array (2);
if ($var == TRUE){
include ‘/home/express/public_html/news/news.php’;
}
else {
echo “Sorry there is no news at this time”;
}
?>
Now when array(2) has content it displays but when array (2) is FALSE or empty my else echo doesn’t display. I’ve tried several different ways just can’t quite get it.

Help please and thank you :slight_smile:

array(2); cannot be false. It has content. The value 2. (the array() language construction does not take a size parameter. Anything included in the array definition is data. PHP arrays have no fixed size.

When there is no data for array(2) nothing displays on the page. When there is data for array(2) of course it displays and the code for this is below.
<?php
$fn_category = array (2)
include ‘/home/express/public_html/news/news.php’;
?>

But what I want is that when there is no data to display for array(2) the page displays alternate data. So why wouldn’t the below code work? Or how can I accomplish what I’m trying to do.

<?php
if ($fn_category = array (2)) {
include ‘/home/express/public_html/news/news.php’;
} else {
echo “Sorry there is no news at this time”;
}
?>

Thanks

Your array always has data the way you are defining it - it has one entry with a value of 2.

array(2) = “Create an array with entry at key 0, and value of the integer 2.”

Where are you supposed to be getting the data for this array?

Data for this array is stored in a flat file. array (0) = category id
Each category has it’s news page(s), so when there is no news in a category I want the script to say so instead of being blank.

I’m not the sharpest pin in the haystack, I understand about the array always having data. I guess I have to find in the script where I can accomplish this as I understand I’m not going to get it done at the web document level.

What’s the structure of the flat file? (would be much easier to store in a database, but we can work with flat files.)

<?php die (‘You may not access this file.’); ?>
2|<|1313407754|<|admin|<|Demo News|<|5,24|<|
1|<|1313393104|<|admin|<|Demo Story|<|1,24|<|

mkay, so delimited by |<| and ignoring the first line…


<?php
$news = array();
$f = fopen('path/to/flatfile');
fgets($f); // Get rid of line 1.
while($line = fgets($f)) {
 $news[] = explode("|<|",$line);
}
// $news is now a populated array of elements containing your news data.
if(count($news) == 0) {
  //There was no data.
} else {
  //There was.
}