SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2002
    Location
    Lake Tahoe
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with a "for loop"

    Please bear with me I am a PHP hacker and not a coder....yet.

    Anyhow, I am trying to simplify some current code that I have to make it easier to update. I have a set of variables that I define in my code that increment from 1..2..3..4 in order depending on the section and want to make it into a for loop that I can just set the start number and ending number for each section.

    Currently...
    Code:
    $question1 = $REProw['quest_1'];
    $question2 = $REProw['quest_2'];
    $question3 = $REProw['quest_3'];
    $question4 = $REProw['quest_4'];
    ...
    $total=abs($question1) + abs($question2)...
    and I would like to have something like this..

    Code:
    $totalI = 0;
    for ($i=1;$i <= 21; $i++){
    $totalI = $totalI + ${"question".$i};
    }
    As you can see I haven't gotten far so any help in finishing this would be appreciated.

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function TotalI$end$start 1)
    $totalI 0// Reset to zero
    for ($i $start;$i <= $end$i++){
        
    $totalI += $REProw["quest_" $i]; //Add the value
    }

    retrun $totalI;

    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please bear with me I am a PHP hacker and not a coder....yet.
    Uchm...

    PHP Code:
    $total 0;
    for (
    $i 1$length count($REProw)+1$i $length$i++) {
        
    $total += $REProw['quest_'.$i];
    }

    // Or even faster

    $total array_sum($REProw); 
    FOR SALE: 1 set of morals, never used, will sell cheap

  4. #4
    SitePoint Member
    Join Date
    Aug 2002
    Location
    Lake Tahoe
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help. I had to make a fix with it but it appears to be working somewhat. Now can I display $totalI just by echoing it seeing how it is wrapped in a function?

    Code:
    <?php echo $totalI; ?>

    Quote Originally Posted by byron3@earthlink View Post
    PHP Code:
    function TotalI$end$start 1)
    $totalI 0// Reset to zero
    for ($i $start;$i <= $end$i++){
        
    $totalI += $REProw["quest_" $i]; //Add the value
    }

    retrun $totalI;


  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to provide the Upper limit of your loop and a start for your loop if you do not want to use 1 as the start.

    PHP Code:
    function TotalI$In_r$end$start 1)
    $totalI 0// Reset to zero
    for ($i $start;$i <= $end$i++){
        
    $totalI += $In_r["quest_" $i]; //Add the value
    }

    retrun $totalI;

    I hope that fix was being able to pass in an array....

    PHP Code:

    <?php echo $totalI$Input_r$End_value$Start_value ); ?>
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  6. #6
    SitePoint Member
    Join Date
    Aug 2002
    Location
    Lake Tahoe
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what I ended up with...

    PHP Code:
    function TotalI$end 21$start 1) {
    $totalI 0// Reset to zero
    for ($i $start;$i <= $end$i++){
        
    $totalI += $REProw["quest_" $i]; //Add the value
    }
    return 
    $totalI;

    and then to display totalI on the page..

    <?php echo $totalI; ?>



    I see that you changed your previous example???


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
  •