Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

AK Pro Tutorial Database Tutorials Live ITexperts Central 

Change Language

Who's online

There are currently 0 users and 12 guests online.

Lesson 17 - Reading External Files into PHP - Part 2

Now lets put what we learned about the explode() function and arrays into practice parsing (dealing with) files. Lets make a file called "file.txt" and fill it with at least 5 lines of non-sense. Open up SciTE or Notepad and type the following:

This is line 1
This is line 2
This is line ...uh... (what is after 2?)
anyways, this is the next line
And this is the last line!

Save the file and up load it to your web server.

Make an new make a new file called "filetoarray.php". After you save it type the following into it:

<?php
// Store the file name in a string.
$filename "file.txt";

// Read file into array
$contents file($filename);

echo 
"there are <b>"count($contents). "</b> lines in this file. Here is what he array looks like now:<br>"

print_r($contents);

echo 
"<hr><b>Below are the lines in the file after they were \"shuffled\":</b><br>";
shuffle($contents);

// Loop through the array and print each line to the screen.
foreach ($contents as $line)
{
   echo 
$line"<br>";
}

echo 
"<hr>Now the SHUFFLED array looks like this:<br>";
print_r($contents);
?>

Then save it and upload it to the same directory as "file.txt".

After you run the file you should see two parts to the script. First is the Number of lines that are in the file and a print-out of what array element each line is in. Second you should see the lines you typed in file.txt showing up in a random order every time you refresh the page, with the print-out of what array element they are below them. So lets go over them!

<?php
// Store the file name in a string.
$filename "file.txt";
?>

In this first part of the code we store the name of a file in a variable.

<?php
// Read file into array
$contents file($filename);
?>

Next we use the "file" function to read every line out of "file.txt" into and array called "$contents". Every line will be a different element it the array. For example, line one will be the first element in the array:
<?php
Array ([0] => This is line 1);
//Remember arrays start at "0" and go up for each element.
?>

Line two will be the second element in the array
<?php
Array ([1] => This is line 2);
?>

And so on....
Each new line will be put into a new element of the array. Since my file.txt has five lines there will be five elements in the array. (0,1,2,3,4 because arrays start at 0)

Next we will use the count() function to count how many elements are in the array:

<?php
echo "there are <b>"count($contents). "</b> lines in this file. Here is what he array looks like now:<br>"
?>

(Note: Remember, there are the same number of lines in the file as there are elements in the array so counting the elements in the array will tell us the number of lines in the file. Confused yet?)

<?php
echo "<hr><b>Below are the lines in the file after they were \"shuffled\":</b><br>";
shuffle($contents);
?>

In the above code we then use the shuffle() function to randomly shuffle the elements in the array. So element "0" might end up as element "3" and so on. End the end we will still have five elements in the array... they'll just be in different places!

Last, we use a foreach loop to go through each element in the array and print out the line:

<?php
// Loop through the array and print each line to the screen.
foreach ($contents as $line)
{
   echo 
$line"<br>";
}

echo 
"<hr>Now the SHUFFLED array looks like this:<br>";
print_r($contents);
?>

We end by using the print_r function to show you what the array looks like after it has been shuffled.

##################################################################

Now lets make something useful out of this....I know! how about a random quote?
(Like the one you see at the bottom of this page!)
Change the code in "filetoarray.php" to this:

<?php
// Store the file name in a string.
$filename "file.txt";

// Read file into array
$contents file($filename);

// Shuffle the elements to make the array random
shuffle($contents);

// Print out the first element.
echo $contents[0];
?>

This is pretty simple to follow so I am just going to review some of it. After we have read each line into an array called "$contents" we just shuffle the arrays elements to make them random and print out the first element. (Which would be one of the lines in the file.)

#######################################################

You could also use something like this to choose a random banner or affiliate! So that is your home work - I want you to try to figure out how you could make a random image appear. I'll give you a major hint, 8) change "file.txt" to contain a new HTML image tag () for each line!


Submitted by David on November 10, 2006 - 7:58pm.
printer friendly version

Wicked tutorial

Wow I can't believe how easy it is to create a randomation array, I know this will be usefull for manythings on my website. and cheers for the heads up for creating random images :]

regards,

Vinnie


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati