Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic display of images

Status
Not open for further replies.

misslois74

Programmer
Sep 27, 2008
63
PH
im currently working on a page wherein i have to dynamically display an image in which the value is coming from the database, if the field contains null value meaning no image stored on it i set a default image which represent that there is no current image now my problem is if the field is null its not displaying the default image i have set but if there is an image stored its displaying that image:

here is my sample code:

Code:
<?php 
		include "connection_dbase_settings.php";
		$membr_id = $_GET['mem_id'];
		$pfile = $_POST['pic'];
		$quer = "Select * from tbl_member_profile where member_id = $membr_id";
		$result = mysql_query($quer);
		while($row = mysql_fetch_array($result))
		{
			$picfile = $row['pic_file'];
?>
        <div class="mid_in" id="mid_in">
        	<div class="mid_in_left">
            <div class="mid_in_left_box2">
            
                   <?php if(isset($picfile))
				         { ?>
                   			  <table>
                   		 	  <tr><td><img src="<?php echo $row['pic_file'] ?>" width="137" height="175"/></td></tr></table>
                           	  <!--<img src="images/avatar.gif" width="137" height="137" />-->
             	  <?php  }    
              	   		 
						 else
				   		 {  ?>
				   			  <table>
                   			  <tr><td><img src="../images/nopic_192.gif" width="137" height="175"/></td></tr> </table>
                     	       <!--<img src="images/avatar.gif" width="137" height="201" /><img src="members/alvin.jpg" width="137" height="201" />-->
                          	   
             	   <?php }  ?>

im not sure where the problem lies...
hope somebody could help me with these...
thanks in advance..
 
You appear to be using both $_POST and $_GET at the same time.

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
I might be wrong but pic_file will be set since you are saying...

Code:
$picfile = $row['pic_file'];

Try either checking whether $pic_file is null or $pic_file==""

Better still check the value of $row['pic_file'].

You might also want to try something like

Code:
if(fileexists("path/to/images/{$pic_file}"){
   echo("<img src=/"path/to/images/{$pic_file}/" />");
} else {
   echo("<img src=/"path/to/images/default.jpg/" />");
}


Finally, to reduce your code clutter. Start outputting the table, row and cell and [i]then[/i] do the image check and output the appropriate HTML. That way you'll only have a single block of HTML to work with. Might make maintenance easier.

[COLOR=red]Tek-Tips Forums is Member Supported. [url=http://www.tek-tips.com/supportus.cfm]Click Here[/url] to donate[/color]

<honk>*:[COLOR=red]O[/color])</honk>

[i]Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril. [/i]
 
I would edit your code to this:
Code:
<?php
include "connection_dbase_settings.php";
$membr_id = $_GET['mem_id'];
$pfile = $_POST['pic'];
$quer = "Select * from tbl_member_profile where member_id = $membr_id";
$result = mysql_query($quer);

while($row = mysql_fetch_array($result))
{
$picfile = $row['pic_file'];
if (!file_exists(realpath($row['pic_file'])) $picfile = '<img src="../images/nopic_192.gif" style="width: 137px; height: 175px; border: 0px;" alt="" />';

?>
<div class="mid_in" id="mid_in">
<div class="mid_in_left">
<div class="mid_in_left_box2">

<table>
<tr>
<td><img src="<?php echo $picfile; ?>" style="width: 137px; height: 175px; border: 0px;" /></td>
</tr>
</table>
<?PHP } ?> <!-- End Do/While Loop! -->

I think it makes things easier to read by simplifying the code. Also, following Foamcow advise, make sure targeted images does exist.

Hope this helps!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
thanks southbeach now i was able to work it out already
 
yes i was able to work it out after going through all your postings so again thanks to all your postings!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top