Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I place data between if else

Status
Not open for further replies.

ro6er

Programmer
Jul 1, 2003
76
I don't get this, it should be simple one but I keep getting an error:
syntax error, unexpected T_ELSE in

All i want to do here is place some data in between the statement, in this case "Some text here..".


Code:
	if($level_depth1 ==0 or $level_depth1 ==1) {
	echo $catname1;
	echo "\n";
	}
	?>
	Some text here..
	<?

	//SUBCAT LISTING
	 else {
	echo "\t";
	echo $catname1;
	echo "\n";
	}
 
So I can place <ul> tags either side of the list of the data that is outputted where it says $catname1;

It's for a little drop down I'm working on and it's a nightmare. I keep reinventing the wheel. Don't worry I'll proceed to triangle, circular wheel's were never that cool.

I'm sure I've done something like this in the past with ASP but with PHP it just doesn't want to play.
 
Hi

Well, there is no life between the closing brace ( [tt]}[/tt] ) and the following [tt]else[/tt] keyword. What you are trying to do there is basically similar to this :
Code:
[b]if[/b] [teal]([/teal]condition[teal])[/teal] [teal]{[/teal]

[teal]}[/teal] [b]echo[/b] [green][i]'wtf'[/i][/green][teal];[/teal] [b]else[/b] [teal]{[/teal]

[teal]}[/teal]
Wondering how ASP or anything else could interpret such thing.

Feherke.
 
@ro6er
you are missing feherke's point.

there can be no value in putting text BETWEEN if ... else constructs. it makes no sense. values go in the IF or the ELSE construct.

you are free to break in and out of php tags whenever you want, so long as you maintain the integrity of the constructs.

so this is valid
Code:
<?php if ($test == true): ?>
<div> I am some html </div>
<?php else : ?>
<div> I am some different html </div>
<?php endif;?>

but this is not
Code:
<?php if ($test == true): ?>
<div> I am some html </div>
<?php else  ?>
<div> I am some different html </div>
<?php :  //close the else
$var = 'something';
endif;?>

because the php is then trying to parse the string
Code:
<div> I am some different html </div>
as the condition for the ELSE construct. and it is barfing at finding an implied string literal in the wrong place.

in your case, it is barfing because it is finding an ELSE after it has assumed that the IF contruct is finalised (because you have succeeding output that is not part of the construct.
 
Yeah yeah I get it. Any who, all sorted I just popped in a count and got the desired effect, which was to have a tag appear once and once only. Thanks for kick up the arse/help
Code:
	 else {
	
	$count=$count+1;
	if ($count<=1)
	echo "<ul>";

	echo "\t";
	echo $catname1;
	echo "\n";
 
Looking at your posted example, I would suggest you optimize your code.

Instead of doing:
echo "\t";
echo $catname1;
echo "\n";

You can do
echo "\t{$catname1}\n";

Also, why do you use $catname1 ?
If you have several categories(?), you should consider using arrays, like:
$cat[1]

It's hard to give you some better examples, without more of your code, but look into this.

You can also replace the $count=$count+1; with:
$count++;

Ps. did you make a </ul> ?

Olav Alexander Mjelde
 
Hi

Olav said:
I would suggest you optimize your code
You mean speed or readability ?

Because [tt]echo[/tt]ing 3 separate values is faster than 1 variable interpolation + [tt]echo[/tt]ing 1 value.

To have both speed and readability I would prefer 1 [tt]echo[/tt] and 3 values : [tt]echo [green]"\t"[/green][teal],[/teal][navy]$catname1[/navy][teal],[/teal][green]"\n"[/green][teal];[/teal][/tt] .

Feherke.
 
@feherke
I was thinking about both, not just the things I posted.
Saw the general coding-style, so I guess there is a lot of optimization to be done, which also could make it more dynamic.


eg. I dont see him echo out any <li> or </li>, </ul>, also this:
if($level_depth1 ==0 or $level_depth1 ==1) {

instead of or, it should be either || or <=1.
Then there is the question: why $level_depth1 ?

Olav Alexander Mjelde
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top