freakunleash Posted March 26, 2012 Share Posted March 26, 2012 Hi All, Need help. I'm strugling to figure out how to write the code to display default image if no image is available. I'm able to display image if data is available. Not able figure how to echo if/else statment. Below is the code <?php function perDetails($var1, $result, $var2){ $dft_img = 'lib/images/name.png'; echo '<div class="desc">'.$var1.'</div>'; $color="1"; echo '<table align="center" cellpadding="2" cellspacing="1">'; while($row = mysql_fetch_array($result)){ $id = $row['pid']; $name = $row['pname']; $img = $row['pphoto_localurl']; $poster = str_replace("./", "lib/", $img); if($color==1){ echo '<tr bgcolor="#FFC600"><td><img src="'.$poster.'" width="32" height="42" alt="'.$name.'"</td><td><a href="persons.php?det='.$var2.'&perid='.$id.'"> '.$name.'" </a></td></tr>'; $color="2"; } else { echo '<tr bgcolor="#C6FF00"><td><img src="'.$poster.'" width="32" height="42" alt="'.$name.'"</td><td><a href="persons.php?det='.$var2.'&perid='.$id.'"> '.$name.'" </a></td></tr>'; $color="1"; } } echo '</table>'; } ?> Quote Link to comment Share on other sites More sharing options...
smerny Posted March 26, 2012 Share Posted March 26, 2012 try something like this while($row = mysql_fetch_array($result)){ $id = $row['pid']; $name = $row['pname']; $img = (file_exists($row['pphoto_localurl']) ? $row['pphoto_localurl'] : "default.png"); $poster = str_replace("./", "lib/", $img); ... } edit: changed from checking whats in the db to if the file actually exists Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 26, 2012 Share Posted March 26, 2012 You are putting the if/else in the wrong place. You should use it to define the image function perDetails($var1, $result, $var2) { $dft_img = 'lib/images/name.png'; echo '<div class="desc">'.$var1.'</div>'; echo '<table align="center" cellpadding="2" cellspacing="1">'; while($row = mysql_fetch_array($result)) { $img = ($row['pphoto_localurl']!='') ? $row['pphoto_localurl'] : $dft_img; $poster = str_replace("./", "lib/", $img); echo "<tr bgcolor='#FFC600'>\n"; echo "<td><img src='{$poster}' width='32' height='42' alt='{$row['pname']}'</td>\n"; echo "<td><a href='persons.php?det={$var2}&perid={$row['pid']}'> {$row['pname']} </a></td>\n"; echo "</tr>\n"; } echo '</table>'; } Quote Link to comment Share on other sites More sharing options...
freakunleash Posted March 26, 2012 Author Share Posted March 26, 2012 @Psycho Thanks I got the result... I used below line to display default image... $img = ($row['pphoto_localurl']!='') ? $row['pphoto_localurl'] : $dft_img; if/else statment in the code is for alternating the row color not to provide default image. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 26, 2012 Share Posted March 26, 2012 @Psycho Thanks I got the result... I used below line to display default image... $img = ($row['pphoto_localurl']!='') ? $row['pphoto_localurl'] : $dft_img; if/else statment in the code is for alternating the row color not to provide default image. Then you don't need the if/else for the row color either function perDetails($var1, $result, $var2) { $dft_img = 'lib/images/name.png'; echo '<div class="desc">'.$var1.'</div>'; echo '<table align="center" cellpadding="2" cellspacing="1">'; while($row = mysql_fetch_array($result)) { $img = ($row['pphoto_localurl']!='') ? $row['pphoto_localurl'] : $dft_img; $poster = str_replace("./", "lib/", $img); $color = ($color=='#FFC600') ? '#C6FF00' : '#FFC600'; echo "<tr bgcolor='{$color}'>\n"; echo "<td><img src='{$poster}' width='32' height='42' alt='{$row['pname']}'</td>\n"; echo "<td><a href='persons.php?det={$var2}&perid={$row['pid']}'> {$row['pname']} </a></td>\n"; echo "</tr>\n"; } echo '</table>'; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.