Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: php for no image, if image is not present

  1. #1
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default php for no image, if image is not present

    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"

    PHP Code:
    <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!

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Use file_exists() to check if the image exists. Then print the image if it exists, or don't if it doesn't.

    PHP Code:
    <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
    - Josh

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by alli7
    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! )

  4. #4
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

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

  5. The Following User Says Thank You to JShor For This Useful Post:

    alli7 (08-15-2011)

  6. #5
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

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

  7. #6
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    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:
    PHP Code:
    <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.
    - Josh

  8. #7
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Wink

    I didn't get an error... just a white page. I will try this. Thank you again! I appreciate your help!

  9. #8
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    I tried it and it worked, except that it made the images that were present also go away. Any thoughts? So close!

  10. #9
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    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.

    PHP Code:
    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>
    - Josh

  11. #10
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

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

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •