Welcome Guest, Not a member yet? Register   Sign In
Suggestion for helper function: anchor_image
#1

[eluser]t'mo[/eluser]
I got tired of writing stuff like this:

Code:
<a href="&lt;?=site_url('A-G')?&gt;"><img src="&lt;?=site_url('res/a-g.png')?&gt;" border="0" alt="A-G"></a><br>
<a href="&lt;?=site_url('H-P')?&gt;"><img src="&lt;?=site_url('res/h-p.png')?&gt;" border="0" alt="H-P"></a><br>
...
<a href="&lt;?=site_url('A-Z')?&gt;"><img src="&lt;?=site_url('res/all.png')?&gt;" border="0" alt="All"></a>

so I wrote a little helper to do it for me:

Update 5/22/2008, 12:16 PM EDT - shortened this up a bit, removed check for defined function

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

// file: application/helpers/MY_url_helper.php

function anchor_image($href, $src, $alt = null)
{
  $altStr = 'alt="' . (null != $alt ? $alt : $href) . '"';
  $imgStr = '<img src="' . site_url($src) . '" border="0" ' . $altStr . '>';
  return anchor($href, $imgStr);
}

and now my view code looks like this:

Code:
&lt;?=anchor_image('A-G', 'res/a-g.png')?&gt;<br>
&lt;?=anchor_image('H-P', 'res/h-p.png')?&gt;<br>
...
&lt;?=anchor_image('A-Z', 'res/all.png', 'All')?&gt;

It could be improved by adding the ability to take arrays as params (for things like attributes), but it does what I need for now. I would really like to see something like this in the Array helper core; if any of you working on the core want to "steal" this code, feel free.


Messages In This Thread
Suggestion for helper function: anchor_image - by El Forum - 05-21-2008, 10:32 PM
Suggestion for helper function: anchor_image - by El Forum - 05-22-2008, 12:20 AM
Suggestion for helper function: anchor_image - by El Forum - 05-22-2008, 09:07 AM
Suggestion for helper function: anchor_image - by El Forum - 05-22-2008, 09:21 AM
Suggestion for helper function: anchor_image - by El Forum - 05-22-2008, 09:21 AM
Suggestion for helper function: anchor_image - by El Forum - 05-22-2008, 09:24 AM
Suggestion for helper function: anchor_image - by El Forum - 05-22-2008, 10:09 AM
Suggestion for helper function: anchor_image - by El Forum - 05-22-2008, 05:58 PM
Suggestion for helper function: anchor_image - by El Forum - 05-23-2008, 01:12 AM



Theme © iAndrew 2016 - Forum software by © MyBB