Welcome Guest, Not a member yet? Register   Sign In
URL Helper Extended - anchor_img() added
#1

[eluser]Isern Palaus[/eluser]
Hello,

I am improving my website and doing all I can for a simply use of the features included in views and this includes the anchors on the images.

I ever use a images with a especific width and height but I have added the attributes on the image for distributing on this forum and using in their scripts.

I based my code on the original anchor() and the img(). It is only a modificaction including one inside the other.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Anchor Image Link
*
* Creates an anchor on a image based on the local URL.
* Based on the CodeIgniters original Anchor Link and Image (img()).
*
* Author(s): Isern Palaus <[email protected]>
*
* @access    public
* @param    string    the URL
* @param    string    the image URL
* @param    string    the image alt
* @param    mixed    a attributes
* @param    mixed    img attributes
* @return    string
*/    

if ( ! function_exists('anchor_img'))
{
    function anchor_img($uri = '',$img = '', $alt = '', $aatt = '',$iatt = '')
    {
        $alt = (string) $alt;
        $imgatt = "";
    
        if ( ! is_array($uri))
        {
            $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
        }
        else
        {
            $site_url = site_url($uri);
        }
        
        if ( ! is_array($img))
        {
            $image = ( ! preg_match('!^w+://! i', $img)) ? site_url($img) : $img;
        }
        else {
            $image = site_url($img);
        }
        
        if($iatt) {
            foreach ($iatt as $k=>$v)
            {
                $imgatt .= " $k=\"$v\" ";
            }
        }
    
        if ($alt == '')
        {
            $alt = $site_url;
        }

        if ($aatt != '')
        {
            $aatt = _parse_attributes($aatt);
        }

        return '<a href="'.$site_url.'"'.$aatt.'><img src="'.$image.'" alt="'.$alt.'"'.$imgatt.'/></a>'."\n";
    }
}
?&gt;

How to use it? It is very simple to use:

Code:
&lt;?php
$img = array(
    'width' => '800px',
    'height' => '900px',
);
?&gt;
&lt;?=anchor_img('test.html','images/logo.png','The Alt on the Image','',$iatt);?&gt;

Also...
Code:
&lt;?=anchor_img('test.html','images/logo.png');?&gt;
... will work fine.


Wish you like it.

Regards,
-- Isern Palaus
#2

[eluser]stuffradio[/eluser]
Cool stuff!

You should have used a different variable name than $aatt Big Grin
#3

[eluser]Isern Palaus[/eluser]
[quote author="stuffradio" date="1212985636"]Cool stuff!

You should have used a different variable name than $aatt Big Grin[/quote]

Hello stuffradio,

What kind of variable did you use for this kind of function? Because there is a A Attributes and Image Attributes :S.

Thank you,
-- Isern Palaus
#4

[eluser]stuffradio[/eluser]
well I'm just saying that the name of the variable doesn't sound like it means much to the general user. A good variable has a name that makes sense.

Example: Images could be
Code:
$img

Links
Code:
$link
etc.

Edit: I do understand your variables now though.
#5

[eluser]tjmthome[/eluser]
Hi, I recently realize about the need of a helper like the one you are sharing, but it has a little problem:

im working with $config['url_suffix'] = ".html";
then; the output shows some like this:
Code:
<a href="http://localhost/prods/my_prods.html"><img src="prod_shoe.jpg.html" alt="boots"></a>




Theme © iAndrew 2016 - Forum software by © MyBB