Jump to content

Mutliple If Statements not working :(


valoukh

Recommended Posts

Hi all. I'm having trouble with the following If Statement, wonder if anyone can help. It's only displaying the first If, i.e. " if ($row['Row'] == "1" )". Not sure what I'm doing wrong. Thanks.

 

<?
while($row = mysql_fetch_array($result))
  {
  if ($row['Row'] == "1" )
  {
  echo "<td><table style='margin-left: 20; margin-right: 20;' width='100%' border='0' cellspacing='0' cellpadding='0'>";
  echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='title'><a href='../Photos/" . $row['Path'] . "_big.jpg' target='_blank'><img src='../Photos/" . $row['Path'] . "_sml.jpg' /></a></p></td></tr>";
  echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='main'>" . $row['Caption'] . "</p></td></tr>";
  echo "</table></td>";
  } else {
  }
}

echo "</tr><tr>";

while($row = mysql_fetch_array($result))
  {
  if ($row['Row'] == "2" )
  {
  echo "<td><table style='margin-left: 20; margin-right: 20;' width='100%' border='0' cellspacing='0' cellpadding='0'>";
  echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='title'><a href='../Photos/" . $row['Path'] . "_big.jpg' target='_blank'><img src='../Photos/" . $row['Path'] . "_sml.jpg' /></a></p></td></tr>";
  echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='main'>" . $row['Caption'] . "</p></td></tr>";
  echo "</table></td>";
    } else {
  }
}

echo "</tr><tr>";

while($row = mysql_fetch_array($result))
  {
  if ($row['Row'] == "3" )
  {
  echo "<td><table style='margin-left: 20; margin-right: 20;' width='100%' border='0' cellspacing='0' cellpadding='0'>";
  echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='title'><a href='../Photos/" . $row['Path'] . "_big.jpg' target='_blank'><img src='../Photos/" . $row['Path'] . "_sml.jpg' /></a></p></td></tr>";
  echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='main'>" . $row['Caption'] . "</p></td></tr>";
  echo "</table></td>";
    } else {
  }
}

mysql_close($dbh);
?>

Link to comment
https://forums.phpfreaks.com/topic/96395-mutliple-if-statements-not-working/
Share on other sites

Great! That worked; all the brackets were confusing me a bit. I've got a different problem now - table related. I want the records to display on different rows, 1, 2 and 3. So let's say I've got three records for each row, I want to end up with a grid of 3x3. At the moment they're all in one column. Got any ideas? I'm having trouble spotting the problem!

 

Thanks so much for your help.

 

<?

while($row = mysql_fetch_array($result))

  {

  if ($row['Row'] == "1" )

  {

  echo "<td><table style='margin-left: 20; margin-right: 20;' width='100%' border='0' cellspacing='0' cellpadding='0'>";

  echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='title'><a href='../Photos/" . $row['Path'] . "_big.jpg' target='_blank'><img src='../Photos/" . $row['Path'] . "_sml.jpg' /></a></p></td></tr>";

  echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='main'>" . $row['Caption'] . "</p></td></tr>";

  echo "</table></td>";

  }

 

echo "</tr><tr>";

 

  if ($row['Row'] == "2" )

  {

  echo "<td><table style='margin-left: 20; margin-right: 20;' width='100%' border='0' cellspacing='0' cellpadding='0'>";

  echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='title'><a href='../Photos/" . $row['Path'] . "_big.jpg' target='_blank'><img src='../Photos/" . $row['Path'] . "_sml.jpg' /></a></p></td></tr>";

  echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='main'>" . $row['Caption'] . "</p></td></tr>";

  echo "</table></td>";

  }

 

echo "</tr><tr>";

 

  if ($row['Row'] == "3" )

  {

  echo "<td><table style='margin-left: 20; margin-right: 20;' width='100%' border='0' cellspacing='0' cellpadding='0'>";

  echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='title'><a href='../Photos/" . $row['Path'] . "_big.jpg' target='_blank'><img src='../Photos/" . $row['Path'] . "_sml.jpg' /></a></p></td></tr>";

  echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='main'>" . $row['Caption'] . "</p></td></tr>";

  echo "</table></td>";

  }

}

I don't see any difference is the output for each if statement. You can combine the if statements into one.

<?php
while($row = mysql_fetch_array($result)) {
  if ($row['Row'] == "1" || $row['Row'] == 2 || $row['Row'] == 3) {
      echo "<td><table style='margin-left: 20; margin-right: 20;' width='100%' border='0' cellspacing='0' cellpadding='0'>";
      echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='title'><a href='../Photos/" . $row['Path'] . "_big.jpg' target='_blank'><img src='../Photos/" . $row['Path'] . "_sml.jpg' /></a></p></td></tr>";
      echo "<tr><td height='10' style='vertical-align: top' colspan='2'><p class='main'>" . $row['Caption'] . "</p></td></tr>";
      echo "</table></td>";
  } 
}
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.