Default image using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hcanning
Forum Newbie
Posts: 3
Joined: Mon Jul 27, 2009 9:35 am

Default image using php

Post by hcanning »

Hi Guys,
I need to set a default image if a user chooses not to upload one. "image_small" refers to an image filepath eg images/myimage.jpg that the user chooses to upload. How can I adjust the code below so that if image_small is null then image_small=images/defaultimage.jpg? See lines 34 and 35
Thanks

Code: Select all

 
// product images 
        $t->set_var("images", "");
        $sql  = " SELECT COUNT(*) FROM " . $table_prefix . "ads_images ";
        $sql .= " WHERE item_id=" . intval($item_id);
        $sql .= " AND image_small IS NOT NULL ";
        $db->query($sql);
        $db->next_record();
        $total_images = $db->f(0);
        if ($total_images > 0) {
            $tabs[] = "images";
            if (!$use_tabs || $tab == "images") {
                if ($use_tabs) {
                    $t->set_var("title_images", "");
                } else {
                    $t->global_parse("title_images", false, false, true);
                }
 
                $image_number = 0;
                $sql  = " SELECT image_id, image_title, image_small, image_large, image_description  ";
                $sql .= " FROM " . $table_prefix . "ads_images ";
                $sql .= " WHERE item_id=" . intval($item_id);
                $sql .= " AND image_small IS NOT NULL ";
                $db->query($sql);
                while ($db->next_record()) {
                    $image_number++;
        
                    $image_id = $db->f("image_id");
                    $image_title = $db->f("image_title");
                    $image_small = $db->f("image_small");
                    $image_large = $db->f("image_large");
                    if (isset($restrict_ads_images) && $restrict_ads_images) { 
                        if ($image_small && !preg_match("/^http\:\/\//", $image_small)) {
                            $image_small = "image_show.php?ad_image_id=".$image_id."&type=small"; 
                        }
                        if ($image_large && !preg_match("/^http\:\/\//", $image_large)) {
                            $image_large = "image_show.php?ad_image_id=".$image_id."&type=large"; 
                        }
                    }
                    if (!strlen($image_large)) {
                        $image_large = $image_small;
                    }
                    $image_description = $db->f("image_description");
      
                    $t->set_var("image_title", $image_title);
                    $t->set_var("image_small", $image_small);
                    $t->set_var("image_width", "");
                    $t->set_var("image_height", "");
                    $t->set_var("image_large", $image_large);
                    $t->set_var("image_description", $image_description);
                    $t->parse("images_cols", true);
                    if ($image_number % 2 == 0) {
                        $t->parse("images_rows", true);
                        $t->set_var("images_cols", "");
                    }
                }       
                if ($image_number % 2 != 0) {
                    $t->parse("images_rows", true);
                }
 
                $t->parse("images", false);
            } 
        }
        // end images 
 
chinstroker
Forum Newbie
Posts: 5
Joined: Sun Apr 26, 2009 3:01 pm

Re: Default image using php

Post by chinstroker »

Why not just

Code: Select all

if ( empty($image_small) )
{
    $image_small = 'images/defaultimage.jpg';
}
 
Post Reply