read | delimited fields from record in variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hockeyman10
    New Member
    • Mar 2007
    • 7

    read | delimited fields from record in variables

    I am loading from a .data file and each line has all of the variables divided by "|". The code I use is in Perl and I need to have access to these variables just the same as with my perl code but with PHP. Please let me know if you have any idea what I can do. Thanks in advance. Here is the code.

    $data_file="ads .data";

    open(DAT, $data_file) || die("Could not open file!");
    @raw_data=<DAT> ;
    close(DAT);



    foreach $ads (@raw_data)
    {
    chop($ads);
    ($ad_number,$us er_name,$user_t ype,$date,$var_ 1,$var_2,$var_3 ,$var_4,$var_5, $var_6,$var_7,$ name,$var_8,$ci ty,$province,$p ostal_code,$cou ntry,$phone_num ber,$var_9,$ema il,$var_10,$cat egory,$subcateg ory,$caption_he ader,$caption,$ ad_text,$price, $var_11,$var_12 ,$var_13,$var_1 4,$var_15,$var_ 16,$private,$va r_17)=split(/\|/,$ads);



    if ($city eq "Toronto") {
    print "<table width=100%><tr> <td>
    $city

    </td></tr></table>";
    }
    }
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Welcome to TSDN. Next time enclose your code within php or code tags, as stated in the Posting Guidelines at the top of this forum.

    Use the explode function of PHP. See http://nl3.php.net/manual/en/function.explode.php

    Sample usage for your script it would be something like:

    [php]
    // read your record in $data
    list($ad_number ,$user_name,$us er_type,$date,$ var_1,$var_2,
    $var_3,$var_4,$ var_5,$var_6,$v ar_7,$name,$var _8,$city,
    $province,$post al_code,$countr y,$phone_num ber,$var_9,
    $email,$var_10, $category,$subc ategory,$captio n_header,
    $caption,$ad_te xt,$price,$var_ 11,$var_12,$var _13,$var_14,
    $var_15,$var_16 ,$private,$va r_17)= explode("|", $data);[/php]

    Ronald :cool:

    Comment

    • hockeyman10
      New Member
      • Mar 2007
      • 7

      #3
      Thank you for the reply!

      So my code should look like this?

      <?php

      $data_file="dat a.txt";



      list($ad_number ,$user_name,$us er_type,$date,$ var_1 ,$var_2,
      $var_3,$var_4,$ var_5,$var_6,$v ar_7,$name,$var _8,$c ity,
      $province,$post al_code,$countr y,$phone_num ber,$var_9,
      $email,$var_10, $category,$subc ategory,$captio n_hea der,
      $caption,$ad_te xt,$price,$var_ 11,$var_12,$var _13,$ var_14,
      $var_15,$var_16 ,$private,$va r_17)= explode("|", $data);








      if ($city == "Toronto") {
      print "<table width=100%><tr> <td>$city</td></tr></table>";
      }


      ?>


      This makes the page not work. Is there something I'm doing wrong? Thanks alot for the help!

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Where is your read (of the file) statement? As I pointed out in the sample:

        [php]// read your record in $data
        [/php]

        Ronald :cool:

        Comment

        • hockeyman10
          New Member
          • Mar 2007
          • 7

          #5
          Would it be something like this?
          [php]
          <?php
          $data_file="dat a.txt";
          $myFile = "data.txt";
          $fh = fopen($myFile, 'r');
          $data = fread($fh, 5);

          list($ad_number ,$user_name,$us er_type,$date,$ var_1 ,$var_2,
          $var_3,$var_4,$ var_5,$var_6,$v ar_7,$name,$var _8,$c ity,
          $province,$post al_code,$countr y,$phone_num ber,$var_9,
          $email,$var_10, $category,$subc ategory,$captio n_hea der,
          $caption,$ad_te xt,$price,$var_ 11,$var_12,$var _13,$ var_14,
          $var_15,$var_16 ,$private,$va r_17)= explode("|", $data);

          if ($city == "Toronto") {
          print "<table width=100%><tr> <td>$city</td></tr></table>";
          }
          fclose($fh);
          ?> [/php]
          Not to sure what you mean. New to this. Thanks for your help!

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            You were requested in a previous post to enclose your code within php or code tags! Read the Posting Guidelines. before you continue.

            moderator

            Comment

            • hockeyman10
              New Member
              • Mar 2007
              • 7

              #7
              Originally posted by ronverdonk
              You were requested in a previous post to enclose your code within php or code tags! Read the Posting Guidelines. before you continue.

              moderator

              I'm sorry. I am so new! I thought you meant within the <?php ?> tags. Then after my last post I realized what you meant but I could not find an edit feature! I'm sorry.

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                php command fgets will read one text line at a time. So the process is:
                Code:
                open file
                while (not EOF)
                   read text line
                   assign content to variables
                   .. what else to process ...
                end while
                close file
                Ronald :cool:

                Comment

                • hockeyman10
                  New Member
                  • Mar 2007
                  • 7

                  #9
                  So it should look something like this?? This deffiantely isn't it though.

                  [PHP]
                  <?php

                  $data_file="dat a.txt";



                  open file
                  while (not EOF)





                  list($ad_number ,$user_name,$us er_type,$date,$ var_1 ,$var_2,
                  $var_3,$var_4,$ var_5,$var_6,$v ar_7,$name,$var _8,$c ity,
                  $province,$post al_code,$countr y,$phone_num ber,$var_9,
                  $email,$var_10, $category,$subc ategory,$captio n_hea der,
                  $caption,$ad_te xt,$price,$var_ 11,$var_12,$var _13,$ var_14,
                  $var_15,$var_16 ,$private,$va r_17)= explode("|", $data);








                  if ($city == "Toronto") {
                  print "<table width=100%><tr> <td>$city</td></tr></table>";
                  }


                  end while
                  close file




                  ?>
                  [/PHP]

                  Comment

                  • hockeyman10
                    New Member
                    • Mar 2007
                    • 7

                    #10
                    I am very confused. Is there something I am supposed to be adding to that?

                    Comment

                    Working...