What's wrong the the logic in this code?

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

    What's wrong the the logic in this code?

    Greetings:

    I am attempting to get conditional output based on POSTed form data.
    If the posted value is either the key or value of an array, $x=key and
    $q=foo. elseif it is neither key nor value(I tried || too) , $q=bar.
    Even though $x is properly assigned the new value, $q always ends up
    being bar. Abstractions aside, here's the code: (note ## comments
    added to this post)

    <snip>
    $search = $_POST[search];
    $sort = $_POST[sortby];
    $states = array(
    "AL" ="Alabama",
    "AK" ="Alaska",
    "AZ" ="Arizona",
    ...
    );
    ##echo $search here outputs the POSTed input
    foreach ($states as $key =$value)

    if ($search == $value || $search == $key) {
    $search = $key;
    ##echo $search here outputs the new value, when applicable
    $q = "
    SELECT
    organization.*, contacts.*
    FROM organization
    LEFT JOIN contacts
    ON organization.or g_id = contacts.org_id
    OR organization.or g_id = 0
    WHERE organization.st ate
    = '$search'
    OR contacts.state
    = '$search'
    ORDER BY '$sort' ASC";

    } elseif ($search != $value && $search != $key) { ##I tried || here too.
    ##echo $search here outputs the new value too, when applicable
    $q = "
    SELECT
    CONCAT(contacts .firstname,' ',contacts.last name) AS fullname,
    organization.*, contacts.*
    FROM organization
    LEFT JOIN contacts
    ON organization.or g_id = contacts.org_id
    OR organization.or g_id = 0
    WHERE organization.st ate
    LIKE '%$search%'
    OR organization.or gname
    LIKE '%$search%'
    OR contacts.state
    LIKE '%$search%'
    OR contacts.lastna me
    LIKE '%$search%'
    OR contacts.firstn ame
    LIKE '%$search%'
    ORDER BY '$sort' ASC";
    }

    $result = mysql_query($q)
    or die('Query Failed: ' . mysql_error());
    </snip>

    Where is this evaluation breaking down and why? Thanks in advance.
    --

    Regards,

    Jeff Gardner
    _______________ ____________

    "Contrary to popular belief, Unix is user friendly. It just happens
    to be very selective about who its friends are." --Kyle Hearn
  • Hans 'pritaeas' Pollaerts

    #2
    Re: What's wrong the the logic in this code?

    "Jeff Gardner" <user@example.t ldwrote in message
    news:FNrOg.2076 $IA.204@newssvr 11.news.prodigy .com...
    Greetings:
    >
    I am attempting to get conditional output based on POSTed form data.
    If the posted value is either the key or value of an array, $x=key and
    $q=foo. elseif it is neither key nor value(I tried || too) , $q=bar.
    Even though $x is properly assigned the new value, $q always ends up
    being bar. Abstractions aside, here's the code: (note ## comments
    added to this post)
    >
    <snip>
    $search = $_POST[search];
    $sort = $_POST[sortby];
    $states = array(
    "AL" ="Alabama",
    "AK" ="Alaska",
    "AZ" ="Arizona",
    ...
    );
    ##echo $search here outputs the POSTed input
    foreach ($states as $key =$value)
    >
    if ($search == $value || $search == $key) {
    $search = $key;
    ##echo $search here outputs the new value, when applicable
    $q = "
    SELECT
    organization.*, contacts.*
    FROM organization
    LEFT JOIN contacts
    ON organization.or g_id = contacts.org_id
    OR organization.or g_id = 0
    WHERE organization.st ate
    = '$search'
    OR contacts.state
    = '$search'
    ORDER BY '$sort' ASC";
    >
    } elseif ($search != $value && $search != $key) { ##I tried || here too.
    ##echo $search here outputs the new value too, when applicable
    $q = "
    SELECT
    CONCAT(contacts .firstname,' ',contacts.last name) AS fullname,
    organization.*, contacts.*
    FROM organization
    LEFT JOIN contacts
    ON organization.or g_id = contacts.org_id
    OR organization.or g_id = 0
    WHERE organization.st ate
    LIKE '%$search%'
    OR organization.or gname
    LIKE '%$search%'
    OR contacts.state
    LIKE '%$search%'
    OR contacts.lastna me
    LIKE '%$search%'
    OR contacts.firstn ame
    LIKE '%$search%'
    ORDER BY '$sort' ASC";
    }
    >
    $result = mysql_query($q)
    or die('Query Failed: ' . mysql_error());
    </snip>
    >
    Where is this evaluation breaking down and why? Thanks in advance.
    --
    >
    Regards,
    >
    Jeff Gardner
    Instead of the elseif you could just use else. Why check again, it's either
    a state value, or something else.

    Hans


    Comment

    • Jeff Gardner

      #3
      Re: What's wrong the the logic in this code?

      Hans 'pritaeas' Pollaerts wrote:
      "Jeff Gardner" <user@example.t ldwrote in message
      news:FNrOg.2076 $IA.204@newssvr 11.news.prodigy .com...
      >Greetings:
      >>
      >I am attempting to get conditional output based on POSTed form data.
      >If the posted value is either the key or value of an array, $x=key and
      >$q=foo. elseif it is neither key nor value(I tried || too) , $q=bar.
      >Even though $x is properly assigned the new value, $q always ends up
      >being bar. Abstractions aside, here's the code: (note ## comments
      >added to this post)
      >>
      ><snip>
      >$search = $_POST[search];
      >$sort = $_POST[sortby];
      >$states = array(
      > "AL" ="Alabama",
      > "AK" ="Alaska",
      > "AZ" ="Arizona",
      > ...
      > );
      >##echo $search here outputs the POSTed input
      >foreach ($states as $key =$value)
      >>
      >if ($search == $value || $search == $key) {
      >$search = $key;
      >##echo $search here outputs the new value, when applicable
      >$q = "
      >SELECT
      >organization.* , contacts.*
      >FROM organization
      >LEFT JOIN contacts
      >ON organization.or g_id = contacts.org_id
      >OR organization.or g_id = 0
      >WHERE organization.st ate
      >= '$search'
      >OR contacts.state
      >= '$search'
      >ORDER BY '$sort' ASC";
      >>
      >} elseif ($search != $value && $search != $key) { ##I tried || here too.
      >##echo $search here outputs the new value too, when applicable
      >$q = "
      >SELECT
      >CONCAT(contact s.firstname,' ',contacts.last name) AS fullname,
      >organization.* , contacts.*
      >FROM organization
      >LEFT JOIN contacts
      >ON organization.or g_id = contacts.org_id
      >OR organization.or g_id = 0
      >WHERE organization.st ate
      >LIKE '%$search%'
      >OR organization.or gname
      >LIKE '%$search%'
      >OR contacts.state
      >LIKE '%$search%'
      >OR contacts.lastna me
      >LIKE '%$search%'
      >OR contacts.firstn ame
      >LIKE '%$search%'
      >ORDER BY '$sort' ASC";
      >}
      >>
      >$result = mysql_query($q)
      >or die('Query Failed: ' . mysql_error());
      ></snip>
      >>
      >Where is this evaluation breaking down and why? Thanks in advance.
      >--
      >>
      >Regards,
      >>
      >Jeff Gardner
      >
      Instead of the elseif you could just use else. Why check again, it's either
      a state value, or something else.
      >
      Hans
      >
      >
      I added the elsif because I get the same output with an else. That's
      why I suspect that there is a logic problem. For some reason, the second
      $q variable is populated regardless of the previous conditional
      statement being satisfied or not. If a state is not input, $search
      remains untouched and the 2nd $q is populated as expected. If a state
      is input, $search is modified but the 2nd $q is still populated.

      --

      Regards,

      Jeff Gardner
      _______________ ____________

      "Contrary to popular belief, Unix is user friendly. It just happens
      to be very selective about who its friends are." --Kyle Hearn

      Comment

      • Jerry Stuckle

        #4
        Re: What's wrong the the logic in this code?

        Jeff Gardner wrote:
        Greetings:
        >
        I am attempting to get conditional output based on POSTed form data.
        If the posted value is either the key or value of an array, $x=key and
        $q=foo. elseif it is neither key nor value(I tried || too) , $q=bar.
        Even though $x is properly assigned the new value, $q always ends up
        being bar. Abstractions aside, here's the code: (note ## comments
        added to this post)
        >
        <snip>
        $search = $_POST[search];
        $sort = $_POST[sortby];
        $states = array(
        "AL" ="Alabama",
        "AK" ="Alaska",
        "AZ" ="Arizona",
        ...
        );
        ##echo $search here outputs the POSTed input
        foreach ($states as $key =$value)
        >
        if ($search == $value || $search == $key) {
        $search = $key;
        ##echo $search here outputs the new value, when applicable
        $q = "
        SELECT
        organization.*, contacts.*
        FROM organization
        LEFT JOIN contacts
        ON organization.or g_id = contacts.org_id
        OR organization.or g_id = 0
        WHERE organization.st ate
        = '$search'
        OR contacts.state
        = '$search'
        ORDER BY '$sort' ASC";
        >
        } elseif ($search != $value && $search != $key) { ##I tried || here too.
        ##echo $search here outputs the new value too, when applicable
        $q = "
        SELECT
        CONCAT(contacts .firstname,' ',contacts.last name) AS fullname,
        organization.*, contacts.*
        FROM organization
        LEFT JOIN contacts
        ON organization.or g_id = contacts.org_id
        OR organization.or g_id = 0
        WHERE organization.st ate
        LIKE '%$search%'
        OR organization.or gname
        LIKE '%$search%'
        OR contacts.state
        LIKE '%$search%'
        OR contacts.lastna me
        LIKE '%$search%'
        OR contacts.firstn ame
        LIKE '%$search%'
        ORDER BY '$sort' ASC";
        }
        >
        $result = mysql_query($q)
        or die('Query Failed: ' . mysql_error());
        </snip>
        >
        Where is this evaluation breaking down and why? Thanks in advance.
        You're problem is you are setting $q each time through the loop, then
        using it outside of the loop. $q will always have the last value set in
        the loop.

        If $search matched the last state in your list, you would get the value
        from your "then" clause. Any other time you'll get the value from your
        "then" clause.

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

        Comment

        • Jeff Gardner

          #5
          Re: What's wrong the the logic in this code?

          Jeff Gardner wrote:
          Hans 'pritaeas' Pollaerts wrote:
          >"Jeff Gardner" <user@example.t ldwrote in message
          >news:FNrOg.207 6$IA.204@newssv r11.news.prodig y.com...
          >>Greetings:
          >>>
          >>I am attempting to get conditional output based on POSTed form data.
          >>If the posted value is either the key or value of an array, $x=key and
          >>$q=foo. elseif it is neither key nor value(I tried || too) , $q=bar.
          >>Even though $x is properly assigned the new value, $q always ends up
          >>being bar. Abstractions aside, here's the code: (note ## comments
          >>added to this post)
          >>>
          >><snip>
          >>$search = $_POST[search];
          >>$sort = $_POST[sortby];
          >>$states = array(
          >> "AL" ="Alabama",
          >> "AK" ="Alaska",
          >> "AZ" ="Arizona",
          >> ...
          >> );
          >>##echo $search here outputs the POSTed input
          >>foreach ($states as $key =$value)
          >>>
          >>if ($search == $value || $search == $key) {
          >>$search = $key;
          >>##echo $search here outputs the new value, when applicable
          >>$q = "
          >>SELECT
          >>organization. *, contacts.*
          >>FROM organization
          >>LEFT JOIN contacts
          >>ON organization.or g_id = contacts.org_id
          >>OR organization.or g_id = 0
          >>WHERE organization.st ate
          >>= '$search'
          >>OR contacts.state
          >>= '$search'
          >>ORDER BY '$sort' ASC";
          >>>
          >>} elseif ($search != $value && $search != $key) { ##I tried || here too.
          >>##echo $search here outputs the new value too, when applicable
          >>$q = "
          >>SELECT
          >>CONCAT(contac ts.firstname,' ',contacts.last name) AS fullname,
          >>organization. *, contacts.*
          >>FROM organization
          >>LEFT JOIN contacts
          >>ON organization.or g_id = contacts.org_id
          >>OR organization.or g_id = 0
          >>WHERE organization.st ate
          >>LIKE '%$search%'
          >>OR organization.or gname
          >>LIKE '%$search%'
          >>OR contacts.state
          >>LIKE '%$search%'
          >>OR contacts.lastna me
          >>LIKE '%$search%'
          >>OR contacts.firstn ame
          >>LIKE '%$search%'
          >>ORDER BY '$sort' ASC";
          >>}
          >>>
          >>$result = mysql_query($q)
          >>or die('Query Failed: ' . mysql_error());
          >></snip>
          >>>
          >>Where is this evaluation breaking down and why? Thanks in advance.
          >>--
          >>>
          >>Regards,
          >>>
          >>Jeff Gardner
          >>
          >Instead of the elseif you could just use else. Why check again, it's
          >either
          >a state value, or something else.
          >>
          >Hans
          >>
          >>
          I added the elsif because I get the same output with an else. That's
          why I suspect that there is a logic problem. For some reason, the second
          $q variable is populated regardless of the previous conditional
          statement being satisfied or not. If a state is not input, $search
          remains untouched and the 2nd $q is populated as expected. If a state
          is input, $search is modified but the 2nd $q is still populated.
          >
          The problem is how I am comparing $search with the array:

          foreach ($states as $key =$value)

          if ($search == $value || $search == $key)...

          This ends up populating $search with multiple strings from the array.
          The issue isn't with the logic but with my method of comparison to the
          array. What's the proper way to compare strings with values within an
          array? Would probably be a better question

          --

          Regards,

          Jeff Gardner
          _______________ ____________

          "Contrary to popular belief, Unix is user friendly. It just happens
          to be very selective about who its friends are." --Kyle Hearn

          Comment

          • Jeff Gardner

            #6
            Re: What's wrong the the logic in this code?

            Jerry Stuckle wrote:
            Jeff Gardner wrote:
            >Greetings:
            >>
            > I am attempting to get conditional output based on POSTed form
            >data. If the posted value is either the key or value of an array,
            >$x=key and $q=foo. elseif it is neither key nor value(I tried || too)
            >, $q=bar. Even though $x is properly assigned the new value, $q always
            >ends up being bar. Abstractions aside, here's the code: (note ##
            >comments added to this post)
            >>
            ><snip>
            >$search = $_POST[search];
            >$sort = $_POST[sortby];
            >$states = array(
            > "AL" ="Alabama",
            > "AK" ="Alaska",
            > "AZ" ="Arizona",
            > ...
            > );
            >##echo $search here outputs the POSTed input
            >foreach ($states as $key =$value)
            >>
            >if ($search == $value || $search == $key) {
            >$search = $key;
            >##echo $search here outputs the new value, when applicable
            >$q = "
            > SELECT
            > organization.*, contacts.*
            > FROM organization
            > LEFT JOIN contacts
            > ON organization.or g_id = contacts.org_id
            > OR organization.or g_id = 0
            > WHERE organization.st ate
            > = '$search'
            > OR contacts.state
            > = '$search'
            > ORDER BY '$sort' ASC";
            > } elseif ($search != $value && $search != $key) { ##I tried ||
            >here too.
            >##echo $search here outputs the new value too, when applicable
            >$q = "
            > SELECT
            > CONCAT(contacts .firstname,' ',contacts.last name) AS fullname,
            > organization.*, contacts.*
            > FROM organization
            > LEFT JOIN contacts
            > ON organization.or g_id = contacts.org_id
            > OR organization.or g_id = 0
            > WHERE organization.st ate
            > LIKE '%$search%'
            > OR organization.or gname
            > LIKE '%$search%'
            > OR contacts.state
            > LIKE '%$search%'
            > OR contacts.lastna me
            > LIKE '%$search%'
            > OR contacts.firstn ame
            > LIKE '%$search%'
            > ORDER BY '$sort' ASC";
            >}
            >>
            >$result = mysql_query($q)
            > or die('Query Failed: ' . mysql_error());
            ></snip>
            >>
            >Where is this evaluation breaking down and why? Thanks in advance.
            >
            You're problem is you are setting $q each time through the loop, then
            using it outside of the loop. $q will always have the last value set in
            the loop.
            >
            If $search matched the last state in your list, you would get the value
            from your "then" clause. Any other time you'll get the value from your
            "then" clause.
            >
            Here's what ended up working: (note the if(){foreach(){ if()}} at the
            beginning) Unfortunately, in_array() and array_key_exist s() are case
            sensitive. I'm trying to figure out a way to get strtoupper and
            ucfirst() to fix any input.

            <snip>
            $search = $_POST[search];
            $sort = $_POST[sortby];
            $states = array(
            "AL" ="Alabama",
            "AK" ="Alaska",
            "AZ" ="Arizona",
            ...
            );

            if (in_array($sear ch ,$states) || array_key_exist s($search, $states)) {
            foreach ($states as $key =$value){ if ($search == $value || $search
            == $key){
            $search = $key;
            }}
            $q = "
            SELECT
            organization.*, contacts.*
            FROM organization
            LEFT JOIN contacts
            ON organization.or g_id = contacts.org_id
            OR contacts.org_id = 0
            WHERE organization.st ate
            LIKE '$search%'
            OR contacts.state
            LIKE '%$search%'
            ORDER BY '$sort' ASC";

            } else {
            $q = "
            SELECT
            CONCAT(contacts .firstname,' ',contacts.last name) AS fullname,
            organization.*, contacts.*
            FROM organization
            LEFT JOIN contacts
            ON organization.or g_id = contacts.org_id
            OR contacts.org_id = 0
            WHERE organization.st ate
            LIKE '%$search%'
            OR organization.or gname
            LIKE '%$search%'
            OR contacts.state
            LIKE '%$search%'
            OR contacts.lastna me
            LIKE '%$search%'
            OR contacts.firstn ame
            LIKE '%$search%'
            ORDER BY '$sort' ASC";
            }
            </snip>



            --

            Regards,

            Jeff Gardner
            _______________ ____________

            "Contrary to popular belief, Unix is user friendly. It just happens
            to be very selective about who its friends are." --Kyle Hearn

            Comment

            • Jerry Stuckle

              #7
              Re: What's wrong the the logic in this code?

              Jeff Gardner wrote:
              Jerry Stuckle wrote:
              >
              >Jeff Gardner wrote:
              >>
              >>Greetings:
              >>>
              >> I am attempting to get conditional output based on POSTed form
              >>data. If the posted value is either the key or value of an array,
              >>$x=key and $q=foo. elseif it is neither key nor value(I tried ||
              >>too) , $q=bar. Even though $x is properly assigned the new value, $q
              >>always ends up being bar. Abstractions aside, here's the code:
              >>(note ## comments added to this post)
              >>>
              >><snip>
              >>$search = $_POST[search];
              >>$sort = $_POST[sortby];
              >>$states = array(
              >> "AL" ="Alabama",
              >> "AK" ="Alaska",
              >> "AZ" ="Arizona",
              >> ...
              >> );
              >>##echo $search here outputs the POSTed input
              >>foreach ($states as $key =$value)
              >>>
              >>if ($search == $value || $search == $key) {
              >>$search = $key;
              >>##echo $search here outputs the new value, when applicable
              >>$q = "
              >> SELECT
              >> organization.*, contacts.*
              >> FROM organization
              >> LEFT JOIN contacts
              >> ON organization.or g_id = contacts.org_id
              >> OR organization.or g_id = 0
              >> WHERE organization.st ate
              >> = '$search'
              >> OR contacts.state
              >> = '$search'
              >> ORDER BY '$sort' ASC";
              >> } elseif ($search != $value && $search != $key) { ##I tried ||
              >>here too.
              >>##echo $search here outputs the new value too, when applicable
              >>$q = "
              >> SELECT
              >> CONCAT(contacts .firstname,' ',contacts.last name) AS fullname,
              >> organization.*, contacts.*
              >> FROM organization
              >> LEFT JOIN contacts
              >> ON organization.or g_id = contacts.org_id
              >> OR organization.or g_id = 0
              >> WHERE organization.st ate
              >> LIKE '%$search%'
              >> OR organization.or gname
              >> LIKE '%$search%'
              >> OR contacts.state
              >> LIKE '%$search%'
              >> OR contacts.lastna me
              >> LIKE '%$search%'
              >> OR contacts.firstn ame
              >> LIKE '%$search%'
              >> ORDER BY '$sort' ASC";
              >>}
              >>>
              >>$result = mysql_query($q)
              >> or die('Query Failed: ' . mysql_error());
              >></snip>
              >>>
              >>Where is this evaluation breaking down and why? Thanks in advance.
              >>
              >>
              >You're problem is you are setting $q each time through the loop, then
              >using it outside of the loop. $q will always have the last value set
              >in the loop.
              >>
              >If $search matched the last state in your list, you would get the
              >value from your "then" clause. Any other time you'll get the value
              >from your "then" clause.
              >>
              Here's what ended up working: (note the if(){foreach(){ if()}} at the
              beginning) Unfortunately, in_array() and array_key_exist s() are case
              sensitive. I'm trying to figure out a way to get strtoupper and
              ucfirst() to fix any input.
              >
              <snip>
              $search = $_POST[search];
              $sort = $_POST[sortby];
              $states = array(
              "AL" ="Alabama",
              "AK" ="Alaska",
              "AZ" ="Arizona",
              ...
              );
              >
              if (in_array($sear ch ,$states) || array_key_exist s($search, $states)) {
              foreach ($states as $key =$value){ if ($search == $value ||
              $search == $key){
              $search = $key;
              }}
              $q = "
              SELECT
              organization.*, contacts.*
              FROM organization
              LEFT JOIN contacts
              ON organization.or g_id = contacts.org_id
              OR contacts.org_id = 0
              WHERE organization.st ate
              LIKE '$search%'
              OR contacts.state
              LIKE '%$search%'
              ORDER BY '$sort' ASC";
              >
              } else {
              $q = "
              SELECT
              CONCAT(contacts .firstname,' ',contacts.last name) AS fullname,
              organization.*, contacts.*
              FROM organization
              LEFT JOIN contacts
              ON organization.or g_id = contacts.org_id
              OR contacts.org_id = 0
              WHERE organization.st ate
              LIKE '%$search%'
              OR organization.or gname
              LIKE '%$search%'
              OR contacts.state
              LIKE '%$search%'
              OR contacts.lastna me
              LIKE '%$search%'
              OR contacts.firstn ame
              LIKE '%$search%'
              ORDER BY '$sort' ASC";
              }
              </snip>
              >
              >
              >
              Hmmm, not easy to make a case insensitive search. The keys are already
              upper case; maybe change the values to all lower case then if you find
              it use ucwords() to uppercase the first char of each word?

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

              Comment

              Working...