Multiple query variables based on str_word_count

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

    Multiple query variables based on str_word_count

    Greetings:

    I am attempting to split an input string using the str_word_count
    function to return multiple single word query variables.
    I can create an array based on
    $search=(str_wo rd_count($searc h, 1, '0123456789')); (I want words and/or
    numbers).
    I can extract named variables from the resulting array via
    $search=extract ($search, EXTR_PREFIX_ALL , search);
    and then call each variable by
    $search_0 $search_1 $search_2, etc.
    Several issues that I am having:
    Since the size of the array is determined by user input. I can't
    hardcode $search_x, $search_y, etc. because I don't know what x, y, etc.
    will be. How can I produce $search_x... et al. and then hand it/them to
    a query string? I can echo desired output using for or while loops,
    (i.e. the *value* of $search_x...et al. but I can't figure out how to
    get them into query strings. I want my query strings to contain
    something like

    $q="
    SELECT *
    FROM *
    WHERE table1.row1
    LIKE '%$search_x%'
    OR LIKE '%$search_y%'
    etc..."

    Advice or a sanity check is appreciated.

    --

    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
  • Kimmo Laine

    #2
    Re: Multiple query variables based on str_word_count

    "Jeff Gardner" <null@spamvoid. tldwrote in message
    news:_ZRRg.5286 $e66.3828@newss vr13.news.prodi gy.com...
    Greetings:
    >
    I am attempting to split an input string using the str_word_count function
    to return multiple single word query variables.
    I can create an array based on
    $search=(str_wo rd_count($searc h, 1, '0123456789')); (I want words and/or
    numbers).
    Stop right here and go no further with the extract thingy. Here you already
    have a nice beautiful array, arrays are the coolest, aren't they!? Why go
    thru all the trouble with exteracting and such...

    You can build up the query conditions with a little implode trick:

    $query_conditio ns =
    " my_field LIKE '%" . implode( "%' OR my_field LIKE '%", $search) . "%'";

    now insert $query_conditio n in your query string:
    $q="
    SELECT *
    FROM *
    WHERE table1.row1
    LIKE '%$search_x%'
    OR LIKE '%$search_y%'
    etc..."

    HTH

    --
    "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
    http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
    spam@outolempi. net || Gedoon-S @ IRCnet || rot13(xvzzb@bhg byrzcv.arg)


    Comment

    • Jeff Gardner

      #3
      Re: Multiple query variables based on str_word_count

      Kimmo Laine wrote:
      "Jeff Gardner" <null@spamvoid. tldwrote in message
      news:_ZRRg.5286 $e66.3828@newss vr13.news.prodi gy.com...
      >Greetings:
      >>
      >I am attempting to split an input string using the str_word_count function
      >to return multiple single word query variables.
      >I can create an array based on
      >$search=(str_w ord_count($sear ch, 1, '0123456789')); (I want words and/or
      >numbers).
      >
      Stop right here and go no further with the extract thingy. Here you already
      have a nice beautiful array, arrays are the coolest, aren't they!? Why go
      thru all the trouble with exteracting and such...
      >
      You can build up the query conditions with a little implode trick:
      >
      $query_conditio ns =
      " my_field LIKE '%" . implode( "%' OR my_field LIKE '%", $search) . "%'";
      >
      now insert $query_conditio n in your query string:
      >
      >$q="
      >SELECT *
      >FROM *
      >WHERE table1.row1
      >LIKE '%$search_x%'
      >OR LIKE '%$search_y%'
      >etc..."
      >
      >
      HTH
      >
      This is what ended up working:

      $table=array(
      'table.column',
      'table.column2' ,
      etc...
      );

      $str = '';
      foreach ($search as $s) {
      foreach($table as $t){
      if ($str != "")
      $str .= "OR\n";
      $str.= "$t\n" ;
      $str .= "LIKE '%$s%'\n";
      }}
      $q = "
      SELECT
      $alias
      FROM table1
      LEFT JOIN table2
      ON table1.id = table2.id
      WHERE
      $str
      ";

      --

      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

      Working...