bbuller
bbuller

Reputation: 195

Coldfusion - Modify value from database query

Excuse the ignorance of this question. Fairly new to coldfusion. I have a database query that returns the title of an image and also the file name of the image. I'd like to modify the file name of the image (#Image#) so that I can show the thumbnail I have created instead.

For example, all of my images names are in this form: Title-[HeightxWidth].jpg

And I'd like modify the results of the query for #Image# so that HeightXwidth is replaced with [Thumbnail], so that it will look like this: Title-[Thumbnail].jpg

I will be using links to both Title-[HeightxWidth].jpg and Title-[Thumbnail].jpg but don't want to have to input both values in my database.

Upvotes: 1

Views: 177

Answers (1)

Adam Cameron
Adam Cameron

Reputation: 29870

To answer the question you asked, one can simply reassign a value in a recordset in one of two ways:

// create some test data
q = queryNew("");
queryAddColumn(q, "col", "varchar", [1,2,3,4]);
writedump(var=q, label="Initial values");

// update it
querySetCell(q, "col", "one", 1);
q.col[4] = "four";
writedump(var=q, label="Updated values");

So there's either querySetCell() or just an assignment statement.

However you don't need to do this. You can either just create a new variable based on the value in the query, or just use an expression if you don't need to reuse that value:

<cfloop query="q">
    <cfoutput>
    Original value: #col#<br>
    <cfset withSuffix = col & "_suffix">
    With suffix: #withSuffix#<br>
    With prefix: prefix_#withSuffix#<br>
    <hr>
    </cfoutput>
</cfloop>

The "tricky" part of your specific requirement is to change HxW - eg 100x200 to a new value. This is most easily achieved using a regular expression replace, eg:

newValue = reReplace(oldValue, "\d+x\d+", "thumbnail", "all");

You could amke that regex pattern more specfic if you wanted to, but that would do the trick based on what you have said.

I work through CFML regular expressions at reasonable length on my blog.

Upvotes: 2

Related Questions