SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple variables in for statement

    Hi there,

    I've written some code to calculate a price based on some options choosed by the client.

    Now I would like to multiply this code by using a for statement and I am wondering if there is a way to have different variables for each run.

    What I mean:

    I have e.g.

    for (i=1; i <= 3; i++) {
    ...
    ammount1 = 0;
    ...
    }

    If i=1 the variable should be ammount1, if i=2 it should be ammount2 and so on. Is there a way to do this? ammount+i = 0 obviously doesn't work

    Thanks and regards,

    ASchweti

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use an array
    Code:
    var amounts = new Array();
    for ( var i = 0; i < 3; i++ )
    {
    	amounts[i] = 0;
    }
    Will that work?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am trying to solve this at this very moment, so I was playing around with arrays when I received your reply.

    So to me it sounds like the logical solution, but I am getting a "amount is not defined"

    Any idea why?

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe I found it?

    My smart O'Reilly book told me to add
    Code:
    amount = new Object();
    It seems to work. Is that the correct way to go?

    Thanks ASchweti

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd have to see more of your code to understand the error.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Beetle,

    just read my second post right after yours, that I solved this issue. I just was not sure if I solved it the correct way?!

    Anyhow I am facing another problem!

    In order to calculate different amounts based on user entries I am using PHP to create the appropriate amount of forms.

    The form fields are named e.g. color1 on the first form, color2 on the second form. In order to calculate the price I would need to get a hold of the values.

    So again if i=1 I need to check
    Code:
    document.form.color1.checked
    if i=2 I need to check
    Code:
    document.form.color2.checked
    an so on...

    Do you know what I mean? Is there a way to keep this flexible as well?

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes

    document.form.elements['color'+i].checked
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Beetle,

    works great! Thanks very much.

    Best regards,

    Alex


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
  •