School Map:  Home / Enroll $ Student Records; Class  
$ News @ LIVE Tech Radio * Support/FAQ | Store | FORUMS

Class / Forums / php101 Refresh the current page Week #10 Basic To-Do Assignment, post here
 

 (#18 2003-08-07 16:56:11) Post Reply

TDavid
php mySQL Perl C/C++
Principal


Enrolled: Mar 2000
Posts: 193
AP: 1

Week #10 Basic TO-DO ASSIGNMENT: Write a script to add sorted email addresses and names to a file and with the same script how to retrieve all the information and print to browser the name and email address in a table to the browser.

Post this assignment here so that we can offer comment/critique for you, but also post a URL to the working copy of the script and the complete source code of the script in your post.

scriptname.php <-- script name

Good luck to you!

 

 (#266 2003-09-22 07:51:41) Post Reply

Shrek
Need To Set


Enrolled: Aug 2003
Posts: 33
AP: 1

My Week 10 Basic Assignment
http://members.lycos.co.uk/jef...wan/class10.php

The delete Switch is just for further purpose. No use now.



<?
$error
= 'Sorry, I could not open this file! Check that the permissions are set properly for this file and that you used the correct absolute path to the file.';
$file_contents = file('filemail');
sort($file_contents);
$sizeof = count($file_contents);

$action=$_REQUEST['action'];
$todelete=$_REQUEST['todelete'];
$user=$_REQUEST['user'];
$email=$_REQUEST['email'];

switch (
$action)
{
case
'add':
echo (
"Enter add Mode<br>");
if(
$filehandle = fopen("filemail", "a"))
{
fputs($filehandle, "$user:$email\n");
fclose($filehandle);
print(
"Successfully added <b>$user</b> to the file<br>");
} else {
echo(
$error);
}
echo (
"Click <a href=".$_SERVER['PHP_SELF'].">Here</a> to Refresh!<br>");
exit;
break;
case
'delete':
echo (
"Enter Delete Mode<br>");
if(
$filehandle = fopen("filemail", "w"))
{
for(
$i=0; $i < $sizeof; $i++)
{
if(
$file_contents[$i] != $email)
{
fputs($filehandle, "$file_contents[$i]");
} else
{
$flag = 1;
}
}
fclose($filehandle);
if(
$flag)
{
print(
"Successfully deleted <b>$email</b> from the file<br>");
} else
{
print(
"I didn't find $todelete to delete from file");
}
} else
{
echo(
$error);
}
echo (
"Click <a href=".$_SERVER['PHP_SELF']."> Here to Refresh!<br>");
exit;
break;
default:
?>
<table summary="OutPut Name">
<tr>
<td>Name</td>
<td>Email</td>
</tr>
<?
for($i=0; $i < $sizeof; $i++)
{
echo (
"<tr>");
$user = explode(":", $file_contents[$i]);
echo (
"<td>".$user[0]."</td>");
echo (
"<td><a href='mailto:".$user[1]."'>".$user[1]."</a></td>");
echo (
"</tr>");
}
?>
</table>
<form method=POST action=<? echo ($_SERVER['PHP_SELF']); ?> >
<input type="hidden" name="action" value="add">
Username: <br>
<input type="text" name="user" size=10><br>
Email Address: <br>
<input type="text" name="email" size=10><br>
<input type="submit" value="Add New User">
</form>
<?
exit;
break;
}
?>



 

 (#342 2003-10-29 16:01:24) Post Reply

Quicksaver
Need To Set


Enrolled: Sep 2003
Posts: 90
AP: 1

In the course text, on the "A form to add to and delete members from the existing file" section, i found this line:

<input type="action" value="delete">

isnt it <input type="hidden" name="action" value="delete"> ?

were you in a rush? :)

 

 (#343 2003-10-29 16:25:29) Post Reply

Quicksaver
Need To Set


Enrolled: Sep 2003
Posts: 90
AP: 1

ok forget what i said, i got tottaly lost with that code:


<?
$file_directory
= '/usr/home/tdavid';
$error = 'Sorry, I could not open this file! Check that the permissions are set properly
for this file and that you used the correct absolute path to the file.'
;
$file_contents = file("$file_directory/.htpasswd");
sort($file_contents);
$sizeof = count($file_contents);
if((
$action != "delete") OR ($action != "add")) {
?>
<form method=POST action="<? echo($PHP_SELF); ?>">
<input type="action" value="delete">
<select name="todelete">
<?
for($i=0; $i < $sizeof; $i++) {
$user = explode(":", $file_contents[$i]);
print(
"<option value=\"$file_contents[$i]\">$user[0]</option>");
}
?>
</select> <input type="submit" value="Delete User">
</form><br>
<form method=POST action="<? echo($PHP_SELF); ?>">
<input type="action" value="add">
Username: <input type="text" name="user" size=10><br>
Password: <input type="password" name="pwd" size=10> <input type="submit" value="Add New User">
</form>
<?
// start action routine for actual deletion or additon of new members
}
?>



the form part is tottally wrong i beleive, instead of
<input type="action" value="delete">
and
<input type="action" value="add">

it should defenetly be:
<input type="hidden" name="action" value="delete">
and
<input type="hidden" name="action" value="add">

and i was gonna say something about the select dropdown for() loop but i would be dumb cuz its cool :) you could put the explode $file_contents outside the loop tho, if the file is too big it can take a considerable ammount of time to explode as many times as existing names

 

 (#400 2003-11-23 05:25:24) Post Reply

Quicksaver
Need To Set


Enrolled: Sep 2003
Posts: 90
AP: 1

hi :)


<?
global $PHP_self;
$action = $_POST["action"];
$name = $_POST["name"];
$email = $_POST["email"];
$valid = "(.+)\@(.+)\.(.+)";

if(
$action == "addemail") {
if(
eregi($valid, $email)) {
if(
$buffer = fopen("10.assignement", "a")) {
fputs($buffer, $name."§"."$email\n");
fclose($buffer); } else {
print(
"Error accessing file.");
exit(); } } else {
print(
"<font color=\"red\"><b>Error:</b> Please insert a correct email address.</font><br><br>"); } }

if(
$buffer = fopen("10.assignement", "r")) {
while(!
feof($buffer)) { $file[] = fgets($buffer); }
fclose($buffer); } else {
print(
"Error accessing file.");
exit(); }

if(
$file[0] == "") {
print(
"<b>No stored emails.</b><br><br>"); } else {

sort($file);
$number = count($file);
print(
"<b>Stored emails</b><br><table border=1 cellspacing=0>
<tr><td align=\"center\">Name</td>
<td align=\"center\">Email</td></tr>"
);

for(
$a=0; $a < $number; $a++) {
$toshow = explode("§", $file[$a]);
print(
"<tr><td>$toshow[0]</td>
<td><a href=
\"mailto:$toshow[1]\">$toshow[1]</a></td><tr>"); }

print(
"</table><br><br>"); }
?>

<table>
<form method="post" action="<? $PHP_self ?>">
<input type="hidden" name="action" value="addemail">
<tr><td>Name: </td><td><input type="text" name="name" size="50"></td></tr>
<tr><td>Email: </td><td><input type="text" name="email" size="50"></td></tr>
<tr><td aligin="left" colspan="2"><input type="submit" value="Add email"></td></tr>
</form>
</table>


working url

ho yeah, since i changed hosts all the previous assignements are located here

 

 (#754 2004-12-14 11:09:28) Post Reply

lestat
Need To Set


Enrolled: Jan 2004
Posts: 44
AP: 1

I am stuck. I can't get my page to display the data until submit is hit a second time, or the page is refreshed!? * Please help me *

Incidentally, when 'refresh' is hit, the same data is resubmitted. Whats a way around that?

Code is not error checked..yet...

<html>
<head>
<title>Week #10 BASIC ~ Lestat</title>
<link rel="stylesheet" type="text/css" href="/css/scriptskewl.css">
</head>
<body>

<?
// [ DIR PATH, CONTENTS, SORTING]
$action = $_POST['action'];
$email = $_POST['email'];
$name = $_POST['name'];
$path_to_dir = '/home/content/mypath'; // path to directory
$file_contents = file("$path_to_dir/email.txt"); // contents of the file in the dir
sort($file_contents); // alphabetically sort contents
$sizeof = count($file_contents); // count how many lines of content are in the flat file
$error = "Whoops, Can't read/write or access the file for some reason<br>"; // error msg

// [ ADD ENTRY TO FLAT FILE]
if($_POST['action'] == "true"){
if(
$filehandle = fopen("$path_to_dir/email.txt", "a")){ // Open the file
fputs($filehandle, $name.":"."$emailrn"); // Append the contents
print("Added,<b> $name </b>with the address<b> $email </b>to the flat file<br>"); // Print confirm
}else{
echo(
$error);
}

// [READ FILE CONTENTS]
if($filehandle = fopen("$path_to_dir/email.txt", "r")){
while(!
feof($filehandle)){$file_contents[] = fgets($filehandle);}
fclose($filehandle);
}else{
print(
$error);
}

// [ OUTPUT FILE CONTENTS ]
print("<table border=1><tr><td>Name:</td><td>Email:</td></tr>");
for(
$i=0; $i<$sizeof; $i++){
$entry=explode(":", $file_contents[$i]);
print (
"<tr><td>" . $entry[0] . "</td><td>" . $entry[1] . "</td></tr>");
}
print(
"</table>");
}
?>

<form method="POST" action="<? $PHP_SELF ?>">
<input type="hidden" name="action" value="true">
Name:<input type="text" name="name"><br>
Email:<input type="text" name="email"><br>
<input type="submit" name="submit" value="submit">
</form>


<pre>
</pre>
<br>
<iframe src='wk10basicsrc.php' width='100%' height='300'></iframe>
<br>

<a href='../index.html'>BACK</a>

</body>
</html>



Working url: http://tdurl.com/a7

 

 (#758 2004-12-15 15:53:04) Post Reply

TDavid
php mySQL Perl C/C++
Principal


Enrolled: Mar 2000
Posts: 193
AP: 1

Hi Lestat - as I just showed you in the irc.scriptschool.com #scriptschool chat. The $sizeof variable is being assigned a value before you add a new item to it so the for loop isn't reaching the last item in the array (the newest item).

 

 (#760 2004-12-16 15:07:32) Post Reply

lestat
Need To Set


Enrolled: Jan 2004
Posts: 44
AP: 1

OK got it. Somebody *wink* mentioned to me that my $sizeof count was up in the beggining of the code. This was causing the table to be output from the original data, not the new appended data. Its always something small!


<?
// [ DIR PATH, & CONTENTS ]
$action = $_POST['action'];
$email = $_POST['email'];
$name = $_POST['name'];
$path_to_dir = '/my/path/'; // path to directory
$error = "Whoops, Can't read/write or access the file for some reason<br>"; // error msg

// [ ADD ENTRY TO FLAT FILE ]
if(($name == " " || $email == " ") || ($name == "" || $email == "")){ // Disallow blank or empty string entries
print("<b>Invalid Entry</b>, Please go back and check your entry");
}else{
if(
ereg("([[:alnum:]\.\-]+)(\@[[:alnum:]\.\-]+\.+)", $email)){ // proceed ONLY if email is valid format
if($_POST['action'] == "true"){
if(
$filehandle = fopen($path_to_dir . "email.txt", "a")){ // Open the file
fputs($filehandle, $name . ":" . "$email\r\n"); // Append the contents
print("Added,<b> $name </b>with the address<b> $email </b>to the flat file"); // Print confirm
fclose($filehandle);
}else{
echo(
$error);
}
}
}else{
print(
"Sorry, <b>$email</b>, is not a correct email format,<br>Please go back and correct the address");
}
}

// [ READ & OUTPUT FILE CONTENTS ]
$file_contents = file($path_to_dir . "email.txt"); // contents of the file in the dir
$sizeof = count($file_contents); // count how many lines of content are in the flat file
sort($file_contents); // alphabetically sort contents
print("<table border=1> <tr><td><b>Name:</b></td><td><b>Email:</b></td></tr>");
if(
$filehandle = fopen($path_to_dir . "email.txt", "r")){
while(!
feof($filehandle)){
$file_contents[] = fgets($filehandle);
}
for(
$i=0; $i<$sizeof; $i++) {
$entry=explode(":", $file_contents[$i]);
print (
"<tr><td>" . $entry[0] . "</td><td>" . $entry[1] . "</td></tr>");
}
print(
"</table><br>");
}else{
print(
$error);
}
?>

<table border="0" align="left">
<form method="POST" action="http://www.timslan.com/scriptskewl/week10/wk10basic.php">
<input type="hidden" name="action" value="true">
<tr><td align="center">Name:<input type="text" name="name"></td></tr>
<tr><td align="center">Email:<input type="text" name="email"></td></tr>
<tr><td colspan="2"><center><input type="submit" name="submit" value="submit"></center></td></tr>
</form>
</table><br>
<br clear="left">


Working url: [url]http://tdurl.com/a7[/url]

 

 (#843 2005-07-03 05:29:56) Post Reply

vegyta2004
Need To Set


Enrolled: Apr 2005
Posts: 34
AP: 1

MY assignment:


<?
if((($_POST["name"]=="")or($_POST["name"]==" "))or(($_POST["e_mail"]=="")or($_POST["e_mail"]==" ")))
{echo"You haven't completed the form";}
else{
if(ereg("^(.+)@(.+)\\.(.+)$",$_POST["e_mail"]))
{
$file1=fopen("D:\PROGRAME INSTALATE-FUNCTIONALE\wamp\www\proiecte\assignments\week10\basic\basic.txt","a");
fwrite($file1,$_POST["name"]);
fwrite($file1,":");
fwrite($file1,$_POST["e_mail"]);
fwrite($file1,"\r\n");
$file1=fopen("D:\PROGRAME INSTALATE-FUNCTIONALE\wamp\www\proiecte\assignments\week10\basic\basic.txt","r");
while(!feof($file1))
{$info[]=fgets($file1);}
sort($info);
$sizeof=count($file1);
foreach($info as $key=>$value){$separator=explode(":",$info[$key]);?>
<table width="104" border="1" cellpadding="2" cellspacing="1" bordercolor="#0000FF" bordercolorlight="#3300FF" bgcolor="#ECE9D8">
<td width="65" ><?echo"$separator[0]";?></td>
<td width="65"><?echo"$separator[1]";?></td>
</table>

<?
}
fclose($file1);
}
else {echo"Your mail Is Invalid";}
}
?>
<br><br>
<form method="POST"action="<?PHYP_SELF?>">
NAME<input type="text"name="name"><br>
E-Mail:<input type="text"name="e_mail"><br>
<input type="submit"><br>
<input type="reset">
</form>

 

 (#925 2006-01-03 20:11:58) Post Reply

Dylan
Need To Set


Enrolled: Dec 2005
Posts: 14
AP: 1

Link to my email list script for week 10: http://www.algore.org/~admin/mailfile.php

And the code:


<?
// Write a script to add sorted email addresses and names
// to a file and with the same script how to retrieve all
// the information and print to browser the name and email
// address in a table.

$file_directory = '/Users/admin/Sites/';
$error = 'I could not open the email file! Verify permissions are correct and the absolute path to the file.';
$file_contents = file("$file_directory/emaillist.txt");
sort($file_contents);
$sizeof = count($file_contents);

// if they aren't adding a new address, show the form
if($_POST['action'] != "add") {
?>
<html><body>
<i>You may add your name to our public list below:</i> <br />
<form method=POST action="<? echo($PHP_SELF); ?>">
<input type="hidden" name="action" value="add"><br />
Your Name:
<input type="text" name="name" size="20"><br />
Email Address:
<input type="text" name="address" size="18"><br />
<input type="submit" value="Add New Email">
</form> <br />

<i>Current members:</i> <br /> <br />
<!-- Begin Table of email addresses -->
<table border="1">
<tr><td><b>NAME:</b></td><td><b>EMAIL:</b></td></tr>
</body>
</html>
<?
for($i=0; $i < $sizeof; $i++) {
$email = explode(":", $file_contents[$i]);
print(
"<tr>");
print(
"<td>".$email[0]."</td>");
print(
"<td>".$email[1]."</td>");
print(
"</tr>");
}

print (
"</table>");
// add the new email address to the file
} else {
if (
$_POST['action'] == "add") {
if(
$filehandle = fopen("$file_directory/emaillist.txt", "a")) {
fputs($filehandle, $_POST['name'].":".$_POST['address']."\n");
fclose($filehandle);
print(
"Successfully added <b>".$_POST['name']."</b> to the file");
} else {
echo(
$error);
}
}
}
?>



 

 (#981 2006-03-23 23:51:13) Post Reply

Russell
Need To Set


Enrolled: Mar 2006
Posts: 19
AP: 1

i thought this was very tough. took me all day to figure out the explode and how to open and write to the files. anyways, here it is



<?
$theform
="<form method=\"post\" action=\"$PHP_SELF\">
<input type=\"hidden\" name=\"form\" value=\"true\">
name: <input type=\"text\" name=\"yourname\"><br>
email: <input type=\"text\" name=\"youremail\"><br>
<input type=\"submit\" value=\"Submit\">"
;
$theerror="Bad info or something. check script and try again";
$file_directory="/home/rlang/domains/bigtitsfinder.com/public_html/testing";
if(
$form=="true"){
$info_to_add="$yourname|$youremail\n";
if(
$file_content=fopen("$file_directory/info.txt","a")){
fputs($file_content, $info_to_add);
fclose ($file_content);
print(
"info added<br><a href=\"$PHP_SELF?infoadded=true\">click here</a> to see the info.");
}else{
print(
"$theerror");
}
}elseif(
$infoadded=="true"){
if(
$file_content=file("$file_directory/info.txt")){
sort($file_content);
print(
"<table border=\"1\">");
$countinfo=count($file_content);
for(
$i=0; $i<$countinfo; $i++){
$user = explode("|", $file_content[$i]);
print(
"<tr><td width=\"300\">name: $user[0]</td><td width=\"300\" bgcolor=\"#aaaaaa\">email: $user[1]</td></tr>");
}
}else{
print(
"$theerror");
}
}else{
print(
"$theform");
}
?>




 

 

View Previous Thread
Print this page Print This Page

View Next Thread

School Map:  Home / Enroll $ Student Records; Class  
$ News @ LIVE Tech Radio * Support/FAQ | Store

Advertising | Link To Us | Privacy 
Copyright 2000-2002 Script School Productions / KMR Enterprises 
No part of this website may be reproduced, copied and/or distributed in any medium 
without express written permission