Jump to content

Add Modify Delete a single or more lines in PHP


D3158

Recommended Posts

How would I go about doing it? I have code for them(below) but I am not sure what I am doing wrong. Your help is appreciated!

 

lab6part2menu.php

<html>

<body>

<h1>Content of your reminder file:</h1>

 

<form action="lab6part2decision.php" method="post">

 

<!--Display content of the reminder file-->

<textarea name="content" rows="15" cols ="100%" readonly="readonly">

 

<?php

include "classes.php";

$orgFile = new ModifyFile;

 

$orgFile->loadContent('cronpart2.txt');

$size=$orgFile->countLine();

 

$orgFile->displayContent();

?>

</textarea><br>

 

Select your options:<br><br>

<input type="radio" name="doWhat" value="add" />Add a line<br>

<input type="radio" name="doWhat" value="delete" />Delete a line<br>

<input type="radio" name="doWhat" value="modify" />Modify a line<br><br>

 

 

Line No. to Delete/Modify: <select name='chooseLine'>

<?php

for ($i=0; $i<$size; $i++) {

    echo "<option value='$i'>$i</option>";

}

?></select>

<br><br>

 

<!--if user select add or modify, need to fill in content to work with-->

 

Content to Add/Modify:

<?php

 

    echo "Hour";

    echo "<select name='hour'>";

    for ($hour=1; $hour<=24; $hour++) {

        echo "<option value='$hour'>$hour</option>";

    }

    echo "</select>";

 

    echo "Minute";

    echo "<select name='minute'>";

    for ($min=0; $min<=59; $min++) {

 

        if ($min<10){

            echo "<option value='0$min'>$min</option>";

        }

        else {

            echo "<option value='$min'>$min</option>";

        }

    }

    echo "</select>";

 

    echo "Day";

    echo "<select name='day'>";

    for ($day=1; $day<=31; $day++) {

        echo "<option value='$day'>$day</option>";

    }

    echo "</select>";

 

    echo "Month";

    echo "<select name='month'>";

    for ($month=1; $month<=12; $month++) {

        echo "<option value='$month'>$month</option>";

    }

    echo "</select>";

 

    echo "Year";

    echo "<input type='text' size='4' maxlength='4' name='year' />";

 

    echo "Email";

    echo "<input type='text' name='email' />";

 

    echo "Message";

    echo "<input type='text' name='message' />";

?>

 

<br>

<br>

<input type="submit" value="Submit" />

<input type="reset" value="Reset" />

</form>

 

 

 

 

lab6part2decision.php

<?php

if (!isset($_POST["doWhat"])){

    die("No selection made.<a href='lab6part2menu.php'>Back</a>");

}

$choice=$_POST["doWhat"];

 

include "classes.php";

 

$oldFile = new ModFile;

$oldFile->loadContent("email.txt");

$oldFile->countLine();

 

switch ($choice) {

 

    case "add":

        if (!preg_match('/\b\d\d\d\d\b/', trim($_POST['year']))) {

            die("Year is not valid. <br><a href='lab6part2menu.php'>Back</a>");

        }

 

        if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", trim($_POST['email']))) {

            die("Email is not valid. <br><a href='lab6part2menu.php'>Back</a>");

        }

 

        $new=$_POST['hour'].",".$_POST['minute'].",".

        $_POST['day'].",".$_POST['day'].",".$_POST['month'].",".

        trim($_POST['year']).",".trim($_POST['email']).",".trim($_POST['message']);

 

        $oldFile->addLine($new);

        $oldFile->countLine();

        $oldFile->writeToFile('email.txt');

        break;

 

    case "delete":

 

        $lineToDelete=$_POST["chooseLine"];

        $oldFile->deleteLine($lineToDelete);

        $oldFile->countLine();

        $oldFile->writeToFile('email.txt');

        break;

 

    case "modify":

        $lineToModify=$_POST["chooseLine"];

 

        if (!preg_match('/\b\d\d\d\d\b/', trim($_POST['year']))) {

            die("Year is not valid. <br><a href='lab6part2menu.php'>Back</a>");

        }

 

        if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", trim($_POST['email']))) {

            die("Email is not valid. <br><a href='lab6part2menu.php'>Back</a>");

        }

 

        $change=$_POST['hour'].",".$_POST['minute'].",".

        $_POST['day'].",".$_POST['day'].",".$_POST['month'].",".

        trim($_POST['year']).",".trim($_POST['email']).",".trim($_POST['message']);

 

        $oldFile->modifyLine($lineToModify,$change);

        $oldFile->countLine();

        $oldFile->writeToFile('email.txt');

        break;

}

?>

<html>

<head>

</head>

<body>

 

<h1>Content of your REVISED reminder file:</h1>

<form><textarea name='content' rows='20' cols ='100%' readonly='readonly'>

 

<?php

$oldFile->countLine();

$oldFile->displayContent();

?>

 

</textarea></form>

<a href="lab6part2menu.php">Back for More Change</a>

</body>

</html>

 

classes.php

<?php

 

class ModifyFile {

    public $arrayContent;

    public $endofline;

    public $size;

 

    function ModifyFile () {

        if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {

            $this->endofline="\r\n";

        }

        elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {

            $endofline="\r";

        }

        else {

            $endofline="\n";

        }

        //return $endofline;

 

        //$endofline="\r\n";

    }

 

    //open a file to write, from beginning of line

    function writeToFile($filepath) {

        $fh=fopen($filepath,'w');

        if (!$fh) {

            die ("error opening file.");

        }

        //lock file

 

        foreach($arrayContent as $temp) {

            fwrite($fh,$temp);

 

        }

        //unlock file

        fclose($fh);

    }

 

    //add a line to content array

    function addLine($newLine) {

        $newLine="\r\n".$newLine."\r\n";

        return array_push($arrayContent,$newLine);

        $temp=$size;

        $temp=$temp-1;

        $arrayContent[$temp]=$arrayContent[$temp].$endofline;

        array_push($arrayContent,$newLine);

        return $arrayContent;

 

    }

 

    //delete a line from content array

    function deleteLine($delLine) {

        //step through each line of the original file

        for ($i=0; $i<$this->size; $i++) {

            //only copy line over to another array when line is not the line to delete

            if ($delLine!=$i) {

                $modArrayContent[]=$arrayContent[$i];

            }

        }

 

        $temp=$size-1;

        print $temp;

        if ($modArrayContent[count($modArrayContent)-1]=="") {

            array_pop($modArrayContent);

        }

 

        $temp=array($arrayContent[$delLine],$endofline, "\r\n", "\r","\n");

     

        //$arrayContent=array_diff($arrayContent, $temp);

        //print count($arrayContent);

        return $arrayContent=array_diff($arrayContent, $temp);

        /*print count($modArrayContent);

        $arrayContent=$modArrayContent;

        print count($arrayContent);*/

        //return $arrayContent=$modArrayContent;

    }

 

    //modify a line from content array

    function modifyLine($modLine,$someString) {

        $size=count($arrayContent);

        $someString=$someString.$endofline;

        for ($i=0; $i<$size; $i++) {

            if ($modLine!=$i) {

                $modArrayContent[$i]=$arrayContent[$i];

            }

            else {

                $modArrayContent[$i]=$someString;

            }

        }

        return $arrayContent=$modArrayContent;

    }

 

    //show file content, one line at a time

    function displayContent() {

        for($i=0; $i<$this->size; $i++) {

            print "Line No. " . $i . ": " . $arrayContent[$i];

        }

 

    }

 

    //count number of line in file

    function countLine() {

        $size=count($arrayContent);

        return $this->size;

    }

 

    //load content of txt as an array of lines

    function loadContent($filepath) {

        $fh=fopen($filepath,'r');

        if (!$fh) {

            die ("error opening file.");

        }

        $count=0;

        while (!feof($fh)){

            $arrayContent[$count] = fgets($fh) ;

            $count++;

        }

        return $arrayContent;

    }

 

 

}

?>

 

 

So basically this is what I see on lab6part2menu.php page.

 

Select your option:

Radio button: Add

Radio button: Modify

Radio button: Delete

 

Line No. to Delete/Modify = dropdown (doesnt have any values to it... I am assuming it's because the content within the table isn't shown which is indicated by line number 1 to 10 or something.. BUT it has this mean thing:

 

<br>

<b>Notice</b>: Undefined variable: arrayContent in

<b>/home/..../..../..../public_html/..../Lab6/classes.php</b> on line <b>89</b><br/>)

 

Content to Add/Modify = Hrs [Dropdown from 1:00-24:00]

                                      Mins[Dropdown from 1:00-59:00]

                                      Day [Dropdown from 1-31]

                                    Month[Dropdown from 1-12]

 

                                    Year [Whatever year user wants to input]

                                  E-mail[Whatever email user wants to input]

                              Message[Whatever message user wants to input]

 

SUBMIT                RESET

 

 

 

Thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.