problem with elseif

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
plankguy
Forum Newbie
Posts: 18
Joined: Sat Jun 25, 2005 7:39 pm

problem with elseif

Post by plankguy »

I have some script that lists the contents of text files based on whether there is a specific letter "a, b, c..." within the text file. I am trying to include a elseif statement to tell the user of the page that there are no files if the letter does not comply...Here's the code:

Code: Select all

//go through the array of files and list each
foreach ( $listings_array as $val ) { 
	$listings_file = file("admin/listings/listings/$val"); 
	//create name variable (name = first line of listing_file array [k, c, e, w])
	$name = $listings_file[0];
	//if the first character of line 1 = the first character of the member's name list their posts
	if ($name[0] == "k"){
		$listMembers = array_shift($listings_file);
		$listAddress = array_shift($listings_file);
		$listDescription = implode('<BR>',$listings_file); 
		echo '<font class="LISTINGS" align="left"><b>'.$listAddress.'</font></b>'; 
		echo '<font class="BODY_TEXT" align="left"><BR>'.$listDescription.'</font><br><HR class="HORIZ_RULE_BTM" width="95%" align="left">'; 
		break;
	}
	elseif ($name[0] != "k"){
		echo '<font class="BODY_TEXT_BOLD" align="left"><b>There are no available properties in this area.</font></b>';
		break;
	}
}
So if the letter is "k" it will list the file...The first IF statement works perfectly, the elseif isn't catching the exceptions...

What am I doing wrong???

Thnx. :oops:
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

well first! you would not need a elseif if you are like do this if this equals that and if not do this. just use "else".

second. have you tried to echo out what $name[0] is? maybe you are missing somthing simple.

third. please use php tags when posting php code. i do respect that you used code tags (as many new people do not) but php tags make it a lot easier to read.

edit: either you or a admin switch the tags, thanks whoever it was.
plankguy
Forum Newbie
Posts: 18
Joined: Sat Jun 25, 2005 7:39 pm

Post by plankguy »

Okay, well I echoed the $name[0] and it returned "k". I also just changed it to just else, still doesn't want to list the file.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Two things I can see. First the break; statements will cause the loop to exit after the first loop, so you will never see anything but the first line. And

Code: Select all

if ($name[0] == "k"){
    } elseif ($name[0] != "k"){ 
    }
// is the same as
if ($name[0] == "k"){
    } else{ 
    }
(#10850)
Post Reply