Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

How to reference an attachement?

Quote Reply
How to reference an attachement?
I added attached2_file column (image file) to category db, now I want to reference this column from the browser, so I did:

<IMG SRC="/cats/images/<%ID%>/<%ID%>-<%attached2_file%>">

but this way was not the best way to do that, so I read you can do something like:

<IMG SRC="<%db_cgi_url%>/jump.cgi?view=File&ID=<%ID%>">

and it should work, but in my case it did not work???!!! any idea why.. thanks...
The uploaded files in LinksSQL gets stored in the local directory in this format:
<%ID%>/<%ID%>-<%File_Name%>
example:
1/1-cat3.jpg

Thanks..
Quote Reply
Re: [Mark2] How to reference an attachement? In reply to
Hi,

Something like this is probably more simple =)

1) Add a new global, called "load_image_url". Put the following in it;

Code:
sub {

my ($ID,$col) = @_;

# make sure a fieldname and ID are provided...
if (!$ID || !$col) { return qq|You need to define the ID and fieldname.|; }

my $schema = $DB->table('Links')->cols;
my $path = $schema->{$col}->{'file_save_url'};
$path =~ s,/$,,;

my $tbl = $DB->table('Links');
my $fh = $tbl->file_info( $col, $ID );
my $rel_path;
if ($fh) {
$rel_path = $fh->File_URL;
} else { return ''; }
undef $fh;

$rel_path =~ s|([^\/]+)$|GT::CGI->escape( $1 )|e;

return $rel_path;

}

2) Call with:

Code:
<%if Field_Name%>
<%load_image_url($ID,'Field_Name')%>
<%endif%>

That should do it =)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Dec 7, 2005, 8:35 AM
Quote Reply
Re: [Andy] How to reference an attachement? In reply to
Thanks andy for the big code.
I got the following error message:
Unable to compile 'load_image_url': Global symbol "$field" requires explicit package name at (eval 27) line 9.
I do not know why, but also, I was looking for the jump.cgi URL that I indecated before, it seems less troublesome if it works.. as I want to reference it from other templates...
so is your code will return something like: /cars/images/1/1-<%image_name%> if this the case, then it will be great.
thanks again..
Quote Reply
Re: [Mark2] How to reference an attachement? In reply to
Hi,

No problem =)

The error was my fault =)

Change;

my $path = $schema->{$field}->{'file_save_url'};

..to;

my $path = $schema->{$col}->{'file_save_url'};

Quote:
I do not know why, but also, I was looking for the jump.cgi URL that I indecated before, it seems less troublesome if it works.. as I want to reference it from other templates...

Yes, and no =) If using this global (and making static pages), then you only have to generate an <img ..> tag, and not access jump.cgi every time someone views that image (for example, you have 25 images on a page, that would be 25 calls to jump.cgi, compared to 25 simple image calls, which would be a lot less strain on the server).

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to reference an attachement? In reply to
It will make negligible difference. You either load jump.cgi or you load your global - either way you are performing SQL queries.
Quote Reply
Re: [Hargreaves] How to reference an attachement? In reply to
Hi,

Did you actually just read my post? ;)

Using static with jump.cgi:

<img src="jump.cgi?ID=1;View=Field">
<img src="jump.cgi?ID=2;View=Field">
<img src="jump.cgi?ID=3;View=Field">
<img src="jump.cgi?ID=4;View=Field">
<img src="jump.cgi?ID=5;View=Field">
<img src="jump.cgi?ID=6;View=Field">
<img src="jump.cgi?ID=7;View=Field">
<img src="jump.cgi?ID=8;View=Field">
<img src="jump.cgi?ID=9;View=Field">
<img src="jump.cgi?ID=10;View=Field">

..using static with the global;

<img src="/full/;path/to/image1.gif">
<img src="/full/;path/to/image2.gif">
<img src="/full/;path/to/image3.gif">
<img src="/full/;path/to/image4.gif">
<img src="/full/;path/to/image5.gif">
<img src="/full/;path/to/image6.gif">
<img src="/full/;path/to/image7.gif">
<img src="/full/;path/to/image8.gif">
<img src="/full/;path/to/image9.gif">
<img src="/full/;path/to/image10.gif">

As you can see, the first call uses jump.cgi for EVERY time the page is loaded. However, the static one will only run the global xx times (when building), and then not have to be run again.

Its common sense Wink (obviously this wouldn't be the case if using mod_rewrite).

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to reference an attachement? In reply to
I'm aware thanks...not sure why the need for the sarcasm.

My comment still stands. jump.cgi isn't a resource hog.
Quote Reply
Re: [Andy] How to reference an attachement? In reply to
WOOW... worked sooo good, thanks for all your help.
I did go with the global as I tried the jump as stated above and some how it did not work. I did not see anything from it.
Now I need to copy the global to a second one to use in subcategory.html template to display the loaded image next to each category name... so I do not know what need to be changed (maybe database name/table name) so I can use a similar gloabl in category.
thanks thanks
Quote Reply
Re: [Mark2] How to reference an attachement? In reply to
Hi,

Glad to hear it :)

1) Add a new global, called "load_image_url_cat". Put the following in it;

Code:
sub {

my ($ID,$col) = @_;

# make sure a fieldname and ID are provided...
if (!$ID || !$col) { return qq|You need to define the ID and fieldname.|; }

my $schema = $DB->table('Category')->cols;
my $path = $schema->{$col}->{'file_save_url'};
$path =~ s,/$,,;

my $tbl = $DB->table('Category');
my $fh = $tbl->file_info( $col, $ID );
my $rel_path;
if ($fh) {
$rel_path = $fh->File_URL;
} else { return ''; }
undef $fh;

$rel_path =~ s|([^\/]+)$|GT::CGI->escape( $1 )|e;

return $rel_path;

}

2) Call with:

Code:
<%if Field_Name%>
<%load_image_url_cat($ID,'Field_Name')%>
<%endif%>

That should do it =)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to reference an attachement? In reply to
Thanks Andy, it was very helpful....
It worked just fine and returns: http://www.domain.com/...es/cats/7/7-cat1.jpg
Now can this global be modified to preduce the path to the image instead of the entire URL .. something like this: /images/cats/7-7cat1.jpg (means stip the http and the domain off)...
thank you again...
Quote Reply
Re: [Mark2] How to reference an attachement? In reply to
Hi,

No problem :P

To "strip" off those details, just add this;

Code:
$rel_path =~ s|\Qhttp://\E(.*?)/(.*?)|/$2|i;

..or if that doesn't work;

Code:
$rel_path =~ s|\Qhttp://www.yourdomain.com||i;

.. right before;

Code:
return $rel_path;

(untested, so no guarantees =))

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How to reference an attachement? In reply to
That worked automagically...... thanks Andy