if else display image based on MySQL field

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
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

if else display image based on MySQL field

Post by bruceg »

Hello,

I am displaying the results of a my SQL table in HTML via PHP, and what I want to do for the first <td> to display an image based upon data in one of the MySQL columns. The Column in MySQL is named 'f15' and I want to display the image if the data in the column is equal to 1.

Here is what I have right now:

Code: Select all

<?php
$connection = mysql_connect("localhost","username","password"); 

mysql_select_db ('DBName'); 



$sql = "SELECT f2,f3,phone,f8,f9,f10,f11,f12,f13,f15 FROM station ORDER BY f11 ASC" ;

$sql_resulta = mysql_query($sql,$connection)  or die('Query failed: ' . mysql_error());
$img = '';   
//result set
$rs = mysql_query($sql);

//creating the table w/ headers
     
     echo "<table id='display' align='center'><tr><td id='header'>Airport Indicator</td><td id='header'>State</td><td id='header'>Airport Code</td><td id='header'>Airport Name</td><td id='header'><a href='#' onClick='FuncShowHide()'> More Info.</a></td><td id='header'>Select this Location</td></tr>";

    if($myinfo[f15] == 1) {
      $img = '<td><img src="icons/apo.gif" width="15" height="17" alt="airportindicator"></td>';
    }

  echo $img;
    //row for each record
     while ($row = mysql_fetch_array($rs)) {
                 echo"<tr id='trX'><td>" . $row['f11'] . "</td><td>" . $row['f2'] . "</td><td>" .$row['f3'] ."</td></tr>";
                 echo"<tr id='hideShow' style='display:none'><td>" . $row['f8'] . "</td><td>" . $row['f9'] . "</td><td>" . $row['f10'] . "</td><td>" .$row['f11'] ."</td><td>" .$row['f12'] ."</td><td>" .$row['phone'] ."</td></tr>";     
     }  

     echo "</table>";
   

//close the db
mysql_close();

      echo  "</body>";
      echo  "</html>";
?>
Again, I want to display the image if column f15 in the MySQL table is =1 and I want it to display with the HTML <td> syntax.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

1) I don't see where you set $myinfo. Without $myinfo being set, $img will never get set either.
2) The if statement that determines what the value of $img is, should be in the loop. That way, whether an image is displayed or not will be determined on a row by row basis.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

pickle wrote:1) I don't see where you set $myinfo. Without $myinfo being set, $img will never get set either.
2) The if statement that determines what the value of $img is, should be in the loop. That way, whether an image is displayed or not will be determined on a row by row basis.
I guess you are referring to this part...

Code: Select all

//row for each record
     while ($row = mysql_fetch_array($rs)) {
                 echo"<tr id='trX'><td>" . $row['f15'] . "</td><td>" . $row['f11'] . "</td><td>" . $row['f2'] . "</td><td>" .$row['f3'] ."</td></tr>";
                 echo"<tr id='hideShow' style='display:none'><td>" . $row['f8'] . "</td><td>" . $row['f9'] . "</td><td>" . $row['f10'] . "</td><td>" .$row['f11'] ."</td><td>" .$row['f12'] ."</td><td>" .$row['phone'] ."</td></tr>";     
     }
could you provide an example of what you were referring to?

thanks!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

No he was refering to

Code: Select all

if($myinfo[f15] == 1) {
      $img = '<td><img src="icons/apo.gif" width="15" height="17" alt="airportindicator"></td>';
    }
Where is $myinfo set?
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

what I need to add here is just an if else statement something like

if ( $row[10]== 1 ) {
display $img;
} else {
don't display anything
}
where I have :
<td>
$img
</td>

so I would need the if else between the <td></td> tags

can someone assist me w/ this?

here is the entire code:

Code: Select all

<html>
<head>
 <script language="javascript" type="text/javascript">
   function FuncShowHide(){
      
      if (document.getElementById('hideShow').style.display == document.getElementById('trX').style.display){
         document.getElementById('hideShow').style.display = "none";
      } else {
         document.getElementById('hideShow').style.display = document.getElementById('trX').style.display
      }
   }
</script>
<style type="text/css">
table#display {
border:1px solid #333;
font-family:arial, helvetica, sans-serif;
font-size:12px;
color:#333;
background-color:#d3d3d3;
width:770px;

} 

td{
border:1px solid #666;
border-collapse:collapse;
padding:5px 0 5px 0;
text-align:center;
}

table#display tr{
margin:10px 0 10px 0;
}

table#display td#header{
background-color:#336699;
color:#fff;
padding:15px;
text-align:center;
border:none;
font-weight:bold;
}
table#display a:link{
color:#fff;
font-family:arial, helvetica, sans-serif;
font-size:12px;
}

table#display a:visited{
color:#fff;
font-family:arial, helvetica, sans-serif;
font-size:12px;
}

table#display a:hover{
color:#f0f8ff;
font-family:arial, helvetica, sans-serif;
font-size:12px;
text-decoration:none;
}

table#display a:active{
color:#fff;
font-family:arial, helvetica, sans-serif;
font-size:12px;
}
tr#hideshow{
background-color:#666;color:#fff;
font-family:arial, helvetica, sans-serif;
font-size:12px;
}  
</style>   
</head>        
<body>
<?php
$connection = mysql_connect("localhost","username","password"); 

mysql_select_db ('dbname'); 



$sql = "SELECT f2,f3,phone,f8,f9,f10,f11,f12,f13,f15 FROM station ORDER BY f11 ASC" ;

$sql_resulta = mysql_query($sql,$connection)  or die('Query failed: ' . mysql_error());
 
//result set
$rs = mysql_query($sql);
$img= "<img src='icons/apo.gif' width='15' height='17' alt='airportindicator'>";
//creating the table w/ headers
     
     echo "<table id='display' cellpadding='0' cellspacing='0' align='center'><tr><td id='header'>Airport Indicator</td><td id='header'>State</td><td id='header'>Airport Code</td><td id='header'>Airport Name</td><td id='header'><a href='#' onClick='FuncShowHide()'> More Info.</a></td><td id='header'>Select this Location</td></tr>";

    
 
    //row for each record
     while ($row = mysql_fetch_array($rs))
   
      {   
                 echo"<tr id='trX'><td>
     $img
</td><td> $row[6] </td><td> $row[0]</td><td> $row[1] </td></tr>";
                 echo"<tr id='hideShow' style='display:none'><td><strong>Address:</strong> $row[3] </td><td><strong>Address:</strong>$row[4] </td><td><strong>City:</strong> $row[5] </td><td> $row[6]</td><td>$row[7] </td><td><strong>Phone:</strong> $row[2] </td></tr>";     
     }  

     echo "</table>";
   

//close the db
mysql_close();

      echo  "</body>";
      echo  "</html>";
?>
Post Reply