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
https://forums.phpfreaks.com/topic/259750-php-default-image-if-array-is-empty/
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

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

@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.

 

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

Archived

This topic is now archived and is closed to further replies.

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