Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

For each txt file in directory... do this...

Been stuck on this for over 2 hours - pls assist:

End goal: To turn contents of every txt file in my directory into a table.

So I have .txt files with contents like:

Giraffe: 1
Banana: 2
Monkey: 3

From each txt file I want it take out the "1" "2" and "3" and put it into columns (so I can later just paste it into excel and its already splitted).

I was able to achieve this for ONE file at a time using the script below:

PHP Code

<?php 
<body>


<?
php
$lines 
= file('alskdgj.txt'); //here's the filename

?>
<table width=800>
<?php
foreach ($lines as $line) {
list(
$a,$b) = explode(':', $line);
print 
"<td width=40>$b</td>";
}
?>
</table>

</body> 

That is for alskdgj.txt, works perfect. But is it possible to do a forreach argument in the script so that it scans the directory and does this function for EVERY txt file in that directory ?

So that if I have 5 files, it would eventually give me a table with the values from the 5 txt files - rather doing each one by one.

Plz help. 🤝
You can use something like this to iterate:
PHP Code

<?php 
foreach(glob("*.txt") as $filename) {
    
$lines = file($filename);
    
// do splitting here
} 

hi
thx will try now


(Jan 6, 2018 12:25 PM)Anxuein Wrote:  You can use something like this to iterate:
PHP Code

<?php 
foreach(glob("*.txt") as $filename) {
 
   $lines = file($filename);
 
   // do splitting here
} 
Here is my code:

PHP Code

<?php 
<body>



<
table width=800>
<?
php
foreach(glob("folda/*.txt") as $filename) {
 
   $lines = file($filename);
 
       foreach ($lines as $line) {
        list(
$a,$b) = explode(':', $line);
        print 
"<td width=40>$b</td>";
}
} 


?>
</table>

</body> 

It does good for the first txt in the directory, but displays error:





Quote:Notice: Undefined offset: 1 in C:\xampp\htdocs\tabled\444.php on line 17

Notice: Undefined offset: 1 in C:\xampp\htdocs\tabled\444.php on line 17

Notice: Undefined offset: 1 in C:\xampp\htdocs\tabled\444.php on line 17

Notice: Undefined offset: 1 in C:\xampp\htdocs\tabled\444.php on line 17

Notice: Undefined offset: 1 in C:\xampp\htdocs\tabled\444.php on line 17

Notice: Undefined offset: 1 in C:\xampp\htdocs\tabled\444.php on line 17

Notice: Undefined offset: 1 in C:\xampp\htdocs\tabled\444.php on line 17

Notice: Undefined offset: 1 in C:\xampp\htdocs\tabled\444.php on line 17

1 2 3

Can you provide the contents of the first file in directory and second?

hi
(Jan 6, 2018 12:52 PM)Anxuein Wrote:  Can you provide the contents of the first file in directory and second?

wierd. restarted browser now it shows all 3. but still has the errors on top.

Contents of files:
first one:

Quote:Giraffe: 1
Banana: 2
Monkey: 3


second one:
Quote:Giraffe: 12
Banana: 21
Monkey: 53


third one
Quote:Giraffe: 12
Banana: 24
Monkey: 223




And this is what I'm seeing in the browser:
Quote:
Notice: Undefined offset: 1 in C:\xampp\htdocs\tabled\444.php on line 17

Notice: Undefined offset: 1 in C:\xampp\htdocs\tabled\444.php on line 17

Notice: Undefined offset: 1 in C:\xampp\htdocs\tabled\444.php on line 17

1   2   3                 12   21   53                 12   24   223

Try this my dude:
PHP Code

<?php 
<body>



<
table width="800">
<?
php
foreach(glob("folda/*.txt") as $filename) {
    
$lines = file($filename);
    foreach (
$lines as $line) {
        
$split = explode(': ', $line);
        if (isset(
$split[1])) {
            echo 
"<td width=\"80\">$split[1]</td>";
        }
    }
}


?>
</table>

</body> 

hi
(Jan 6, 2018 01:21 PM)Anxuein Wrote:  Try this my dude:
PHP Code

<?php 
<body>



<
table width="800">
<?
php
foreach(glob("folda/*.txt") as $filename) {
 
   $lines = file($filename);
 
   foreach ($lines as $line) {
 
       $split = explode(': ', $line);
 
       if (isset($split[1])) {
 
           echo "<td width=\"80\">$split[1]</td>";
 
       }
 
   }
}


?>
</table>

</body> 

Almost perfect bro!
It's returning me a table of 9 columns. No errors tho.
Is it possible to make it 3 x 3 rather?
That is; so that every txt file has its own row?
PHP Code

<?php 
<body>



<
table width="800">
<?
php
foreach(glob("folda/*.txt") as $filename) {
    
$values = [];
    
$lines = file($filename);
    foreach (
$lines as $line) {
        
$split = explode(': ', $line);
        if (isset(
$split[1])) {
            
$values[] = $split[1];
        }
    }

    
// just to make sure we don't try to output an empty row
    
if (!empty($values)) {
        echo 
"<tr>";
        foreach (
$values as $v) {
            echo 
"<td width=\"80\">$v</td>";
        }
        echo 
"</tr>";
    }
}


?>
</table>

</body> 

hi
(Jan 6, 2018 01:36 PM)Anxuein Wrote:  
PHP Code

<?php 
<body>



<
table width="800">
<?
php
foreach(glob("folda/*.txt") as $filename) {
 
   $values = [];
 
   $lines = file($filename);
 
   foreach ($lines as $line) {
 
       $split = explode(': ', $line);
 
       if (isset($split[1])) {
 
           $values[] = $split[1];
 
       }
 
   }

 
   // just to make sure we don't try to output an empty row
 
   if (!empty($values)) {
 
       echo "<tr>";
 
       foreach ($values as $v) {
 
           echo "<td width=\"80\">$v</td>";
 
       }
 
       echo "</tr>";
 
   }
}


?>
</table>

</body> 

🤗 YOU THE BEST!!!


@Anxuein

I'm trying to PM you but its not letting me.
You really know what you're doing in php (unlike me). If you have time, would you be interested in doing a php project for btc?
Its really short and simple - and this script was probably 40% of it.

If ya, pls pm me.
OP, if you want to import data stored in a text file into Excel, it'd probably be easier to just parse your data and save it as CSV format (columns separated by commas) and then load it normally into Excel.
I ♥ Andy Quez


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by BabadookFeb 1, 2019 10:50 PM 11 373 Feb 1, 2019 10:50 PM
Last Post: Babadook
[PHP] by Viper228Aug 26, 2018 02:28 PM 0 124 Aug 26, 2018 02:28 PM
Last Post: Viper228
[PHP] by Dale CarnegieMay 15, 2018 11:53 PM 5 156 May 15, 2018 11:53 PM
Last Post: Squito