Welcome Guest, Not a member yet? Register   Sign In
src attribute of img tag referencing wrong location
#1

[eluser]johnmerlino[/eluser]
Hey all,

I have a helper method that gets called:

Code:
echo img('../public/images/' . $image->image_file_name, 'img_class');


        function img($path = '', $class = '', $attributes = array()){
            
             $img = "<img";
                        
            if ($class != ''){
                 $img .= " class=\"$class\"";
            }
            
            if($path != ''){
                $img .= " src=\"$path\"";
            }
                        
            $title = rtrim($path, '.jpg');
            if(is_array($attributes) AND count($attributes) > 0){
                foreach($attributes as $key => $value){
                    $img .= ' '.$key.'="'.$value.'"';
                }
            }
            else {
                $img .=  ' '.'title="'.$title.'"';
            }
                        
            $img .= ">".$title."</img>";
            return $img;
        }

If I am on page http://localhost/blogs/johnmerlino/category/3

That src attribute ends up pointing to:

http://localhost/blogs/johnmerlino/public/images/a.jpg

even though I want it to point to:

http://localhost/system/application/public/images/a.jpg

Thanks for response.
#2

[eluser]toopay[/eluser]
based by your approach :
Code:
function img($path = '', $class = '', $attributes = array())
    {
                 $img = '<img';
        if($path != '')
        {
            $img .= ' src="'.site_url($path).'" ';    
                }
        else
        {
            // You should give some default value
            $img .= ' src="'.site_url('public/images/no_pic.png').'" ';    
                        
        if ($class != '')
        {
            $img .= 'class="'.$class.'" ';
                }
                        
                $title = rtrim($path, '.jpg');
                if(is_array($attributes) AND count($attributes) > 0)
        {
                    foreach($attributes as $key => $value)
            {
                            $img .= $key.'="'.$value.'" ';
                    }
                }
                else
        {
                    $img .= 'title="'.$title.'" ';
                }
                        
                $img .= ' />'.$title;
                return $img;
        }
Now, you can called it with
Code:
echo img('public/images/' . $image->image_file_name, 'img_class');
#3

[eluser]toopay[/eluser]
Opps, sorry for that unreadable crap, forgot to use Post Replies instead Fast Replies, here you go :
Code:
function img($path = '', $class = '', $attributes = array())
    {
        $img = '<img';
        if($path != '')
        {
            $img .= ' src="'.site_url($path).'" ';    
        }
        else
        {
            // You should give some default value
            $img .= ' src="'.site_url('public/images/no_pic.png').'" ';  
        }  
                        
        if ($class != '')
        {
            $img .= 'class="'.$class.'" ';
        }
                        
        $title = rtrim($path, '.jpg');
        if(is_array($attributes) AND count($attributes) > 0)
        {
            foreach($attributes as $key => $value)
            {
                $img .= $key.'="'.$value.'" ';
            }
        }
        else
        {
            $img .= 'title="'.$title.'" ';
        }
                        
        $img .= ' />'.$title;
        return $img;
    }
#4

[eluser]toopay[/eluser]
Actually, in my opinion, it will be more 'clean' this way :
Code:
function img($args = array())
    {
        $res = '<img';
        foreach($args as $key => $value)
        {
           if($key == 'src')
           {
               $val = ($value != '') ? site_url($value) : site_url('public/images/no_pic.png');
           }
           else
           {
               $val = ($value != '') ? $value : '';
           }
           $res .= ' '.$key .'="'. $val .'"';
         }
         $res .= ' />';
         return $res;
    }
Now you can call it with something like
Code:
echo img(array('src'=>'public/images/'. $image->image_file_name, 'alt'=>'some caption','width'=>100));
#5

[eluser]InsiteFX[/eluser]
@toopay
You put the code tags in without going to POST_REPLY!
Remove the backslash!
Code:
// [code]
// [\/code]

InsiteFX
#6

[eluser]johnmerlino[/eluser]
@toopay thanks for response and the helpful alternative helper.
#7

[eluser]toopay[/eluser]
@InsiteFX, thx for your info. I already know that, but the problem is when we use "POST_REPLY" instead "FAST_REPLY", we could adjust the white space and tab better, because it's "Preview Post" feature.

@johnmerlino, you're welcome.




Theme © iAndrew 2016 - Forum software by © MyBB