Back

Example 3: Add a Bowler - Appends the text file and sorts on index 0 when read

 

Display List of Bowlers



This is an example of using fopen(), with the "a" argument to append new comma-separated entries to a text file, fwrite() to write the entries to it, and fclose() to close the file when finised. It also uses the file() function to read the contents of the file, and a for loop that iterates through each line of the file, and the explode() function to parse each line on the comma, and display each row and field in an html table. This is great for complex data structures that require advanced data manipulation such as sorting records in an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Working with Text Files Example 1</title>
<link rel="stylesheet" href="../php_styles.css" type="text/css" />
</head>

<body>
<div align="center">
<!-- Form 1: Calls (a) addscript.php -->
<h3>Add a Bowler</h3>
<form action="addscript.php" method="get">
<label>First Name </label><input type="text" name="fname"  />&nbsp;<label>Last Name</label><input type="text" name="lname" />
<input type="submit" name="submit" value="Add" />
</form>

<!-- Form 2: Calls (b) displayscript.php -->
<h3>Display List of Bowlers</h3>
<form action="displayscript.php" method="get">
<input type="submit" name="submit" value="Display List" />
</form>

</div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Working with Text Files Example 1</title>
</head>

<body>
<?php
if (empty($_GET['lname']) || empty($_GET['fname'])) {
    echo 
'<div align=center>Please enter your first and last name. Click <a href="javascript: history.go(-1)">here</a> to return to the form.</div>';
} else {
    
$lname ucfirst(strtolower($_GET['lname']));
    
$fname ucfirst(strtolower($_GET['fname']));
    
$file "bowlers.txt";
    
$bowlers fopen($file"a");
    
$bowler $lname ", " $fname "\n";
    
fwrite($bowlers$bowler);
    
fclose($bowlers);
    echo 
'<div align=center><p>' $bowler ' has been added.</p>';
    echo 
'<p>Click <a href="http://php.skillbuilderblocks.com/tutorials/unit05/ex03/form.php" target="unit05content">here</a> to return to the form.</div></p>';
}
?>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Working with Text Files Example 1</title>
</head>

<body>
<?php
if (!file_exists("bowlers.txt")) {
    echo 
"<div align=center>The file requested does not exist. Please add bowlers and try again.<br />";
    echo 
"Click <a href='javascript: history.go(-1)'>here</a> to return to the form.</div>";
} else {
    
$bowlers file("bowlers.txt");
    
sort($bowlers);
    echo 
"<table border=1 width=35% align=center><tr><th>First Name</th><th>Last Name</th></tr>";    
    for (
$i 0$i count($bowlers); ++$i) {
        
$bowler explode(","$bowlers[$i]);
        echo 
"<tr><td>" $bowler[1] . "</td><td>" $bowler[0] . "</td></tr>";
    } 
    echo 
"</table>";
    echo 
"<div align=center>Click <a href=\"http://php.skillbuilderblocks.com/tutorials/unit05/ex03/form.php\" target=\"unit05content\">here</a> to return to the form.</div>";

}
?>
</body>
</html>
Changa, Chimi
Twain, Mark
Sqhhiyg, Sqhhiyg
Sqhhiyg, Sqhhiyg
Iqelcuwacth, Iqelcuwacth
Iqelcuwacth, Iqelcuwacth
Green, Ed
Nfblsgpzvbvcowjv, Dzfvlkhdhvwjdc
Rmnsufo, Rmnsufo
Rmnsufo, Rmnsufo
Birdvzwv, Birdvzwv
Gqtidqin, Gqtidqin
Anirksqd, Anirksqd
Prdveo, Prdveo
Prdveo, Prdveo
Gviupcbd, Gviupcbd
Cqdbvtqr, Cqdbvtqr
Kbkhzh, Kbkhzh
Kbkhzh, Kbkhzh
Laqsrwcncf, Laqsrwcncf
Laqsrwcncf, Laqsrwcncf
Wzqxenwiml, Wzqxenwiml
Wzqxenwiml, Wzqxenwiml
Hqtxqoap, Hqtxqoap
Hqtxqoap, Hqtxqoap
Aaaa, Aaaaaa
Bradley, Bradley
Bradley, Bradley
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Mark, Mark
Ass, Jas
Bnuqkikwgea, Bnuqkikwgea
Bnuqkikwgea, Bnuqkikwgea
Ryuyuwz, Ryuyuwz
Ryuyuwz, Ryuyuwz
Runaas, Scott
Jwuovww, Jwuovww
Jwuovww, Jwuovww
Prncpaisac, Prncpaisac
Prncpaisac, Prncpaisac
Cxkkgwb, Cxkkgwb
Cxkkgwb, Cxkkgwb
Mdfisapsv, Mdfisapsv
Mdfisapsv, Mdfisapsv
Olnxruqtgv, Olnxruqtgv
Olnxruqtgv, Olnxruqtgv
Habhnuj, Habhnuj
Habhnuj, Habhnuj
Xdkpphteoen, Xdkpphteoen
Xdkpphteoen, Xdkpphteoen