Jump to content

PHP default image if array is empty


freakunleash

Recommended Posts

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>';
		}
?> 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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>';
}

Link to comment
Share on other sites

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.