Forum Moderators: coopster

Message Too Old, No Replies

Looping through text list

         

justusthane

5:46 am on Jun 4, 2008 (gmt 0)

10+ Year Member



Hello
What I'm trying to accomplish is this:
I would like to write a PHP program that will move through a text list, one line at a time, and for each line, enter that line into a mySQL database table as a new record. I know some PHP, and less mySQL. Just looking for some input on what the best way to do this would be.
Thanks!

PHP_Chimp

6:38 am on Jun 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is your text list in an external file, or are you going to write it into the code?
If your code is in a seperate file then you should have a look at fgets [uk2.php.net] as that has a good example of reading a line from a file.

Have you got any code started? As if not then you should have a look at mysql_query [uk2.php.net]or mysqli_query [uk2.php.net]. As these give good examples of what to do.
The difference in the 2 is minor. Try to use the mysqli versions if you have php5 as they are improved.

Welcome along.

justusthane

8:16 am on Jun 4, 2008 (gmt 0)

10+ Year Member



Thanks for your help. I'll look into your suggestions and post again if I need more support :)

dreamcatcher

4:43 pm on Jun 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could read a text file using file [uk.php.net], which reads a text file into an array. Then loop through the array and insert your data into your db.

dc

justusthane

4:18 pm on Jun 6, 2008 (gmt 0)

10+ Year Member



Dreamcather, I took your advice, and it's working so far. Haven't tried to put it into a database yet.

<?php
$membernumbers = file('mem_nums.txt', FILE_SKIP_EMPTY_LINES);

// Loop through our array, and display line numbers too.
foreach ($membernumbers as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . $line . "<br />\n";
}
?>

That's the code I'm using, but I don't understand it. How exactly does that loop work? What does

$membernumbers as $line_num
mean? Where does it tell it which line of the array to print?
Thanks for your patience

dreamcatcher

6:30 pm on Jun 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP`s foreach loop is a special function for reading arrays. The file function reads the data into an array. You can see this by doing:

print_r($membernumbers);

An array has key, value pairs.

If I did the foreach loop like this, you might understand it better:

foreach ($membernumbers AS $key => $value)

Its simply reading each slot into the variables $key & $value with each iteration. The good thing about a foreach loop is you can have random keys and it will still work. A for loop on the other hand would be checking for keys in numerical order and would therefore throw an error if a key was missing. Hope that makes sense.

The foreach loop will keep running until it hits the last slot and then it terminates.

Check out the PHP site for more info:
[uk2.php.net...]

dc