1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Passing value through URL then creating a variable for pagination

Discussion in 'Programming' started by MissJane, Oct 1, 2009.

  1. #1
    Hi All,

    I am new to Coldfusion and need help with passing a value via the URL and then setting a variable with the value.

    What I want to do is pass a category id number through the URL (which I have done successfully). I then have a product page that has displays all the products in the category with the category id that was passed in the URL. The problem that I now have is that I have pagination on this page - displaying 10 products per page. When I click on the next page I lose the category id that was originally passed in the URL, therefore returning an error. I know it works if I put the category id into the cfparam default, for example: <cfparam name="URL.cat" default="6">

    I believe the problem is in the first little bit of code, as follows:

    
    <cfparam name="URL.cat" default="">
    
    <cfset CurrentPage=GetFileFromPath(GetTemplatePath())>
    <cfquery name="catalogue" datasource="#request.SiteDSN#" username="#request.DSNUser#" password="#request.DSNPasswd#">
    SELECT *
    FROM products
    WHERE prod_cat_id = #cat_id#
    </cfquery>
    
    <cfset perpage = 10>
    <cfparam name="url.start" default="1">
    <cfif not isNumeric(url.start) or url.start lt 1 or url.start gt catalogue.recordCount or round(url.start) neq url.start>
        <cfset url.start = 1>
    </cfif>
    
    Code (markup):
    Please help! I am going crazy..
     
    MissJane, Oct 1, 2009 IP
  2. Paul_K

    Paul_K Greenhorn

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #2
    Do you mean like this:

    WHERE prod_cat_id = #url.cat#

    BTW you should always be using <cfqueryparam />

    eg:

    WHERE prod_cat_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#url.cat#" />
     
    Paul_K, Oct 1, 2009 IP
  3. MissJane

    MissJane Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry I copy and pasted the wrong code....I have bolded the difference in the code below...

    
    <cfparam name="URL.cat_id" default="" />
    
    <cfset CurrentPage=GetFileFromPath(GetTemplatePath())>
    <cfquery name="catalogue" datasource="#request.SiteDSN#" username="#request.DSNUser#" password="#request.DSNPasswd#">
    SELECT *
    FROM products
    [B]WHERE prod_cat_id = #URL.cat#[/B]
    </cfquery>
    
    <cfset perpage = 10>
    <cfparam name="url.start" default="1" />
    <cfif not isNumeric(url.start) or url.start lt 1 or url.start gt catalogue.recordCount or round(url.start) neq url.start>
        <cfset url.start = 1>
    </cfif>
    
    Code (markup):
    Thank you Paul K for your response, I have noted the tip about closing the cfparam tag. Also, the code you recommended didn't work...any other suggestions?
     
    MissJane, Oct 1, 2009 IP
  4. Paul_K

    Paul_K Greenhorn

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #4
    What errors are you seeing?
     
    Paul_K, Oct 2, 2009 IP
  5. MissJane

    MissJane Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The error I see is as follows:

    Element CAT is undefined in URL.

    The error occurred in 'website address here xxxxxxxxxxxxx': line 9

    7 : SELECT *
    8 : FROM products
    9 : WHERE prod_cat_id = #URL.cat#
    10 : </cfquery>



    Cheers...
     
    MissJane, Oct 4, 2009 IP
  6. Paul_K

    Paul_K Greenhorn

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #6
    If you have:
    <cfparam name="URL.cat" default="" />
    Above the query that wont happen. Are you 100% sure your on the correct page? Your not including the query somewhere else?

    Also note ists cat not cat_id

    <cfparam name="URL.cat" default="" />
     
    Paul_K, Oct 4, 2009 IP
  7. FCM

    FCM Well-Known Member

    Messages:
    669
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    155
    #7
    You should always use <cfqueryparam> like the guys above said.

    I also don't like the fact that you are using request! You should use application instead it much easier to manage (IMO).

    Next if you are going to be limiting it to 10 per page you should define this in your query eg.)

    LIMIT 10;

    Instead of doing the <cfparam> you can try something like this

    <cfif isDefined("url.cat")>
    <cfset varname = url.cat >
    <cfelse>
    <cfset varname = "default">
    </cfif>

    I actually prefer doing this rather than the <cfparam> I have had bad experiences with it but some good ones to I just prefer to make it as secure as possible... Well best of luck
     
    FCM, Oct 8, 2009 IP