foreach

Discussion in 'PHP' started by MyVodaFone, Sep 8, 2010.

  1. #1
    I'm sorry, my head is not with it today...

    What am I doing wrong here, the result I am trying to achieve: 1 80% 2 80% 3 80% etc.. 42 times.

    
    <?php
    
    for($i=1;$i<=42;$i++){
    
    $p = array($percentage = 100 * $i = 80 / 100);
    
    foreach ($p as $percent) {
        echo $i, $percent,"%";
    }
    
    }
    
    ?>
    
    PHP:
     
    Last edited: Sep 8, 2010
    MyVodaFone, Sep 8, 2010 IP
  2. Eager2Seo

    Eager2Seo Member

    Messages:
    72
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #2
    NEVER EVER modify the for counter variable in your for loop. Lets start from there...
     
    Eager2Seo, Sep 8, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    What exactly are you trying to do/achieve?
     
    danx10, Sep 8, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    $i = 1-42
    $percentage = 100 * $i = 80 / 100);
    I will populate the variable "80" from a mysql statement

    ..and thus whatever "80" = I will print/echo that to a page
     
    MyVodaFone, Sep 8, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    Ok I understand the bolded text..but not the percentage - can you reply with an example, lets say if $i = 1; what do you expect $percentage to be? - if $i = 5 what would $percentage be?

    //Caution: im quite tired/sleepy so apologies.
     
    danx10, Sep 8, 2010 IP
  6. Eager2Seo

    Eager2Seo Member

    Messages:
    72
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #6
    <?php
    $p=array();
    $percentage =100;
    for($i=1;$i<=42;$i++){
    	$buff= $i;
    	$p[$i] = ($percentage * $buff = 80 / 100);
    }
    
    $i = 1;
    foreach ($p as $percent) {
        echo $i++.' ', $percent,"%";
    	echo "<br/>";
    
    
    }
    
    ?>
    Code (markup):
     
    Eager2Seo, Sep 8, 2010 IP
    MyVodaFone likes this.
  7. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #7
    Its a percentage thing.
    If you echo $percentage = 100 * 1 = 50 / 100); or $percentage = 100 * 2 = 50 / 100); up to 42 you will get 1 80% , 2 80% etc..
    Which is why I tried the ++, instead of writing out 42 equations
     
    MyVodaFone, Sep 8, 2010 IP
  8. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #8
    Thanks @Eager2Seo
     
    MyVodaFone, Sep 8, 2010 IP
  9. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #9
    Would that be because of a late night watching the England match yesterday, congrats BTW :)
     
    MyVodaFone, Sep 8, 2010 IP