Announcement

Collapse
No announcement yet.

while loop question....

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • while loop question....

    Hi,

    I have a question about a while loop:

    PHP Code:
    echo "$number is number<BR>";

    $t 0;
    while (
    $t <= "$number") {
    $estimate[$t] = ($width[$t] + $length[$t]);
    echo 
    "<BR>$estimate[$t] is estimate $t<BR>";
    $t++;

    If $number is set to 4 I get the following for my output:

    4 is number

    18 is estimate 0

    12 is estimate 1

    24 is estimate 2

    24 is estimate 3

    0 is estimate 4


    How do I change my code to make it say:

    4 is number

    18 is estimate 1

    12 is estimate 2

    24 is estimate 3

    24 is estimate 4

    //0 is estimate 4


    Thanks!

    Tim
    Five Star Review Script - Add reviews to your website!
    Mixed Martial Arts - Houston MMA Training
    Women's Self-Defense - Courses and DVDs available

  • #2
    Would this work?

    PHP Code:
    echo "$number is number<BR>";

    $t 0;
    while (
    $t <= "$number") {
    $estimate[$t] = ($width[$t] + $length[$t]);
    $temp $t 1;
    echo 
    "<BR>$estimate[$t] is estimate $temp<BR>";
    $t++;

    Comment


    • #3
      Hi,

      Partially, here's the result:

      4 is number

      18 is estimate 1

      132 is estimate 2

      48 is estimate 3

      60 is estimate 4

      0 is estimate 5

      It's showing 0 for estimate 5 when there is no number 5.

      Any other solutions?
      Five Star Review Script - Add reviews to your website!
      Mixed Martial Arts - Houston MMA Training
      Women's Self-Defense - Courses and DVDs available

      Comment


      • #4
        I've got it working...I changed it to a for loop:


        PHP Code:
        echo "$number is number<BR>";

        for(
        $t=0$t<= ($number 1); $t++) {
        $estimate[$t] = ($width[$t] + $length[$t]);
        $temp $t 1;
        echo 
        "<BR>$estimate[$t] is estimate $temp<BR>";

        If there's a better way of doing this, please let me know.

        Thanks,

        Tim
        Five Star Review Script - Add reviews to your website!
        Mixed Martial Arts - Houston MMA Training
        Women's Self-Defense - Courses and DVDs available

        Comment


        • #5
          You can also remove the = from the while loop.

          PHP Code:
          echo "$number is number<BR>";

          $t 0;
          while (
          $t "$number") {
          $estimate[$t] = ($width[$t] + $length[$t]);
          $temp $t 1;
          echo 
          "<BR>$estimate[$t] is estimate $temp<BR>";
          $t++;

          Comment


          • #6
            Originally posted by Zzed
            You can also remove the = from the while loop.

            PHP Code:
            echo "$number is number<BR>";

            $t 0;
            while (
            $t "$number") {
            $estimate[$t] = ($width[$t] + $length[$t]);
            $temp $t 1;
            echo 
            "<BR>$estimate[$t] is estimate $temp<BR>";
            $t++;


            um.. that was for less than or equal to. the way he did it before was the correct way. your for loop is..

            4 is number

            0 is estimate 1

            0 is estimate 2

            0 is estimate 3

            0 is estimate 4

            Comment

            widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
            Working...
            X