implode an array for query condition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Karl Engstrom
    New Member
    • Jan 2007
    • 8

    implode an array for query condition

    Hi All,

    I'm newish to php & have an issue on a project I'm working on...

    I've an array containing zip codes which displays if I echo it like
    Code:
    echo "$key";
    it returns 926299262492693 926779267592673 926079267492672

    I need to implode/explode with commas so it passes like 92629,92624,926 93,92677,92675, 92673,92607,926 74,92672 so I can use it as a WHERE IN condition.

    What's the best way to do this?

    Thanks
    Karl
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Originally posted by Karl Engstrom
    Hi All,

    I'm newish to php & have an issue on a project I'm working on...

    I've an array containing zip codes which displays if I echo it like
    Code:
    echo "$key";
    it returns 926299262492693 926779267592673 926079267492672

    I need to implode/explode with commas so it passes like 92629,92624,926 93,92677,92675, 92673,92607,926 74,92672 so I can use it as a WHERE IN condition.

    What's the best way to do this?

    Thanks
    Karl
    $key is not an array! The value you show via the "echo $key;" here is a normal string value. So you cannot plode it. Unless you have made a mistake

    Ronald :cool:

    Comment

    • Karl Engstrom
      New Member
      • Jan 2007
      • 8

      #3
      Kinda new/part-time with php, learning every time I sit down with it, please excuse my ignorance.
      OK, I guess I'm on the wrong path. I'll detail things a little...
      I'm using a class for querying a single table zip code db. I'm adding a table of companies and making a company locator based on range from originating zip code.
      I have
      Code:
      $z = new zipcode_class;
      $zips = $z->get_zips_in_range($_POST["zip1"], $_POST["radius"], _ZIPS_SORT_BY_DISTANCE_ASC, true);
      if ($zips === false) echo 'Error: '.$z->last_error;
      else {
         
         foreach ($zips as $key => $value) {
           $query_rsTech = "SELECT * FROM tech_info WHERE tech_zip_code = '$key,'";
           $rsTech = mysql_query($query_rsTech) or die(mysql_error());
           $row_rsTech = mysql_fetch_assoc($rsTech);
         }
      }
      Should I replace the foreach to $zips as $array then check with an is_array?

      Thanks, learning as I go...
      Karl

      Comment

      • Karl Engstrom
        New Member
        • Jan 2007
        • 8

        #4
        Can I make the string ($key) an array to use in a query, maybe?

        Karl

        Comment

        • vssp
          Contributor
          • Jul 2006
          • 268

          #5
          Count the total number and split the string 5 and the store the valus on single array I hope thsi idea is not bad.

          Comment

          • Karl Engstrom
            New Member
            • Jan 2007
            • 8

            #6
            I'm gonna try that... After iterating through the first array with
            Code:
            foreach ($zips as $key => value)
            I need to split a string that looks like
            926299262492693 926779267592673 926079267492672
            into one that looks like
            92629,92624,926 93,92677,92675, 92673,92607,926 74,92672
            so it can be used in a WHERE clause.

            I've been reading & trying diffrent things, no success yet.
            Any suggestions?

            Comment

            • Karl Engstrom
              New Member
              • Jan 2007
              • 8

              #7
              Can we bounce this one? I re-started it under a more descriptive title.

              Thanks,
              Karl

              Comment

              Working...