Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with array

    I have some code im trying to make smaller using a for loop and an array. I basically have a form with 4 drop down boxes that are either minor or substantial, if minor is selected 5 should be added to the total
    if substantial 10 should be added for each drop down box. My original code that works is:

    PHP Code:
    if($a1 == "Minor")
    {
        
    $count $count 5;
    }
    else if (
    $a1 == "Substantial")
    {
        
    $count $count 10;
    }


    if(
    $b1 == "Minor")
    {
        
    $count $count 5;
    }
    else if (
    $b1 == "Substantial")
    {
        
    $count $count 10;
    }

    if(
    $c1 == "Minor")
    {
        
    $count $count 5;
    }
    else if (
    $c1 == "Substantial")
    {
        
    $count $count 10;
    }


    if(
    $d1 == "Minor")
    {
        
    $count $count 5;
    }
    else if (
    $d1 == "Substantial")
    {
        
    $count $count 10;

    the new code which doesn't work is:

    PHP Code:
    $letter = array(
    0  => $a1 =  $_POST['a1'], 
    1  => $b1 $_POST['b1'], 
    2  => $c1 $_POST['c1'],
    3  => $d1 $_POST['d1']
    );

    $x 0;

    for (
    $i 0$i count($letter); $i++)
    {    
    if(
    $letter[$x++] == "Minor")
            {
                
    $count $count 5;
            }
    if (
    $letter[$x++] == "Substantial")
            {
                
    $count $count 10;
            }

    Im starting to think its not a problem with the array but some problem in the count. Any pointers in the right direction would be appreciated.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Can you change your html to make your selects arrays? e.g. name="selections[]"
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You don't need that $x variable; you are incrementing it twice each loop and you already have a iterator variable defined by the for() loop:
    PHP Code:
    $letter_count count($letter);
    for (
    $i 0$i $letter_count$i++) 
    {     
         if(
    $letter[$i] == 'Minor'
         { 
                
    $count += 5
         } 
         elseif (
    $letter[$i] == 'Substantial'
         { 
                
    $count += 10
         } 



  •  

    Posting Permissions

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