Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help Needed Displaying a image in table format

    Hello all im rather new to php so go easy on me. Im trying to display data in a html table which is being gathered from my database. The part im stuck on is getting the image to show. The images are stored in a folder and inside the database it references the path name. Im struggling to see where ive gone wrong so any help is greatly appriciated, Thanks.


    Code:
    		
    
    <?php
    mysql_connect("pdb6.awardspace.com", "", "") or die(mysql_error());
    mysql_select_db("") or die(mysql_error());
    
    $result = mysql_query("SELECT * FROM products");
    
    echo "<table border='1'>
    <tr>
    <th>Name</th>
    <th>Cost</th>
    <th>Availability</th>
    <th>image</th>
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['Name'] . "</td>";
      echo "<td>" . $row['cost'] . "</td>";
      echo "<td>" . $row['quantity'] . "</td>";
      $image = $row["image"];
      echo "<td>". "<img src='$image' alt='product' width='85' height='85'/>"."</td>";
      echo "</tr>";
      }
    echo "</table>";
    
    mysql_close($con);
    
    ?>

  • #2
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by msx112 View Post
    Hello all im rather new to php so go easy on me. Im trying to display data in a html table which is being gathered from my database. The part im stuck on is getting the image to show. The images are stored in a folder and inside the database it references the path name. Im struggling to see where ive gone wrong so any help is greatly appriciated, Thanks.


    Code:
    		
    
    <?php
    mysql_connect("pdb6.awardspace.com", "", "") or die(mysql_error());
    mysql_select_db("") or die(mysql_error());
    
    $result = mysql_query("SELECT * FROM products");
    
    echo "<table border='1'>
    <tr>
    <th>Name</th>
    <th>Cost</th>
    <th>Availability</th>
    <th>image</th>
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['Name'] . "</td>";
      echo "<td>" . $row['cost'] . "</td>";
      echo "<td>" . $row['quantity'] . "</td>";
      $image = $row["image"];
      echo "<td>". "<img src='$image' alt='product' width='85' height='85'/>"."</td>";
      echo "</tr>";
      }
    echo "</table>";
    
    mysql_close($con);
    
    ?>
    The image doesn't show up, because you can't just put a PHP variable into an echo. PHP will just say it's just a word with a dollar sign in front of it.

    Here's you solution:


    Code:
    <?php
    mysql_connect("pdb6.awardspace.com", "", "") or die(mysql_error());
    mysql_select_db("") or die(mysql_error());
    
    $result = mysql_query("SELECT * FROM products");
    
    echo "<table border='1'>
    <tr>
    <th>Name</th>
    <th>Cost</th>
    <th>Availability</th>
    <th>image</th>
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['Name'] . "</td>";
      echo "<td>" . $row['cost'] . "</td>";
      echo "<td>" . $row['quantity'] . "</td>";
      $image = $row["image"];
      echo "<td>". "<img src='" . $image . "' alt='product' width='85' height='85'/>"."</td>";
      echo "</tr>";
      }
    echo "</table>";
    
    mysql_close($con);
    
    ?>
    Have fun coding

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Riko15 View Post
    The image doesn't show up, because you can't just put a PHP variable into an echo. PHP will just say it's just a word with a dollar sign in front of it.
    That is incorrect. Yes you can use variables inside strings being printed using echo as long as the strings use double quotes.

    http://php.net/manual/en/language.types.string.php
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by msx112 View Post
    Hello all im rather new to php so go easy on me. Im trying to display data in a html table which is being gathered from my database. The part im stuck on is getting the image to show. The images are stored in a folder and inside the database it references the path name. Im struggling to see where ive gone wrong so any help is greatly appriciated, Thanks.


    Code:
            
     
    <?php
    mysql_connect("pdb6.awardspace.com", "", "") or die(mysql_error());
    mysql_select_db("") or die(mysql_error());
     
    $result = mysql_query("SELECT * FROM products");
     
    echo "<table border='1'>
    <tr>
    <th>Name</th>
    <th>Cost</th>
    <th>Availability</th>
    <th>image</th>
    </tr>";
     
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['Name'] . "</td>";
      echo "<td>" . $row['cost'] . "</td>";
      echo "<td>" . $row['quantity'] . "</td>";
      $image = $row["image"];
      echo "<td>". "<img src='$image' alt='product' width='85' height='85'/>"."</td>";
      echo "</tr>";
      }
    echo "</table>";
     
    mysql_close($con);
     
    ?>

    Are you seeing broken images on the page? If so then the path is not correct. Look at the source code of the generated page and look at the path being created and it should be pretty obvious on what to do to fix it.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help all sorted now


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •