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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove Thumbnail When Image Missing 2

Status
Not open for further replies.

janeybad

IS-IT--Management
Sep 28, 2008
26
GB
Can anyone give me any ideas on how I can remove a thumbnail when the image doesn’t exist in the image folder?

Basically I have five small thumbs and one large image, which swap places when clicked but when the image is missing I would like the appropriate thumb not be shown.
 
Would you be able to show me where to place this within my code please. I quite new to PHP and having problems geting this all to work.

Kind Regards


JB XX
 
We don't know what your code looks like so it is difficult to imagine where this function would go within. You could try posting some of it if it is not confidential.
 
Not confidential and would appreciate your help.

Code:
  $imageFile  = "photos/aBRANCH_{$record["PropertyID"]}.jpg";
  $imageFile2 = "photos/bBRANCH_{$record["PropertyID"]}.jpg";
  $imageFile3 = "photos/cBRANCH_{$record["PropertyID"]}.jpg";
  $imageFile4 = "photos/dBRANCH_{$record["PropertyID"]}.jpg";
  $imageFile5 = "photos/pBRANCH_{$record["PropertyID"]}.jpg";



print "  <tr>\n";
print '    <td height="277" width="600">' . "\n";
print '    <div align="center">' . "\n";

 if(isset($_GET['image'])){

          echo "<img src='" . $_GET['image'] ."'>";

}




 else{
print "<img src=\"photos/pBRANCH_{$record["PropertyID"]}.jpg\"";
print ' border="0" height="287" width="416" alt="Property photo"   /></a> ' . "\n";
     }


  print "      <a href='pop_up101.php?propID={$record["PropertyID"]}&image=photos/aBRANCH_{$record["PropertyID"]}.jpg '\><img src=\"{$imageFile}\"";
  print ' border="0" height="70" width="70" alt="Property photo"   /></a> ' . "\n";
  print "      <a href='pop_up101.php?propID={$record["PropertyID"]}&image=photos/bBRANCH_{$record["PropertyID"]}.jpg'\><img src=\"{$imageFile2}\"";
  print ' border="0" height="70" width="70" alt="Property photo"   /></a> ' . "\n";
  print "      <a href='pop_up101.php?propID={$record["PropertyID"]}&image=photos/cBRANCH_{$record["PropertyID"]}.jpg'\><img src=\"{$imageFile3}\"";
  print ' border="0" height="70" width="70" alt="Property photo"   /></a> ' . "\n";
  print "      <a href='pop_up101.php?propID={$record["PropertyID"]}&image=photos/dBRANCH_{$record["PropertyID"]}.jpg'\><img src=\"{$imageFile4}\"";
  print ' border="0" height="70" width="70" alt="Property photo"   /></a> ' . "\n";
  print "      <a href='pop_up101.php?propID={$record["PropertyID"]}&image=photos/pBRANCH_{$record["PropertyID"]}.jpg'\><img src=\"{$imageFile5}\"";
  print ' border="0" height="70" width="70" alt="Property photo"   /></a> ' . "\n";
  print "    </div>  </td>\n";

Thank you for your help in advance!

JaneyBad xxx

 
this should work for you
Code:
<?php
$array = array('a', 'b', 'c', 'd', 'p');

//set up default Image here
$defaultImage = 'path/to/defaultImage.jpg';

//create a holding variable to avoid E_NOTICE 
$thumbsOutput = '';

foreach ($array as $i){
	$file = "photos/{$i}BRANCH_{$record['PropertyID']}.jpg";
	$file = file_exists($file) ? urlencode($file) : urlencode($defaultImage);
	$thumbsOutput .= <<<HTML

			<a href="pop_up101.php?propID={$record["PropertyID"]}&image={$file}">
				<img src="{$file}" border="0" height="70" width="70" alt="Property photo" />
			</a>
			
HTML;
}

//sort out main output
//perform a double check against spoofed image requests
$file = isset($_GET['image']) ? (in_array($_GET['image'], $imageFile) ? $_GET['image'] : $defaultImage ) : $imageFile['p'];
$mainOutput = <<<HTML
	
		<img src="$file" border="0" height="287" width="416" alt="Property photo"  />
HTML;

//display
echo <<<HTML
	<tr>
	<td height="277" width="600">
		<div align="center">
			$mainOutput
			$thumbsOutput
		</div>
	</td>
HTML;
?>
 

I get the five thumbnails and large image areas but no images.

Once I click on a thumbnail, I get the error:

Warning: in_array(): Wrong datatype for second argument in public_html/pop_up101.php on line 103

Which is the line below.
Code:
$file = isset($_GET['image']) ? (in_array($_GET['image'], $imageFile) ? $_GET['image'] : $defaultImage ) : $imageFile['p'];
$mainOutput = <<<HTML

The thumbnails still have no images but the large default image appears and none of the rest of the thumbnails seems to link correctly.

/pop_up101.php?propID=P1321&image=photos%2Fabranch_P1321.jpg

/pop_up101.php?propID=P1321&image=photos%2Fbbranch_P1321.jpg

I’m guessing that the %2F in the above link is causing the problem.

Also what do you suggest to include here?

//create a holding variable to avoid E_NOTICE
$thumbsOutput = '';

One last thing if cBranch image is not available, is there anyway of removing that thumb, or will this code do that.

Thank you for all your help so far really appreciate, as I am fairly new to all this.

Janey Bad XXX
 
the code replaces the thumbnail with a default image. the code below will remove the thumbnail altogether.
it also fixes the bug that you refer to.
Code:
<?php
$array = array('a', 'b', 'c', 'd', 'p');

//instantiate a holding variable to avoid E_NOTICE 
$thumbsOutput = '';
$imageFiles = array();

foreach ($array as $i){
    $file = "photos/{$i}BRANCH_{$record['PropertyID']}.jpg";
   
	//if the file exists then add it to the thumbnails.  otherwise don't
	if (file_exists($file)){
		$imageFiles[$i] = $file;
    	$thumbsOutput .= <<<HTML

            <a href="pop_up101.php?propID={$record["PropertyID"]}&image={$file}">
                <img src="{$file}" border="0" height="70" width="70" alt="Property photo" />
            </a>
            
HTML;
	}
}

//sort out main output
//perform a double check against spoofed image requests
$file = isset($_GET['image']) ? (in_array($_GET['image'], $imageFile) ? $_GET['image'] : $imageFile['p'] ) : $imageFile['p'];
$mainOutput = <<<HTML
    
        <img src="$file" border="0" height="287" width="416" alt="Property photo"  />
HTML;

//display
echo <<<HTML
    <tr>
    <td height="277" width="600">
        <div align="center">
            $mainOutput
            $thumbsOutput
        </div>
    </td>
HTML;
?>

 
Just got it to work, fantastic. just had to change $imageFile to $imageFiles.

Thank you very much
 
Can anyone help and let me know how to include the functions below.

If there is no c don't show b

If there is no d don't show c





 
Code:
$array = array('a', 'b', 'c', 'd', 'p');

//instantiate a holding variable to avoid E_NOTICE 
$thumbsOutput = '';
$imageFiles = array();

foreach ($array as $i){
    $file = "photos/{$i}BRANCH_{$record['PropertyID']}.jpg";
   
    //if the file exists then add it to the thumbnails.  otherwise don't
    if (file_exists($file)){
        $imageFiles[$i] = $file;
        $thumbsOutput .= <<<HTML

            <a href="pop_up101.php?propID={$record["PropertyID"]}&image={$file}">
                <img src="{$file}" border="0" height="70" width="70" alt="Property photo" />
            </a>
            
HTML;
    }[red] else {
    if ($i == 'c' && isset ($imageFiles['b'])) unset ($imageFiles['b']);
    if ($i == 'c' && isset ($imageFiles['c'])) unset ($imageFiles['c']);
    } [/red]
}
 
I,m sur you understood my question and the problem is my fault but couldn't get the code to work. Any chance you can have a quick look please.

Thank you in advance.xx

If there is no c image don't show the b image

If there is no d image don't show c image

 
sorry. must have been too tired.

Code:
<?php
$array = array('a', 'b', 'c', 'd', 'p');

//instantiate a holding variable to avoid E_NOTICE
$thumbsOutput = array();
$imageFiles = array();

foreach ($array as $i){
    $file = "photos/{$i}BRANCH_{$record['PropertyID']}.jpg";
   
    //if the file exists then add it to the thumbnails.  otherwise don't
    if (file_exists($file)){
        $imageFiles[$i] = $file;
        $thumbsOutput[$i] <<<HTML

            <a href="pop_up101.php?propID={$record["PropertyID"]}&image={$file}">
                <img src="{$file}" border="0" height="70" width="70" alt="Property photo" />
            </a>
            
HTML;
    } else {
    if ($i == 'c' && isset ($imageFiles['b'])) unset ($thumbsOutput['b']);
    if ($i == 'c' && isset ($imageFiles['c'])) unset ($thumbsOutput['c']);
    } 
}

//sort out main output
//perform a double check against spoofed image requests
$file = isset($_GET['image']) ? (in_array($_GET['image'], $imageFile) ? $_GET['image'] : $imageFile['p'] ) : $imageFile['p'];
$mainOutput = <<<HTML
    
        <img src="$file" border="0" height="287" width="416" alt="Property photo"  />
HTML;

//display
$_thumbsOutput = implode ("\r\n", $thumbsOutput);
echo <<<HTML
    <tr>
    <td height="277" width="600">
        <div align="center">
            $mainOutput
            $_thumbsOutput
        </div>
    </td>
HTML;
?>
 
Dear JPadie,
Thank you ever so much, I have got the function working and am very happy. Am trying to learn from your generous help and have another question. Is there anyway to remove the small blue link lines that I get in between each thumbnail.

Also just being curious how would I write the below in code.

if c missing show f.

Thank you for all the help XX
 
to answer the second question first:

first of all you need to add an 'f' into the mix. do this by adding and f to the array
Code:
$array = array([red]'f'[/red],'a', 'b', 'c', 'd',  'p');

note that the f needs to be added before the c.

i'm assuming that you do NOT want to show an f if there is a c.
so you would add this line to your conditional above

Code:
    if ($i == 'c' && isset ($imageFiles['b'])) unset ($thumbsOutput['b']);
    if ($i == 'd' && isset ($imageFiles['d'])) unset ($thumbsOutput['c']);
   [red] if ($i == 'c' && isset($imageFiles['c'])) unset ($thumbsOutput['f']);[/red]

to answer the first question (which is really css) put the following into your css

CSS:
div img {border:none;}
div a{ div a:visited, div a:link, div a:active, div a:hover {text-decoration:none;}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top