0

I must import file (example of this file in attachment) from .txt file to table in html (with 5 columns) to PHP document. I want to except the first column, whic added now to my table (Lp. and numbers from 1 ... xx) and the first line (it's the headers of column but not in my table - it's column in txt file and I don't want to import this).

Now my code is:

            <?php
            $file = fopen("test.txt","r");
            if(count >= 30);
            while(! feof($file))
            {
                echo "<tr>";
                $line = fgets($file);
                $cols = explode('|', trim($line, '|'));
                $deleted_item = array_pop($cols);                   
                foreach($cols as $col)
                    echo "<td>$col</td>";
                echo "</tr>";
            }

            fclose($file);
        ?>

I need read and import data for example from 10 to 50 line from .txt file.

Example of file on Pastebin: pastebin.com/4045z1us

Image with what I want to have :)

PICTURE WITH EXAMPLE

Thanks for help.

Paweł G.
  • 1
  • 2
  • 3
    This is not a site where people write code for you, this is a Q'n'A. If you have a specific proble, you should edit your question so it contains that specific problem (but do be sure to use search before that, as your question is very likely already answered). – M. Prokhorov Nov 21 '17 at 11:35
  • Search before asking a question. There are loads of resources online. A tutorial site would probably be more suitable. Check this question: https://stackoverflow.com/questions/4103287/read-a-plain-text-file-with-php. Read php documentation: http://php.net/manual/en/function.fopen.php – Hameed Nov 21 '17 at 11:51
  • Thanks :) I found the resolution (in 80% percent). I need to delete a Lp. column from file and with this code theres added the last, empty small column to my table. Anybody can help? Code: https://pastebin.com/6X3weRxN How it's see: https://imgur.com/a/DsamF – Paweł G. Nov 21 '17 at 12:59
  • @PawełG.- please don't host code externally, as overtime links can expire - please edit the question and add the code in there. – Stuart.Sklinar Nov 21 '17 at 13:25

1 Answers1

0

To remove last item from an array, you can use array_pop

Edited

$linecount = 0;     
while(! feof($file))
     {
        $linecount++;
        /* if line number is greater than 199 and the first 
        character is a + sign stop. The check for plus sign
        is to cover both 200 lines and 400 lines scenario
        */
        if (preg_match("/^\+/", $line) && $linecount > 199)
            break;

        //ignore until you read line 19
        if ($linecount < 19)
            continue;

        echo "<tr>";
        $line = fgets($file);
        $cols = explode('|', trim($line, '|'));

        // only print array items with index 1 to 5  
        foreach (range(1,5) as $i)
            echo "<td>$cols[$i]</td>";
        echo "</tr>";
    }

You need to pick up a good php book or go through some tutorials. As a matter of fact, I believe you should start with basics of control structures. With previous code, you should have been able to work this out easily.

Hameed
  • 2,227
  • 1
  • 21
  • 31
  • Working perfect! You can help to delete the first column and the first line from importing file? (I do not want to import this to my table). – Paweł G. Nov 21 '17 at 13:39
  • Set a counter, to keep track of number of lines. [array_shift](http://php.net/manual/en/function.array-shift.php) removes the first item. I updated the answer. – Hameed Nov 21 '17 at 13:45
  • The code in the answer, skips line 1 of your text file, then removes the first and last element before adding it to the table. With that answer you should be able to work the rest out. – Hameed Nov 21 '17 at 14:00
  • thank you. I modify code but without effect which I want -> https://pastebin.com/1NbZZNvU – Paweł G. Nov 21 '17 at 21:34
  • Maybe the text file is not formatted the way you think it is. Can you paste the first 3 -4 lines of your text file. – Hameed Nov 21 '17 at 22:30
  • Theres it https://pastebin.com/4045z1us - I want to cut the first 17 lines from it (it's always the same) and the last 20 lines (because file can have 200 lines or 400 so I can't have code with for example cut the lines after e.g. >= 200 line. – Paweł G. Nov 21 '17 at 22:37
  • Updated the answer. – Hameed Nov 21 '17 at 23:20
  • With this code I have still first line with text headers and the empty lines with frame in the end of table. – Paweł G. Nov 22 '17 at 06:11
  • I have provided more than a guideline. I trust you could use simple arithmetic to work out line numbers. Good luck. – Hameed Nov 22 '17 at 06:18