Announcement

Collapse
No announcement yet.

I need to read a txt file and show content in a search select box field

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • I need to read a txt file and show content in a search select box field

    Hi all,

    I have a grid with a search area to filter data. The first field of this search area come from the lines of text field.
    I got a txt file like the following where are listed the acceptable values for a specific database field (lenght is fixed to 30 chars). More or less 10 rows.
    DEPT01
    DEPT03
    DEPT10
    DEPT11
    DEPT20
    DEPT22
    DEPT30

    The database field name against where to select those value is i.e. WARDNAME.
    Within the select search field I will insert something like that

    SELECT WARDNAME FROM TABLENAME
    WHERE {WARDNAME} = list of values on the txt file

    Could you please suggest how reach this result ??
    The reading of txt file has to do every time or it's sufficient once at the starting of application (OnScriptInit ?? )


    Googling I founded this that print the list... in some way:
    $file_handle = fopen("../depts/active_depts.txt", "r") or die("Can't open file-----");

    while (!feof($file_handle) ) {

    $line_of_text = fgets($file_handle);
    $line = rtrim($line_of_text);
    print "'".$line."',";
    }

    fclose($file_handle);

    I think to assign the output 'xxxxx','yyyy',......'zzzz' to a variable [glo_active_depts] and then use it into the select like
    SELECT WARDNAME FROM TABLENAME
    WHERE {WARDNAME} = [glo_active_depts]

    is it correct in your opinion ?? Are there any contreindications ?
    Last edited by giovannino; 03-25-2014, 12:48 PM.

  • #2
    Hi,
    try this one.

    $file = file_get_contents('path/to/your/file.txt);
    [glo_list] = "(".str_replace('\n',',',$file).")";

    SELECT ... WHERE {wardname} IN [glo_list];

    jsb

    Comment


    • #3
      Hi Jsb,

      I did a test using your code:

      [glo_list]='';
      $file = file_get_contents('../lista_reparti/reparti_attivati1.txt');
      [glo_list] = '('.str_replace("\n",',',$file).')';
      echo [glo_list];

      it works but I do not understand why it add a blank space after each line content.
      I also used trim($file) but it still add a blank space (???)
      Then, when I use it on the WHERE it gave me an error.
      Attached Files

      Comment


      • #4
        OK , solved .
        It was to substitute only with "\r\n"

        Comment

        Working...
        X