PDA

View Full Version : php for no image, if image is not present



alli7
08-14-2011, 06:44 PM
Hi,
I'm trying to figure out how to tell Joomshopping cart to hide the image that says "no image" if an image is not found. I just want it to close up that space basically.

I tried "display: none" which doesn't show the image on the front end, but an image is still present in the code with an image class that applies padding that I don't want. All efforts to zero those out haven't worked... I'm a newbie to php so I am struggling. Any ideas? Below is the original code..."noimage;" is set up in Controller to tell it to use "noimage.gif"


<td class="image">
<a href = "<?php print $category->category_link;?>"><img class = "jshop_img" src = "<?php print $this->image_category_path;?>/<?php if ($category->category_image) print $category->category_image; else print $this->noimage;?>" alt="<?php print htmlspecialchars($category->name);?>" title="<?php print htmlspecialchars($category->name);?>" /></a>
</td>

Thank you!

JShor
08-15-2011, 01:01 AM
Use file_exists() to check if the image exists. Then print the image if it exists, or don't if it doesn't.



<td class="image">
<a href = "<?php print $category->category_link;?>">
<?php

if(file_exists($this->image_category_path.$category->category_image)) {
echo '<img class = "jshop_img" src = "'.$this->image_category_path.$category->category_image.'" alt="'.htmlspecialchars($category->name);.'" title="'.htmlspecialchars($category->name);.'" /></a>';
}

?>
</td>


This code will display the image if it exists, and display nothing if it doesn't.

See:
http://php.net/file_exists

traq
08-15-2011, 02:39 AM
I just want it to close up that space basically.

from what I can see from your code snippet, the problem likely has to do with the fact that the image is inside a table cell. The image is hidden, but the table cell remains the same size.

changing the layout of that cell would probably have an undesirable affect on the rest of the table.

(another good reason not to use tables for layout! :) )

JShor
08-15-2011, 03:27 AM
Oh yeah, I completely forgot about the table cell being there. Well, at least the image will be gone with the code I provided.

And yes, tables are a terrible way to design a layout.

alli7
08-15-2011, 04:14 PM
JShor: the code you provided didn't work unfortunately. The page didn't load. Thanks for the effort though.

And yes - I freaking hate tables too! I wish the creator of this extension had not made the whole eCommerce extension out of tables. Another part of my frustration...

JShor
08-15-2011, 04:34 PM
The page didn't load? Did you get an error?

I did make a couple of syntax errors on this line. Here's the corrected version:


<td class="image">
<a href = "<?php print $category->category_link;?>">
<?php

if(file_exists($this->image_category_path.$category->category_image)) {
echo '<img class = "jshop_img" src = "'.$this->image_category_path.$category->category_image.'" alt="'.htmlspecialchars($category->name).'" title="'.htmlspecialchars($category->name).'" /></a>';
}

?>
</td>


I'm pretty sure that will work, I don't see any reason for it not to.

alli7
08-15-2011, 04:42 PM
I didn't get an error... just a white page. I will try this. Thank you again! I appreciate your help! ;)

alli7
08-15-2011, 04:48 PM
I tried it and it worked, except that it made the images that were present also go away. Any thoughts? So close!

JShor
08-15-2011, 06:00 PM
Hmm... That's very odd. I want you to try this code, and tell me what it prints out (it should be a directory/file name after "File: "). Then I want you to go to that location and see if the file does exist or not and let us know the results.



print "File: ".$this->image_category_path.$category->category_image;

<td class="image">
<a href = "<?php print $category->category_link;?>">
<?php

if(file_exists($this->image_category_path.$category->category_image)) {
echo '<img class = "jshop_img" src = "'.$this->image_category_path.$category->category_image.'" alt="'.htmlspecialchars($category->name).'" title="'.htmlspecialchars($category->name).'" /></a>';
}

?>
</td>

alli7
08-15-2011, 11:22 PM
It says: print "File: ".$this->image_category_path.$category->category_image;

Which, if I'm understanding it all correctly... Your code should work, right?

JShor
08-15-2011, 11:31 PM
Whoops, I forgot to wrap <?php and ?> around the code. Try this:


<?php print "File: ".$this->image_category_path.$category->category_image; ?>

alli7
08-16-2011, 09:55 PM
It printed:

(for the image present)
File: website url here.../components/com_jshopping/files/img_categories89f8a4e11d4f13feb7bd01d0f27763c4.jpg[/url]

(for no image present)
File: website url here.../components/com_jshopping/files/img_categories[/url]

JShor
08-16-2011, 10:01 PM
Go to: (website url here)/components/com_jshopping/files/img_categories89f8a4e11d4f13feb7bd01d0f27763c4.jpg

Does it exist?

alli7
08-18-2011, 03:03 AM
No, it doesn't exist. Oh well. Maybe I should just accept the ugly "no image" until we have enough images to fill some of the spaces. Thank you for your help! You have been extremely helpful and patient. This system is interesting indeed. Oh well, it's free and you pay for what you get. ;)

JShor
08-18-2011, 09:09 PM
If it doesn't exist, then it is a problem with Joomcart/your file system. Maybe there is a setting in Joomcart for the directory for images? Maybe check if the folders exist?

alli7
08-21-2011, 12:20 PM
The image is visible on the page and when clicked it is linked to it's respective product page. It just doesn't exist as a stand-alone URL. I suppose because it's temporary? When I added the code it removed all of them for whatever reason. I guess the coding for this is far more complicated (or bizarre) than I thought. Oh well, some things should be left alone I suppose. Thanks again!