What is wrong with this if test?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shelly

    What is wrong with this if test?

    OK, I am at a toral loss after having programmed for forty years.

    Here are my results:

    selHint_2=1
    It is clear that for the second time through the loop ($i=1) that
    $index=1 and $selHint_2=1, while for the first pass through ($i=0) $index=0
    and $selHint=1. Yet, the " selected " comes up for the first time and not
    for the second. It should be the other way around. Any clues?

    Shelly


    Here is the code:
    $numHint_2 = 2;
    $hint_2_Lookup = array(0, 1);
    $hint_2 = array();
    $hint_2[] = " Family member's name";
    $hint_2[] = " Mothers maiden name";
    $foo = ""; // put in for debugging only
    $tmpHint2 = array();
    $tmpHint2[] = '<td class="stdBold" ' . $readonly . '>';
    $tmpHint2[] = '<select name="hint_1" value="" >';
    for ($i=0; $i<$numHint_2; $i++) {
    $index = $hint_2_Lookup[$i];
    if ($index == $selHint_2) {
    $sel = " selected ";
    } else {
    $sel = "";
    }
    $tmpHint2[] = '<option value="' . $index . '"' . $sel . '>' .
    $hint_2[$i] . '</option>';
    // next line put in for debugging only
    $foo .= 'index=' . $index . ' option value="' . $index . '"' .
    $sel . $hint_2[$i]. "<br>";
    }
    $tmpHint2[] = '</select></td>';

    $selHint_2 = $_SESSION['vmHint_2']; // put in for
    debugging only
    echo "selHint_2= " . $selHint_2 . "<br>"; // put in for
    debugging only
    echo implode("\n",$t mpHint2)."\n";
    echo $foo; // put in for debugging only

    index=0 option value="0" selected Family member's name
    index=1 option value="1" Mothers maiden





  • Shelly

    #2
    Re: What is wrong with this if test?

    Oops, here is the output:

    selHint_2=1 index=0 option value="0" selected Family member's name
    index=1 option value="1" Mothers maiden



    "Shelly" <sheldonlg.news @asap-consult.com> wrote in message
    news:Z5mdnTRRsd h81y7eRVn-oQ@comcast.com. ..[color=blue]
    > OK, I am at a toral loss after having programmed for forty years.
    >
    > Here are my results:
    >
    > selHint_2=1
    > It is clear that for the second time through the loop ($i=1) that
    > $index=1 and $selHint_2=1, while for the first pass through ($i=0)
    > $index=0 and $selHint=1. Yet, the " selected " comes up for the first
    > time and not for the second. It should be the other way around. Any
    > clues?
    >
    > Shelly
    >
    >
    > Here is the code:
    > $numHint_2 = 2;
    > $hint_2_Lookup = array(0, 1);
    > $hint_2 = array();
    > $hint_2[] = " Family member's name";
    > $hint_2[] = " Mothers maiden name";
    > $foo = ""; // put in for debugging only
    > $tmpHint2 = array();
    > $tmpHint2[] = '<td class="stdBold" ' . $readonly . '>';
    > $tmpHint2[] = '<select name="hint_1" value="" >';
    > for ($i=0; $i<$numHint_2; $i++) {
    > $index = $hint_2_Lookup[$i];
    > if ($index == $selHint_2) {
    > $sel = " selected ";
    > } else {
    > $sel = "";
    > }
    > $tmpHint2[] = '<option value="' . $index . '"' . $sel . '>' .
    > $hint_2[$i] . '</option>';
    > // next line put in for debugging only
    > $foo .= 'index=' . $index . ' option value="' . $index . '"'
    > . $sel . $hint_2[$i]. "<br>";
    > }
    > $tmpHint2[] = '</select></td>';
    >
    > $selHint_2 = $_SESSION['vmHint_2']; // put in for
    > debugging only
    > echo "selHint_2= " . $selHint_2 . "<br>"; // put in for
    > debugging only
    > echo implode("\n",$t mpHint2)."\n";
    > echo $foo; // put in for debugging only
    >
    > index=0 option value="0" selected Family member's name
    > index=1 option value="1" Mothers maiden
    >
    >
    >
    >
    >[/color]


    Comment

    • Shelly

      #3
      Re: What is wrong with this if test?

      OK, I fixed it, but I know why it didn't work before.

      The if test stuff was in an include file (that is bracketed by <?php ?>.
      When I cut and pasted that code into inline code, it worked. The problem
      was that the include file was at the beginning of the page, so the test was
      before getting any values. Some times the stupid errors are the hardest to
      find.

      Shelly

      "Shelly" <sheldonlg.news @asap-consult.com> wrote in message
      news:e8CdnYQ_04 3p0S7eRVn-jw@comcast.com. ..[color=blue]
      > Oops, here is the output:
      >
      > selHint_2=1 index=0 option value="0" selected Family member's name
      > index=1 option value="1" Mothers maiden
      >
      >
      >
      > "Shelly" <sheldonlg.news @asap-consult.com> wrote in message
      > news:Z5mdnTRRsd h81y7eRVn-oQ@comcast.com. ..[color=green]
      >> OK, I am at a toral loss after having programmed for forty years.
      >>
      >> Here are my results:
      >>
      >> selHint_2=1
      >> It is clear that for the second time through the loop ($i=1) that
      >> $index=1 and $selHint_2=1, while for the first pass through ($i=0)
      >> $index=0 and $selHint=1. Yet, the " selected " comes up for the first
      >> time and not for the second. It should be the other way around. Any
      >> clues?
      >>
      >> Shelly
      >>
      >>
      >> Here is the code:
      >> $numHint_2 = 2;
      >> $hint_2_Lookup = array(0, 1);
      >> $hint_2 = array();
      >> $hint_2[] = " Family member's name";
      >> $hint_2[] = " Mothers maiden name";
      >> $foo = ""; // put in for debugging only
      >> $tmpHint2 = array();
      >> $tmpHint2[] = '<td class="stdBold" ' . $readonly . '>';
      >> $tmpHint2[] = '<select name="hint_1" value="" >';
      >> for ($i=0; $i<$numHint_2; $i++) {
      >> $index = $hint_2_Lookup[$i];
      >> if ($index == $selHint_2) {
      >> $sel = " selected ";
      >> } else {
      >> $sel = "";
      >> }
      >> $tmpHint2[] = '<option value="' . $index . '"' . $sel . '>'
      >> . $hint_2[$i] . '</option>';
      >> // next line put in for debugging only
      >> $foo .= 'index=' . $index . ' option value="' . $index . '"'
      >> . $sel . $hint_2[$i]. "<br>";
      >> }
      >> $tmpHint2[] = '</select></td>';
      >>
      >> $selHint_2 = $_SESSION['vmHint_2']; // put in for
      >> debugging only
      >> echo "selHint_2= " . $selHint_2 . "<br>"; // put in for
      >> debugging only
      >> echo implode("\n",$t mpHint2)."\n";
      >> echo $foo; // put in for debugging only
      >>
      >> index=0 option value="0" selected Family member's name
      >> index=1 option value="1" Mothers maiden
      >>
      >>
      >>
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Pedro Graca

        #4
        Re: What is wrong with this if test?

        Shelly wrote:[color=blue]
        > OK, I am at a toral loss after having programmed for forty years.[/color]

        Turn on error_reporting for all errors, warnings and notices.
        .... maybe you'll notice some uninitialized variable.

        Insert these two lines at the top of your script

        error_reporting (E_ALL);
        ini_set('displa y_errors', '1');

        <snip>[color=blue]
        > for ($i=0; $i<$numHint_2; $i++) {
        > $index = $hint_2_Lookup[$i];
        > if ($index == $selHint_2) {[/color]

        possible use of unassigned variable?
        [color=blue]
        > $sel = " selected ";
        > } else {
        > $sel = "";
        > }
        > $tmpHint2[] = '<option value="' . $index . '"' . $sel . '>' .
        > $hint_2[$i] . '</option>';
        > // next line put in for debugging only
        > $foo .= 'index=' . $index . ' option value="' . $index . '"' .
        > $sel . $hint_2[$i]. "<br>";
        > }
        > $tmpHint2[] = '</select></td>';
        >
        > $selHint_2 = $_SESSION['vmHint_2']; // put in for
        > debugging only[/color]

        You're assigning to $selHint_2 after you've used the variable in the
        test above.
        [color=blue]
        > echo "selHint_2= " . $selHint_2 . "<br>"; // put in for
        > debugging only
        > echo implode("\n",$t mpHint2)."\n";
        > echo $foo; // put in for debugging only[/color]

        Happy debugging :)

        --
        Mail to my "From:" address is readable by all at http://www.dodgeit.com/
        == ** ## !! ------------------------------------------------ !! ## ** ==
        TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
        may bypass my spam filter. If it does, I may reply from another address!

        Comment

        • Geoff Berrow

          #5
          Re: What is wrong with this if test?

          Message-ID: <O8SdnRNy_JlO9y 7eRVn-hQ@comcast.com> from Shelly contained
          the following:
          [color=blue]
          >Some times the stupid errors are the hardest to
          >find.[/color]

          This seems to happen quite a bit. I suppose what happens is that you
          work on something for ages and get totally bogged down and then, in
          desperation, make a post to the group. Then, knowing you have to wait
          for a reply, you put the problem to one side and relax. The relaxation
          enables you to see the solution.

          --
          Geoff Berrow (put thecat out to email)
          It's only Usenet, no one dies.
          My opinions, not the committee's, mine.
          Simple RFDs http://www.ckdog.co.uk/rfdmaker/

          Comment

          • Tim Roberts

            #6
            Re: What is wrong with this if test?

            "Shelly" <sheldonlg.news @asap-consult.com> wrote:[color=blue]
            >
            >OK, I am at a toral loss after having programmed for forty years.[/color]

            If you programmed for forty years, you must be tired. ;)
            --
            - Tim Roberts, timr@probo.com
            Providenza & Boekelheide, Inc.

            Comment

            • Stan McCann

              #7
              Re: What is wrong with this if test?

              "Shelly" <sheldonlg.news @asap-consult.com> wrote in
              news:O8SdnRNy_J lO9y7eRVn-hQ@comcast.com:[color=blue]
              > Some times the
              > stupid errors are the hardest to find.
              >
              > Shelly[/color]

              Fact! Those stupid errors are always messing up *my* code. ;)
              Try sprinkling a little Modula into a C program. That's why I like
              PHP, handling C syntax or Perl if I goof on the last couple of
              languages I've done much coding in. Of course, it's forgiving nature
              can sometimes get us in trouble too.

              --
              Stan McCann "Uncle Pirate" http://stanmccann.us/pirate.html
              Webmaster/Computer Center Manager, NMSU at Alamogordo
              http://alamo.nmsu.edu/ There are 10 kinds of people.
              Those that understand binary and those that don't.

              Comment

              Working...