Jump to content

Recommended Posts

Hello,

 

I am displaying the results of a my SQL table in HTML via PHP, and what I want to do for the first column is display and image based information is one of the MySQL columns. The Column 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:

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

 

and it's not working. I have the icons folder with apo.gif located on the server.

 

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.

 

Link to comment
https://forums.phpfreaks.com/topic/55452-phpmyswl-show-image-if/
Share on other sites

<?php

  $fields = 'id, f15, value';
  $table = 'mytable';

  $q = $db->query("SELECT $fields FROM $table");
  $myinfo = $db->fetch_array($q);

  $img = '';

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

  echo $img;

?>

 

If you're using a while loop...then the position of "f15" in the array, depends on the coulmn it is in, and what you're selecting from the table.

 

Example....

 

<?php

  $fields = 'id, f15, value';
  $table = 'mytable';

  $myinfo = $db->query("SELECT $fields FROM $table WHERE id > 0");

  $img = '';

    while($row = $db->fetch_array($myinfo)) {
    
      if($row[1] == 1) {
        $img = '<tr><td><img src="icons/apo.gif" width="15" height="17" alt="airportindicator"></td></tr>';
      }
     echo"$img <br />";
    }

?>

maybe it would help if I displayed the whole code, I have so far. I think I still have the image declaration wrong...

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

mysql_select_db ('db_name'); 



$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);

//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'><span onClick='toggle();'>More Info.</span></td><td id='header'>Select this Location</td></tr>";

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

  echo $img;
    //row for each record
     while ($row = mysql_fetch_array($rs)) {
                 echo"<tr><td>" . $row['f11'] . "</td><td>" . $row['f2'] . "</td><td>" .$row['f3'] ."</td></tr>";
                 echo"<tr id='hidethis'><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>";
?>

Man...that's messy. And not going to do what you want.

 

<?php

  $connection = mysql_connect("localhost","username","password"); 
  mysql_select_db ('db_name'); 

  $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());

  $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'><span onClick='toggle();'>More Info.</span></td>
        <td id='header'>Select this Location</td></tr>";

          while ($row = mysql_fetch_array($rs)) {

           echo"
          <tr><td>" . $row['f11'] . "</td><td>" . $row['f2'] . "</td><td>" .$row['f3'] ."</td></tr>
           <tr id='hidethis'><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>";
?>

 

Now where in there do you want to display the image?

 

And is the path to the image always going to be the same? Also, when you use double quotes, you don't need to keep coming in and out of the quotes to display variables.  (echo"<tr><td>$row[0]</td></tr>"; is fine) Change all the $row[] variables to $row[0]. $row[1]...etc. The number is dependent on the position of the column/field and the order you selected them in your query. (Eg....$row[3] = the 'phone' column. It starts at "0".

 

 

the image needs to be withing this :

 

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>";     
     }  

 

in another table data tag, so <td>image will go here if f15 == 1</td>

I still haven't gotten this working, and need to...can anyone assist looking at my current code?

 

where I have <td>

    $img

</td>

 

I need an if/else like...

if ( $row[10] == 1 ) {

echo "<td>$img</td>";

} else {

echo "<td></td>";

}

 

but I don't think that is exactly right...

 


$connection = mysql_connect("localhost","username","pw"); 

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'>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><td id='header'><a href='#' onClick='FuncShowHide()'> More Info.</a></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>";
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.