• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

php "if" condition not working

Hi

$area[0] is either  TITLE or LOGO or TEXT

i want $size to be "10" if $area[0] = TEXT otherwise "50"

but it isn't working. what am i doing wrong?

Thanks!
<?
foreach ($editareas as $area) {$numlines = $htmleditor ? $size : substr_count (wordwrap ($area[1], $areawidth), "\n"); 

if ($area[0] == "TITLE") {$size = '10';} else {$size = '50';} 
?>
<h3><?=$area[0]?></h3>

Open in new window

0
peps03
Asked:
peps03
1 Solution
 
Rik-LeggerCommented:
It seems to simple,
but shouldn't it be the following?

if ($area[0] == "TEXT")
0
 
Greg AlexanderLead DeveloperCommented:
Not sure if it was a copy paste error, but you were missing a bracket:
<?
foreach ($editareas as $area) {
	$numlines = $htmleditor ? $size : substr_count (wordwrap ($area[1], $areawidth), "\n"); 

	if ($area[0] == "TITLE") {
		$size = '10';
	} else {
		$size = '50';
	}
}
?>

Open in new window

0
 
peps03Author Commented:
Thanks for the reactions.

@ Rik-Legger: I made a typo, i meant TITLE

@galexander07: Missing bracket was copy past error.

Excuse me for my carelessness.

now its correct. but it doesn't work correct.
its supposed to set the width of a text area, the first text area (which is TITLE) outputted doesn't get a value (10)..
<? 
foreach ($editareas as $area) 
{
$numlines = $htmleditor ? $size : substr_count (wordwrap ($area[1], $areawidth), "\n"); 
if ($area[0] == "TITLE") {$size = '10';} else {$size = '50';} 
}
?>

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
peps03Author Commented:
i also added the 2 other lines of code in the foreach condition, maybe they explain more..
this is now how i (would like to) use the code.

<? 
foreach ($editareas as $area) 
{
$numlines = $htmleditor ? $size : substr_count (wordwrap ($area[1], $areawidth), "\n"); 
if ($area[0] == "TITLE") {$size = '10';} else {$size = '50';} 

<h3><?=$area[0]?></h3>
<textarea name="areas[]" rows="<?= max ($numlines, $size) ?>" cols="<?=$areawidth?>" class="tinymce"><?=$area[1]?></textarea>

}
?>

Open in new window

0
 
Ray PaseurCommented:
"it isn't working" is not one of the error messages I recognize.  It helps if you can describe what is wrong.  Parse error?  No output?  Loop, etc.

Also, you may find it easier to write understandable code if you eliminate compound statements like this (coding standards exist for a reason):
foreach ($editareas as $area) {$numlines = $htmleditor ? $size : substr_count (wordwrap ($area[1], $areawidth), "\n");

Could you please show us the values of the variables that you are using in the code at ID:35358174?  This appears to be a data-dependent problem and so the contents of these data fields matter.  Thanks, ~Ray
0
 
liveaspankajCommented:
Seems u mixed up html and php a bit: check this
 
<? 
foreach ($editareas as $area) 
{
$numlines = $htmleditor ? $size : substr_count (wordwrap ($area[1], $areawidth), "\n"); 
if ($area[0] == "TITLE") {$size = '10';} else {$size = '50';} 
?>
<h3><? echo $area[0]; ?></h3>
<textarea name="areas[]" rows="<?= max ($numlines, $size) ?>" cols="<?=$areawidth?>" class="tinymce"><?=$area[1]?></textarea>
<?
}
?>

Open in new window

0
 
hmarcbowerCommented:
liveaspankaj found the problem.

There's no reason you can't continue using the <?=$area[0]?> though (as long as you don't want portability of your files to different servers).
0
 
liveaspankajCommented:
yes true that was not essential. its just i have the habit of using the other format that works everywhere.

there actual problem was the other one.
0
 
peps03Author Commented:
Many thanks for the reactions, i solved the issue this way:

<?
if ($area[0] == 'TITLE') {$size = '5';} 
if ($area[0] == 'LOGO') {$size = '30';} 
if ($area[0] == 'TEXT') {$size = '40';} 
?>

Open in new window


Should i delete this question?
0
 
liveaspankajCommented:
the problem was somewhere else :) see my posts above
0
 
peps03Author Commented:
what did you mention that was wrong?
0
 
liveaspankajCommented:
Please compare these: line (6 and 9)

Your earlier code:
 
<? 
foreach ($editareas as $area) 
{
$numlines = $htmleditor ? $size : substr_count (wordwrap ($area[1], $areawidth), "\n"); 
if ($area[0] == "TITLE") {$size = '10';} else {$size = '50';} 

<h3><?=$area[0]?></h3>
<textarea name="areas[]" rows="<?= max ($numlines, $size) ?>" cols="<?=$areawidth?>" class="tinymce"><?=$area[1]?></textarea>

}
?>

Open in new window


Corrected Code:
 
<? 
foreach ($editareas as $area) 
{
$numlines = $htmleditor ? $size : substr_count (wordwrap ($area[1], $areawidth), "\n"); 
if ($area[0] == "TITLE") {$size = '10';} else {$size = '50';} 

<h3><?=$area[0]?></h3>
<textarea name="areas[]" rows="<?= max ($numlines, $size) ?>" cols="<?=$areawidth?>" class="tinymce"><?=$area[1]?></textarea>

}
?>

Open in new window

<? 
foreach ($editareas as $area) 
{
$numlines = $htmleditor ? $size : substr_count (wordwrap ($area[1], $areawidth), "\n"); 
if ($area[0] == "TITLE") {$size = '10';} else {$size = '50';} 
?>
<h3><?=$area[0] ?></h3>
<textarea name="areas[]" rows="<?= max ($numlines, $size) ?>" cols="<?=$areawidth?>" class="tinymce"><?=$area[1]?></textarea>
<?
}
?>

Open in new window

0
 
liveaspankajCommented:
sorry i double embedded the second code
0
 
peps03Author Commented:
oke, you're right!

Thanks
0
 
liveaspankajCommented:
thanks for accepting the answer :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now