SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    australia
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question how to call a function from within a database

    Hi to everyone,

    I was wondering if anyone here knows how to call a function/ sub loaded from a database.

    For example:

    I hava a page that loads, the page name, description, picture ect.. that has been enterered into the database from the administrators CMS via a form.

    I would like for the adminstrator to have more power and be able to call a script/ function by enetering the script name <% call_this_script() %> into the database through the CMS via a form.

    How would i go about calling this on the page after being loaded from a database.

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    australia
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe i should be more specific,

    Currently when the information loads from the database using a sub on the page it does exactly what i want it to do and loads the information: i.e.

    <&#37; Load_Home_Page_Info() %>

    <% = ds("HomePage") %> - will show the home page Name
    <% = ds("Intro") %> - will show the home page Introduction
    <% = ds("Image") %> - will show the home page Image
    <% = ds("Description") %> - will show the home page Description

    This is where the problem is...
    <% = ds("ShowScript") %> - Should show the function called <% call_this_script() %> But it will only show the the text <% call_this_script() %> and not perform the function.

    When calling the function do i need to replace some characters or does can this only be done when the page loads on its own and not within another script loading from a database.

    Please help, there must some gurus left here. A simple responce would be nice with any ideas.

    Thanks in advance

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    australia
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shocking, that there is no one here with any advice. This forum has gone stale.

  4. #4
    SitePoint Zealot gidday's Avatar
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi dallas

    Nothing like winning hearts and minds.

    I don't know if this helps but have you tried <% Call Response.Write(ds("ShowScript")) %>.

    Just an idea. I haven't tested it.
    Joe
    "All that is necessary for the triumph of evil
    is that good men do nothing."

  5. #5
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you're saying the code does not work as desired... but since we don't know what the code is, we can't really help - can we.

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    australia
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply gidday, i will try that.

    Mr Webber, as i mentioned earlier in the post above, the script works fine for pulling the information form a db, and loads on the page correctly.

    My question was?

    I have another calling function <&#37; call_this_script() %> i would like the administrator to be able to add via form to the db, this is currently being added with the information for that page, which all works fine. So there is no problem with adding.

    I am using a calling function <% call_HomePage() %> that works great and is hardcoded onto the page.

    The problem is...

    When i try to execute the script <% call_this_script() %> which is being loaded from <% call_HomePage() %> with the page content it only shows the text <% call_this_script() %> and does nopt perform the call. All other information shows. i.e. Page Name, Intro, Description, Image

    If i use the call <% call_this_script() %> on the page hardcoded it loads fine.

    Any other suggestions would be great.

    I hope this makes more sense, I would upload the files but i think it would be a waste of time for everyone since the scripts work fine when hardcoded.

  7. #7
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It seems to me that ds() is a function to get data from the database then echo it (response.write) to the output. As such, it is NOT POSSIBLE for that function to determine that the data is ASP code to be executed, unless you rewrite it to do so.

    What you need is the eval() function - use it at your own risk!
    Ian Anderson
    www.siteguru.co.uk

  8. #8
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    response.write will not execute ASP code contained in the output.

    What you're trying to accomplish used to be handled with something called "ProcessForm"

    The template file can contain ASP scripts.

    A script between <% and %> delimiters is treated just like other text in the template and copied into the output file. If the output file is an ASP document, the script will run when the output file is executed.

    If you want script in the template to be executed during the call to ProcessForm, place your code between special <%% and %%> delimiters. Since these scripts are executed before the template data is saved in the output file, the results get saved in the output file, usually as standard text.

    http://www.fentyler.co.uk/iishelp/ii...p/comp0gkt.htm

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    australia
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, i didnt think you could execute a function through another function after the page had loaded without some other form of scripting. Is there any other way to do this. How about if i were to use something like..

    <&#37; If ds("scriptName") = "[ABC]" then replace "[" with "<%" & "]" with "%>" %>

    I know that when using something like this above (im not sure exactly how its written) you can collect additional information from another script and have it displayed. I have seen this in many newsletter sendout functions and automated store reciept functions, where when sending out an email to customers you can enter tags something like [customerDetails] [shippingDetails] and it will load the information based on that customer and tag when sending the email.

    All im trying to acomplish is to load a set of predefined scripts that have already been hardcoded into the file and can be called using something like [scriptName1] [scriptName2] instead of <%Call_This_script%>.

    As mentioned earlier the script works when all is hardcoded, but we want it to only be added to the page to call the secondary scripts according to the tag enetred from the admin.

    here is the hardcoded page script that will call information form the database, the information used will pull down that pages information. When loading this information the tags entered should call secondary scripts that will provide additional information relating to that page.

    db connection is included in another file

    <% Sub loadHomeContent()
    if dMode <> true then on error resume next
    SQLQuery = "SELECT homePageID, homePageName, homeTitle, homeIntroduction, homeDescription, showHomeImage, homeImageType, homeImage, showScripts FROM tblHomePage WHERE PageID=7 ;"
    Set ds = objConn.Execute(SQLQuery)

    If not ds.eof and not ds.bof then
    homePageID = ds("homePageID")

    homePageName = ds("homePageName")
    homeTitle = ds("homeTitle")
    homeIntroduction = ds("homeIntroduction")
    homeDescription = ds("homeDescription")
    showHomeImage = ds("showHomeImage")
    homeImageType = ds("homeImageType")
    homeImage = ds("homeImage")
    homePageamount = ds("homePageamount")
    showScripts = ds("showScripts ")
    end if
    ' ds.close
    ' set ds = nothing
    ' objConn.close
    ' set objConn = nothing
    end sub %>


    To write it to the page i am using..

    <% loadHomeContent()%>

    <%= ds("homeTitle") ' shows the home page title %>, <%= ds("homeDescription") ' shows the home page Description %>, ect... <br> <br>

    <%= ds("showScripts") ' shows the home page secondary scripts %> - this is where the tags are in the db i.e. [scriptName1] [scriptName2] so instead of showing the text [scriptName1] [scriptName2] i want it to replace the "[" & "]" characters and call the functions as listed.

    So it would then call functions <% scriptName1() %> <% scriptName2 ()%>

    I hope this helps everyone understanding what im trying to do. I thought this would make it more flexible for the administrators to be able to customize the functions on the page and have them listed the way they prefer and not based on the standard hardcoded calling functions. this way if they would prefer to have script2 before script1 they can or even if they didnt want to have script2 showing they only have to enter script1.

    If someone has any idea on the best way to do this, i am very interested. im no expert but i understand things if they are explained to me.

    Thanks to everyone

  10. #10
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Replacing text is not the same as running code.

    You can't store live ASP code in a database and have it run "on demand". ASP wasn't designed to work that way.

    The closest solution to use in your approach is the one I already posted above and that comes with limitations.

    Most people simply use Server Side Includes, (SSI) for holding reusable functions. ASP was designed to allow for SSI.

  11. #11
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    australia
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry webber, but i think your still a litle confused on what im really looking for. so here is a more simplified explination.

    Ok, so i know that it can be done this way, which is close to what i want but it will need some adjustments as there are additional script calls in the same text field.

    <&#37; if ds("showScripts") ="script1" then %><% script1()%><% end if %>

    This works if it is only the word "script1" in that field

    which is basic right

    Now how can i loop through a single db Field Name "showScripts" input type = "text"

    Note: There is only 1 record under that id so i cant loop through id's!

    For example.

    Say Field Name "showScripts" had text in it that was

    [script1] [script2] [script3] [script4]

    seperated by [ & ]

    In 1 field on 1 record, how do i loop through this.

    I could define what the scripts were on the page;

    script1 = script1()
    script2 = script2()

    any advice would be great, i hope this explains it easier.

  12. #12
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your database only contains "flags" to select which function to run, then this should help you:

    Code ASP:
    <%
    if instr(ds("showScripts"), "script1" ) > 0 then
          script1()
    elseif instr(ds("showScripts"), "script2" ) > 0 then
          script2()
    ' repeat as needed
    end if 
    %>

  13. #13
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    australia
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again webber, but its only taking the first script name as you are using the elseIf command,

    Im assuming that the "instr" command is used to gather the information from within the same field. How can i collect the additional script names within that field,

    i.ve tried removing the elseif and replaced it with endif and then began with the if statment again for the next script name but this gives me a Type mismatch error where the second if statment starts.

    <&#37;
    if instr(ds("showScripts"), "script1" ) > 0 then
    script1()
    endiif

    if instr(ds("showScripts"), "script2" ) > 0 then ' error is at this line when done like this
    script2()
    ' repeat as needed
    end if
    %>

    Also is there a way that it can be listed Ascending Or Decending or based on the way the script names are inputed.

    Thanks for all your help.

  14. #14
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    australia
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Webber im sorry, you were right, i figured out what i was doing wrong,

    Your example works fine, i had removed the [ ] and replaced it with a space in the db so it was giving an error, now that i have removed the space and replaced it with [ ] leaving out any spaces it seems to work fine without the elseif statement.

    also is there a reason for "> 0" apart from the field is greater than 0, is it the same as "" and isnt "" used for text and 0 used for numbers. confused

    i would still love to know how to have them listed in relation to the way they have been inputed to the database.

    If you can help me with this i will leave this post alone. As im sure you all cant wait to get rid of me on this

    By the way Webber, Thanks again your the man! I knew it was simple and not so interesting for others but it takes good people like you to help out the confused and willing to learn like me.

    Thanks heaps.

  15. #15
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    InStr Function

    Returns the numerical position of the first occurrence of one string within another.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •