Results 1 to 10 of 10

Thread: Can not divide in ASP!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301

    Resolved Can not divide in ASP!

    This is driving me up the wall! I have been trying to get this ever so 'simple' code to work for the last hour!

    All I want to do is divide a number by 2

    this is what I have

    VB Code:
    1. <%
    2. ...
    3. if rstFound.bof = false then found_rows = rstFound.Fields("foundrows").value
    4. rstFound.Close
    5.  
    6. page_count = found_rows / 2
    7.  
    8. %>Found <%=page_count%>

    this does not work!! found_rows is 44 so page count should be 22. If I remove the /2 it works fine, else I get a 500 error.

    I've also tried * 0.5 and this does not work either
    Help!
    Last edited by Rick H; Apr 21st, 2006 at 12:56 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Can not divide in ASP!

    Do you set a default value for found_rows?

    Presumably you want to set it to 0 before the If.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301

    Re: Can not divide in ASP!

    Yup, the default for found_rows is zero.

    What is interesting is this works:

    VB Code:
    1. if rstFound.bof = false then found_rows = rstFound.Fields("foundrows").value
    2. rstFound.Close
    3.  
    4. found_rows=44
    5. page_count = found_rows / 2
    6.  
    7. %>Found <%=page_count%>

    and prints Found 22 on the page

    however this

    VB Code:
    1. if rstFound.bof = false then found_rows = rstFound.Fields("foundrows").value
    2. rstFound.Close
    3.  
    4. page_count = found_rows
    5.  
    6. %>Found <%=page_count%>

    prints Found 44 inidicating that found_rows is 44? but does not work with the divide by 2, but if I manually set it to 44 (top example) then this works????

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301

    Re: Can not divide in ASP!

    Fixed it! What a load of rubbish, only took me two hours

    VB Code:
    1. <%
    2. ...
    3. if rstFound.bof = false then found_rows = cint(rstFound.Fields("foundrows").value)
    4. rstFound.Close
    5.  
    6. page_count = found_rows / 2
    7.  
    8. %>Found <%=page_count%>

    cint was the secret - god knows why

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Can not divide in ASP!

    Presumably because the field foundrows is a String (or Text/Char) data type, rather than a numeric type.


    As you have the answer, could you please do us a little favour, and mark this thread as Resolved?
    (this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do this by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved".

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301

    Re: Can not divide in ASP!

    Thats what I thought - originally I was trying to use Val() but this is not supported in ASP.

    However Cint does not do string to integer conversions, it converts a numeric expression to an integer.

    (see http://psacake.com/web/functions.asp?id=5)

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Can not divide in ASP!

    Quote Originally Posted by Rick H
    However Cint does not do string to integer conversions, it converts a numeric expression to an integer.
    A string is a type of expression. A string such as "234" is a numeric one

    The following is perfectly valid in VB, and according to Microsoft should be exactly the same for VBScript (which is what ASP uses):
    VB Code:
    1. my_var = CInt("234")

    Have you checked what data type the "foundrows" field is?

  8. #8
    Member
    Join Date
    Mar 2006
    Posts
    55

    Re: Can not divide in ASP!

    If you're ever unsure about what type of variable you're pulling back from things then you can try using typename to help you out, so:

    msgbox Typename(found_rows)

    in this case.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301

    Re: Can not divide in ASP!

    I'm not sure of the type as its returned by

    Set rstFound = objConn.Execute("SELECT FOUND_ROWS() as foundrows")

    after doing a "SELECT SQL_CALC_FOUND_ROWS ...." MYSQL query

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301

    Re: Can not divide in ASP!

    Thanks l12ngo, I'll try that

Posting Permissions

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



Click Here to Expand Forum to Full Width