Skip to main content
Inspiring
January 17, 2007
Answered

PHP if else help please

  • January 17, 2007
  • 7 replies
  • 711 views
I have to admit that this is the first time I have tried conditional statements and am struggling a bit although I think I'm getting closer.

I have a dynamic table from a test recordset in which there are only 4 records.

I am trying to get the layout cell to alter colour depending on the contactnt of that cell.

Here is where I am so far...

<table border="1" cellpadding="1" cellspacing="1">
<tr>
<td>TYPE</td>
</tr>
<?php do { ?>
<tr>
<?php if ($row_Recordset1['NOTETYPE'] ="NOTE")?><td bgcolor="#CCCCCC"><?php echo $row_Recordset1['NOTETYPE']; ?>
<?php elseif ($row_Recordset1['NOTETYPE'] ="Action")?><td bgcolor="#00FFCC"><?php echo $row_Recordset1['NOTETYPE']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>

Firstly, it seem to create another layout cell to the right of the first (not intended) and it doesn't like the elseif.

What am I doing wrong - does it need a loop?
This topic has been closed for replies.
Correct answer Newsgroup_User
RichardODreamweaver wrote:
> <?php if ($row_Recordset1['NOTETYPE'] ="NOTE")?><td

<?php if ($row_Recordset1['NOTETYPE'] == "NOTE")?><td

Two equals signs, not one.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/

7 replies

Inspiring
January 18, 2007
Ooops - yes it does seem to work - after erasing the "user errors", it works a treat!
Inspiring
January 18, 2007
RichardODreamweaver wrote:
> Can this code block also be used for dynamic table rows instead of just layout
> cells?

Yes, although your code isn't very efficient. This is much simpler:

<?php do { ?>

<?php
$bgcolor = '';
if ($row_recordset['NOTETYPE'] == "Action") {
$bgcolor = ' bgcolor="#CCCCCC"';
}
elseif ($row_recordset['NOTETYPE'] == "Note") {
$bgcolor = ' bgcolor="#00FFCC"';
}
?>

<tr<?php echo $bgcolor; ?>>
<td><?php echo $row_recordset['TYPE']; ?></td>
<td><?php echo $row_recordset['OTHER']; ?></td>
</tr>

<?php } while ($row_recordset = mysql_fetch_assoc($recordsetName)); ?>

> does not seem to work and seems to jump to the else statement.

PHP is case-sensitive. The values must be "Action" or "Note", not
"action" or "note".

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
January 18, 2007
Can this code block also be used for dynamic table rows instead of just layout cells?

Eg:

<tr>
<td>TYPE</td>
<td>OTHER</td>
</tr>

<?php do { ?>

<?php if ($row_recordset['NOTETYPE'] =="Action") { ?>
<tr>
<td bgcolor="#CCCCCC"><?php echo $row_recordset['TYPE']; ?></td>
<td bgcolor="#CCCCCC"><?php echo $row_recordset['OTHER']; ?></td>
</tr>

<?php } elseif ($row_recordset['NOTETYPE'] =="Note") { ?>
<tr>
<td bgcolor="#00FFCC"><?php echo $row_recordset['TYPE']; ?></td>
<td bgcolor="#00FFCC"><?php echo $row_recordset['OTHER']; ?></td>
</tr>

<?php } else { ?>
<tr>
<td><?php echo $row_recordset['TYPE']; ?></td>
<td><?php echo $row_recordset['OTHER']; ?></td>
</tr>

<?php } ?>

This is a much simplified version of a multiple column table I have but mine does not seem to work and seems to jump to the else statement.
Inspiring
January 18, 2007
Basically, if you want to set something to a value, use 1 = sign eg

$a = 10

Will set the variable $a to hold the value 10

If you want to check whether the value in a variable is equal to another
value, use 2 = signs eg

if($a == 10){
echo $a . " is equal to 10";
}

Its a common mistake for beginners to use

if($a = 10){

}

This actual sets the value of $a to 10, rather than check the value is equal
to 10, and the condition always runs.

--
Gareth
http://www.phploginsuite.co.uk/
PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.


Inspiring
January 18, 2007
quote:

Originally posted by: Newsgroup User

This actual sets the value of $a to 10, rather than check the value is equal
to 10, and the condition always runs.





Thanks Gareth
Inspiring
January 18, 2007
Thanks Gareth/ David - can't work out the "==" syntax but it works!
Newsgroup_UserCorrect answer
Inspiring
January 17, 2007
RichardODreamweaver wrote:
> <?php if ($row_Recordset1['NOTETYPE'] ="NOTE")?><td

<?php if ($row_Recordset1['NOTETYPE'] == "NOTE")?><td

Two equals signs, not one.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
January 17, 2007
Should be:

<table border="1" cellpadding="1" cellspacing="1">
<tr>
<td>TYPE</td>
</tr>
<?php do { ?>
<tr>
<?php if ($row_Recordset1['NOTETYPE'] ="NOTE") { ?>
<td bgcolor="#CCCCCC"><?php echo $row_Recordset1['NOTETYPE']; ?></td>
<?php } elseif ($row_Recordset1['NOTETYPE'] ="Action") { ?>
<td bgcolor="#00FFCC"><?php echo $row_Recordset1['NOTETYPE']; ?></td>
<?php } ?>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>

You were missing a closing </td> tag on the first table cell and you need to
use braces for an if statement eg

if($variable == $value){

}elseif($variable == $something_else) {

}

--
Gareth
http://www.phploginsuite.co.uk/
PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.