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

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

 (#20 2003-08-07 17:03:38) Post Reply

TDavid
php mySQL Perl C/C++
Principal


Enrolled: Mar 2000
Posts: 193
AP: 1

Week #11 Basic TO-DO ASSIGNMENT: Write a script to search through the contents of a file CASE iNsensiTiveLy, match all entries and change from the information submitted on a form and print the entire contents of the file to the browser. All should be self enclosed in one script with find and replace text boxes and use filelocking.

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!

 

 (#404 2003-11-25 15:22:20) Post Reply

Quicksaver
Need To Set


Enrolled: Sep 2003
Posts: 90
AP: 1

used last week's email script for this one



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

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

sort($file);
$number = count($file);

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

if(
$action == "deleteemail") {
if(
$buffer = fopen("10.assignement", "w")) {
flock($buffer, 2);
for(
$a=1; $a<$number; $a++) {
if(
$a != $theone) {
fwrite($buffer, $file[$a]); } }
flock($buffer, 3);
fclose($buffer);
$action = "done";
header("Location: $SCRIPT_NAME"); } else {
print(
"Error accessing file.");
exit(); } }

if(
$action == "modify") {
$toshow = explode("§", $file[$theone]);
print(
"<table>
<tr><td>
<fieldset><legend><b>From:</b></legend>
<table>
<tr><td>Name: $toshow
[0]</td></tr>
<tr><td>Email: $toshow
[1]</td></tr>
</table></fieldset></td>
<td><form method=
\"post\" action=\"$PHP_self\">
<input type=
\"hidden\" name=\"action\" value=\"modifyemail\">
<input type=
\"hidden\" name=\"theone\" value=\"$theone\">
<fieldset><legend><b>To:</b></legend>
<table>
<tr><td>Name: </td><td><input type=
\"text\" name=\"name\" value=\"$toshow[0]\"size=\"40\" maxlength=\"50\"></td></tr>
<tr><td>Email: </td><td><input type=
\"text\" name=\"email\" value=\"$toshow[1]\" size=\"40\" maxlength=\"50\"></td></tr>
<tr><td align=
\"right\"><input type=\"submit\" value=\"Modify email\"></td></tr>
</table></fieldset></td></tr></table>"
);
exit(); }

if(
$action == "modifyemail") {
if(
$buffer = fopen("10.assignement", "w")) {
flock($buffer, 2);
for(
$a=1; $a<$number; $a++) {
if(
$a != $theone) {
fwrite($buffer, $file[$a]); } else {
fwrite($buffer, $name."§"."$email\r\n"); } }
flock($buffer, 3);
fclose($buffer);
$action = "done";
header("Location: $SCRIPT_NAME"); } else {
print(
"Error accessing file.");
exit(); } }

if(
$action == "searchemail") {
if(
$searchfor == "name") {
print(
"<b>Emails found</b><br><table border=1 cellspacing=0>
<tr><td align=\"center\">Name</td>
<td align=\"center\" colspan=\"3\">Email</td></tr>"
);
for(
$a=1; $a < $number; $a++) {
$toshow = explode("§", $file[$a]);
if(
eregi($searchterms, $toshow[0])) {
$flag = "found";
print(
"<tr><td>$toshow[0]</td>
<td><a href=
\"mailto:$toshow[1]\">$toshow[1]</a></td>
<form method=
\"post\" action=\"$PHP_self\">
<input type=
\"hidden\" name=\"action\" value=\"deleteemail\">
<input type=
\"hidden\" name=\"theone\" value=\"$a\">
<td><input type=
\"submit\" value=\"delete\"></td></form>
<form method=
\"post\" action=\"$PHP_self\">
<input type=
\"hidden\" name=\"action\" value=\"modify\">
<input type=
\"hidden\" name=\"theone\" value=\"$a\">
<td><input type=
\"submit\" value=\"modify\"></td></form></tr>"); } }
print(
"</table><br><br>"); }

if(
$searchfor == "email") {
print(
"<b>Emails found</b><br><table border=1 cellspacing=0>
<tr><td align=\"center\">Name</td>
<td align=\"center\" colspan=\"3\">Email</td></tr>"
);

for(
$a=1; $a < $number; $a++) {
$toshow = explode("§", $file[$a]);
if(
eregi($searchterms, $toshow[1])) {
$flag = "found";
print(
"<tr><td>$toshow[0]</td>
<td><a href=
\"mailto:$toshow[1]\">$toshow[1]</a></td>
<form method=
\"post\" action=\"$PHP_self\">
<input type=
\"hidden\" name=\"action\" value=\"deleteemail\">
<input type=
\"hidden\" name=\"theone\" value=\"$a\">
<td><input type=
\"submit\" value=\"delete\"></td></form>
<form method=
\"post\" action=\"$PHP_self\">
<input type=
\"hidden\" name=\"action\" value=\"modify\">
<input type=
\"hidden\" name=\"theone\" value=\"$a\">
<td><input type=
\"submit\" value=\"modify\"></td></form></tr>"); } }
print(
"</table><br><br>"); }

if(!
$flag) {
print(
"<b>No entries found matching your criteria.</b>"); } } else {

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

print(
"<b>Stored emails</b><br><table border=1 cellspacing=0>
<tr><td align=\"center\">Name</td>
<td align=\"center\" colspan=\"3\">Email</td></tr>"
);

for(
$a=1; $a < $number; $a++) {
$toshow = explode("§", $file[$a]);
print(
"<tr><td>$toshow[0]</td>
<td><a href=
\"mailto:$toshow[1]\">$toshow[1]</a></td>
<form method=
\"post\" action=\"$PHP_self\">
<input type=
\"hidden\" name=\"action\" value=\"deleteemail\">
<input type=
\"hidden\" name=\"theone\" value=\"$a\">
<td><input type=
\"submit\" value=\"delete\"></td></form>
<form method=
\"post\" action=\"$PHP_self\">
<input type=
\"hidden\" name=\"action\" value=\"modify\">
<input type=
\"hidden\" name=\"theone\" value=\"$a\">
<td><input type=
\"submit\" value=\"modify\"></td></form></tr>"); }

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

<table>
<tr><td><fieldset><legend><b>Add email</b></legend><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="40" maxlength="50"></td></tr>
<tr><td>Email: </td><td><input type="text" name="email" size="40" maxlength="50"></td></tr>
<tr><td align="right"><input type="submit" value="Add email"></td></tr>
</form></table></fieldset></td>

<? if($file[1] != "") { ?>
<td></td>
<td><fieldset><legend><b>Search for</b></legend><table>
<form method="post" action="<? $PHP_self ?>">
<input type="hidden" name="action" value="searchemail">
<tr><td><input type="text" name="searchterms" size="40" maxlength="50"></td></tr>
<tr><td align="center"><input type="radio" name="searchfor" value="name" checked>name <input type="radio" name="searchfor" value="email">email</td></tr>
<tr><td align="right"><input type="submit" value="search"></td></tr>
</form></table></fieldset></td></tr></table>

<? } else { ?>
</tr></table>

<? } ?>



working url

 

 (#789 2005-03-21 11:24:17) Post Reply

lestat
Need To Set


Enrolled: Jan 2004
Posts: 44
AP: 1

These are getting tougher as we go! I'm srarting to have difficulty with order. Getting things to print out where I want etc.
This code looks a bit sloppy due to all the html formatting Iv'e done to make it look the way I want. I gotta think that includes may change that :) (Next time I will make an unformatted code to post, so others looking at this can use it for a reference)
Ok, heres my code:


<?php
// [ Define Vars ]
$action = $_POST['action'];
$entry = $_POST['entry'];
$entry_to_find = $_POST['entry_to_find'];
$delete = $_POST['delete'];
$modify = $_POST['modify'];

// [ Open the file for Reading ]
if($fh = fopen("basic/email.txt", "r")){
flock($fh,LOCK_SH);
while(!
feof($fh)){
$file[] = fgets($fh);
}
flock($fh,LOCK_UN);
fclose($fh);
}
?>

<table border="0" width="98%">
<tr>
<td width="30%" valign="top">
<?php
// [ Add An Entry ]
if($action == "add_entry"){
if(
$fh = fopen("basic/email.txt" , "a")){
flock($fh,LOCK_EX);
if(
$entry_to_add != ""){
fputs($fh, $entry_to_add . "\n");
flock($fh,LOCK_UN);
fclose($fh);
print(
"<center><b>Added</b> <i>$entry_to_add</i></center>");
}else{
print(
"<b><center><font color=red>Nothing to Add</font></center></b>");
}
}
}
?>

<table border="0" width="95%" cellpadding="0" cellspacing="0">
<tr>
<td align="right"> Add an Entry:</td>
<td align="right">
<form method="POST" action="<? echo($PHP_SELF); ?>" style="margin:0;">
<input type="hidden" name="action" value="add_entry">
<input type="text" name="entry_to_add" size="15">
</td>
<td align="left"> <input type="submit" value="Add"></form></td>
</tr>
<tr>
<td align="right"> Search For Entry:</td>
<td align="right">
<form method="POST" action="<? echo($PHP_SELF); ?>" style="margin:0;">
<input type="hidden" name="action" value="search_for_entry">
<input type="text" name="entry_to_find" size="15">
</td>
<td align="left"> <input type="submit" value="Search"></form></td>
</tr>
</table>
</td>
<td width="25%" valign="top"> <?php

// [ Search For An Entry ]
if($action == "search_for_entry"){
if(
$entry_to_find != ""){
print(
"<center><b>Found entries for</b> <i>$entry_to_find:</i>
<table border=1 cellpadding=0 cellspacing=0>"
);
foreach(
$file as $key => $entry){
if(
eregi($entry_to_find,$entry)){
print(
" <tr>
<td align=right>$entry</td>
<td align=left> <form method=\"POST\" action=$PHP_SELF style=\"margin:0;\">
<input type=\"hidden\" name=\"action\" value=\"modify_entry\">
<input type=\"hidden\" name=\"modify\" value=\"$key\">
<input type=\"submit\" value=\"Modify\"></form>
</td>
<td align=left> <form method=\"POST\" action=$PHP_SELF style=\"margin:0;\">
<input type=\"hidden\" name=\"action\" value=\"delete_entry\">
<input type=\"hidden\" name=\"delete\" value=\"$key\">
<input type=\"submit\" value=\"Delete\"></form>
</td>
</tr>
"
);
}
}
print(
"</table></center>");
}else{
print(
"<b><font color=red>Nothing was entered</font></b><br>");
}
}

// [ Delete Entry ]
if($action == "delete_entry"){
$fh = fopen("basic/email.txt" , "w");
flock($fh,LOCK_EX);
foreach(
$file as $key => $entry){
if(
$key != $delete){
fputs($fh,$entry);
}
}
flock($fh,LOCK_UN);
fclose($fh);
print(
"<b>Deleted</b> <i>$file[$delete]</i>");
}

// [ Modify Entry ]
if($action == "modify_entry"){
print(
" <table border=0 width=100%>
<tr>
<td>File to be modified</td>
</tr>
</tr>
<td> <form method=\"POST\" action=$PHP_SELF style=\"margin:0;\">
<input type=\"hidden\" name=\"action\" value=\"update_entry\">
<input type=\"hidden\" name=\"toupdate\" value=\"$modify\">
<input type=\"text\" size=\"15\" name=\"changed_entry\" value=\"$file[$modify]\">
<input type=\"submit\" value=\"Update\"></form>
</td>
</tr>
</table>
"
);
}

// [ Update the Modified Entry ]
if($action == "update_entry"){
if(
$changed_entry != ""){
// Remove Entry
$fh = fopen("basic/email.txt" , "w");
flock($fh,LOCK_EX);
foreach(
$file as $key => $entry){
if(
$key != $toupdate){
fputs($fh,$entry);
}
}
flock($fh,LOCK_UN);
fclose($fh);

// Append Revised Entry
if($fh = fopen("basic/email.txt" , "a")){
flock($fh,LOCK_EX);
if(
$file[$toupdate] != ""){
fputs($fh, $changed_entry . "\n");
flock($fh,LOCK_UN);
fclose($fh);
print(
"<b>changed</b> <i>$file[$toupdate]</i> <b>to</b> <i>$changed_entry</i>");
}
}
}else{
print(
"<b><font color=red>You can't add an empty entry</font></b>");
}
}
?>
</td>
<td width="15%">
<?php $file = file("basic/email.txt"); ?>
<table border="1" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td><u><b>Entries:</b></u><br><?php foreach($file as $entry){print("$entry<br>");} ?>
</td>
</tr>
</table>
</td>
</tr>
</table>



Working URL: http://tdurl.com/db

 

 (#847 2005-07-30 14:53:51) Post Reply

vegyta2004
Need To Set


Enrolled: Apr 2005
Posts: 34
AP: 1

MY ASSIGNMENT:


<form name="catutare"method="POST"action="<?php $PHP_SELF?>">
Cauta:<input type="text"name="find"><br>
<input type="submit"value="Search"><br>
</form>
<form name="adaugare"method="POST"action="<?php $PHP_SELF?>">
Adauga:<input type="text"name="add"><br>
<input type="submit"value="add">
</form>
<?
//citirea din fisier--------Inceput
$path="/home/vegyta/public_html/proiecte/basic.txt";
$file=fopen($path,"r");
flock($file,LOCK_SH);
while(!feof($file))
{$info[]=fgets($file);}
?>
<p align="center">
<font size=4 color=Blue>Date existente:</font>
<table border=1>
<td>
<?
foreach($info as $key=>$value)
{echo"$value","<br>";}
?></td> </table> </p>
<?
//citirea din fisier--------Sfarsit
flock($file,LOCK_UN);fclose($file);
//adaugarea In fisier-----> Inceput
if($_POST["add"]){
$file4=fopen($path,"a");
flock($file4,LOCK_EX);
if((fwrite($file4,$_POST["add"]))&&(fwrite($file4,"\r\n"))) {echo"element adaugat";} else{echo"eroare la adaugare";}
flock($file4,LOCK_UN); fclose($file4);}
//adaugarea In fisier-----> Sfarsit

//Search------->Inceput

if(($_POST["find"]=="")||($_POST["find"]==" ")) {}
else{$fisier=file($path);
?>
<div align="center">
<font size=4 color=Blue>Rezultatele Cautarii:</font>
<table border=1>
<?
$b=0;
foreach($fisier as $key=>$value)
{if(eregi($_POST["find"],$value)){
echo"<tr>";
echo"<td>";
echo"$value","<br>";$a=1;$b=$b+1; echo"</td>"; }}
?></table></div>
<?
if($a==1) {}
else{echo"elementul cautat nu exista";}
}
if($b>0) { echo"<div align=\"center\">";
?>
<form metod="GET" action="<?$PHP_SELF?>">
<input type="submit" value="Modification">
<input type="hidden" name="Modification" value="action1">
</form>
<?
echo"<form metod=\"POST\" action=\"$PHP_SELF\">";
echo"<input type=\"submit\"value=\"Delete\">";
echo"<input type=\"hidden\" name=\"Delete\"value=\"action2\">";
echo"</form>";
echo"</div>";}
//MODIFICAREA UNEI VALORI

if($_GET["Modification"]){
$fisier=file($path);
echo"<form metod=\"GET\"action=\"$PHP_SELF\">";
foreach($fisier as $key=>$value){
echo"$value";
echo"<input type=\"checkbox\" name=\"ver\" value=\"$value\">","<br>" ;}
echo"<input type=\"text\"name=\"New\">";
echo"<input type=\"submit\"value=\"Update Value\">";
echo"</form>";
}
if($_GET["New"]) {
$verificare=$_GET["ver"];
echo"$verificare";
$fisier=file($path);
$fisier2=fopen($path,"w");
flock($fisier2,LOCK_EX);
foreach($fisier as $key=>$value) {
if(eregi($verificare,$value)){echo"verificat";}

else{ fwrite($fisier2,$value);}
}
if((fwrite($fisier2,$_GET["New"]))
and (fwrite($fisier2,"\r\n")))
{echo"Valoarea a fost modificata cu succes";}
else{echo"eroare la adaugarea noii valori";}
flock($fisier2,LOCK_UN); fclose($fisier2); }
// stergerea


if($_GET["Delete"]){
$fisier=file($path);
echo"<form metod=\"GET\"action=\"$PHP_SELF\">";
foreach($fisier as $key=>$value){
echo"$value";
echo"<input type=\"checkbox\" name=\"del\" value=\"$value\">","<br>" ;}
echo"<input type=\"submit\"value=\"Delete Value\">";
echo"</form>";
}
if($_GET["del"]) {
$verificare_del=$_GET["del"];
echo"$verificare_del";
$fisier=file($path);
$fisier2=fopen($path,"w");
flock($fisier2,LOCK_EX);
foreach($fisier as $key=>$value) {
if(eregi($verificare_del,$value)){echo"Valoare Stearsa";}

else{ fwrite($fisier2,$value);}
}

flock($fisier2,LOCK_UN); fclose($fisier2); }

?>



 

 (#848 2005-07-30 14:54:45) Post Reply

vegyta2004
Need To Set


Enrolled: Apr 2005
Posts: 34
AP: 1

OO,my working url http://www.vegyta.ropage.com/p...eek11_basic.php

 

 (#926 2006-01-04 19:22:22) Post Reply

Dylan
Need To Set


Enrolled: Dec 2005
Posts: 14
AP: 1

I sure had trouble with an extra black space sneaking into my variables -- thanks go to Lestat for showing me how to use trim(); to clear that issue up!

Working link: http://www.algore.org/~admin/m...le_advanced.php

And the code follows (note that the code is visible the link above I'm testing out the other new function Lestat showed me highlight_file();



<?
// Using your email script from last week, add
// functionality so that you will only send to one email
// address per domain (to avoid spamming the same
// domain). Also make sure you have filelocking in place.

$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(
$filehandle = fopen("$file_directory/emaillist.txt", "a"))
{
// is the submitted email address in valid format?
if(ereg("([[:alnum:]\.\-]+)(\@[[:alnum:]\.\-]+\.+)", $_POST['address']))
{
$domain = explode("@", ($_POST['address']));
for (
$i=0; $i < $sizeof; $i++) {
$stored = explode("@", $file_contents[$i]);
$stored[1] = trim($stored[1]);
if (
$stored[1] == $domain[1])
{
print(
"Sorry, someone from your domain has already registered.");
exit;
}
}
flock($filehandle, 2); // lock emaillist.txt
fputs($filehandle, $_POST['name'].":".$_POST['address']."\n");
flock($filehandle, 3); // unlock emaillist.txt
fclose($filehandle);
print(
"Successfully added <b>".$_POST['name']."</b> to the file");
} else {
print(
"Your email address appears to be invalid, please try again.");
exit; }
}
else
{
echo(
$error);
}
}
highlight_file("mailfile_advanced.php");
?>



 

 

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