Strange results from foreach loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stat_holyday@hotmail.com

    Strange results from foreach loop

    Greetings. I'm confused. I'm attemting to create dynamic buttons based
    on the count of questions in a database. The button has 3 states,
    blank, selected, and inactive. The script below works great, but with a
    strange bug that seems impossible to me.

    When the $question_count and $question_id are 2 or more values apart
    the script works, but when they are only 1 value apart, it bugs out.

    Examples...

    --
    $question_count = "5";
    $question_id = "2";

    Output:
    q1.gif
    q2_selected.gif
    q3.gif
    q4.gif
    q5.gif

    --
    $question_count = "4";
    $question_id = "2";

    Output:
    q1.gif
    q2_selected.gif
    q3.gif
    q4.gif
    q5_blank.gif

    --
    $question_count = "4";
    $question_id = "4";

    Output:
    q1.gif
    q2.gif
    q3.gif
    q4_selected.gif
    q5_blank.gif

    --
    $question_count = "4";
    $question_id = "3";

    Output:
    q1.gif
    q2.gif
    q3_selected.gif
    q4_selected.gif <--- WTF
    q5_blank.gif

    --
    End examples

    It's weird. All permutations work properly unless they're 1 apart from
    each other... 1 to 2, 2 to 3, 3 to 4, 4 to 5...

    Any ideas? What am I missing?

    Here's the code:

    $question_count = "3";
    $question_id = "2";

    foreach (range(1, 5) as $number) {

    if ($question_id == $number) {
    $status = "_selected" ;
    }

    elseif ($question_coun t < $number) {
    $status = "_blank";
    }

    elseif ($question_coun t $number) {
    $status = "";
    }
    echo "q" . $number . $status . ".gif<br>";
    }

    Thanks for you attention,
    Luc M. Forget

  • brephophagist

    #2
    Re: Strange results from foreach loop

    Seems like you should change $number here:
    elseif ($question_coun t < $number) {
    $status = "_blank";
    }
    >
    elseif ($question_coun t $number) {
    $status = "";
    }
    to some fixed question count you want to use to determine whether the
    button should be "inactive" or not.
    i.e.

    $question_count = "3";
    $question_id = "2";

    $count_inactive _threshold = "2";

    foreach (range(1, 5) as $number) {

    if ($question_id == $number) {
    $status = "_selected" ;
    }

    elseif ($question_coun t < $count_inactive _threshold) {
    $status = "_blank";
    }

    elseif ($question_coun t >= $count_inactive _threshold) {
    $status = "";
    }
    echo "q" . $number . $status . ".gif<br>";

    }


    You're comparing a variable that doesn't change in the loop against
    your iterator... from your description "based
    on the count of questions in a database" it seems you should be
    comparing a constant / unchanging variable against another variable
    pulled from the database.

    HTH--
    jason

    Comment

    • Rik

      #3
      Re: Strange results from foreach loop

      stat_holyday@ho tmail.com wrote:
      Greetings. I'm confused. I'm attemting to create dynamic buttons based
      on the count of questions in a database. The button has 3 states,
      blank, selected, and inactive. The script below works great, but with
      a strange bug that seems impossible to me.
      >
      When the $question_count and $question_id are 2 or more values apart
      the script works, but when they are only 1 value apart, it bugs out.
      >
      $question_count = "4";
      $question_id = "3";
      >
      Output:
      q1.gif
      q2.gif
      q3_selected.gif
      q4_selected.gif <--- WTF
      q5_blank.gif

      Ahum, it's not advisable to have a sig-seperator in your post
      (--(space)(newline )), as it will automatically trimmed out by correct
      newsreaders.

      To answer your question: You set $status isn't set when ($question_id !=
      $number && $question_count == $number), and $status will be like the last
      status from the last loop (hence doubling the selected in this case.

      Either start with an uncoditional $status=''; in your loop, or use:
      .....
      elseif ($question_coun t <= $number) {
      $status = "_blank";
      }
      .....

      --
      Rik Wasmus


      Comment

      • Pedro Graca

        #4
        Re: Strange results from foreach loop

        stat_holyday@ho tmail.com wrote:
        The script below works great, but with a
        strange bug that seems impossible to me.
        Take a piece of paper and a pen(cil).
        Make four columns for your variables.
        Follow the code ...

        For the example, assume I renamed the variables

        $qc = $question_count ;
        $qi = $question_id;
        $n = $number;

        Example:

        $qc | $qi | $n | $status |
        3 | 2 | 1 | | =$question_id == $number | *NO*
        | | | | $question_count < $number | *NO*
        | | | | $question_count $number | *YES*
        | | | "" | ==q1.gif
        | | 2 | "" | =$question_id == $number | *YES*
        | | | "_selected" | ==q1_selected.g if
        | | 3 | "_selected" | =$question_id == $number | *NO*
        | | | | $question_count < $number | *NO*
        | | | | $question_count $number | *NO*
        | | | | ==q1_selected.g if
        | | 4 | "_selected" | =$question_id == $number | *NO*
        | | | | $question_count < $number | *YES*
        | | | "_blank" | ==q1_blank.gif

        --
        I (almost) never check the dodgeit address.
        If you *really* need to mail me, use the address in the Reply-To
        header with a message in *plain* *text* *without* *attachments*.

        Comment

        • Jerry Stuckle

          #5
          Re: Strange results from foreach loop

          Rik wrote:
          >
          Ahum, it's not advisable to have a sig-seperator in your post
          (--(space)(newline )), as it will automatically trimmed out by correct
          newsreaders.
          >
          Proper newsreaders will only pick it up as a sig separator if they are
          the only characters on the line. Broken newsreaders, however, can pick
          it up anywhere.


          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          Working...