Author Topic: Code to Edit Flat File Entries  (Read 300 times)

x_pariah_x

  • New Member
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Code to Edit Flat File Entries
« on: July 21, 2010, 12:16:44 PM »
Hello everyone,

I am currently working on a control panel for some people who know nothing whatsoever about webpage editing to be able to edit the menu I have posted on their website. I use text file includes to display all the information on the site and I need help with saving the edited data for each section back to its original file. I already have a form set up that inserts new items into the file and then displays the entries below the form within textfields/textareas for editing. The only thing I'm stuck on is how to write each entry that's listed below the add new entry form back into the file it was read from.

Here is the test form: http://bippys.romas-ellicottcity.com/test/addedit.php

And here is the code I'm using:
PHP Code: [Select]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

<
title>Bippy's Website Control Panel: Test</title>

<style type="text/css">
<!--
body, th, td, p, small {
	
font-family: Verdana;
}

.td1 {
	

	
font-weight: bold;
	
font-size: 12px;
	
white-space: nowrap;
	
vertical-align: bottom;
	
border-bottom: 1px dotted black;
	
padding:0px;}

.td2 {
	
width: 96%;
	
vertical-align: bottom;
	
border-bottom: 1px dotted black;
	
padding:0px;}
.td3 {
	
font-size: 12px;
	
text-align: right;
	
vertical-align: bottom;
	
padding:0px;}
.td4 {
	
font-size: 10px;
	
vertical-align: top;
	
padding:0px;}

hr { 
	
height: 0;
	
border-style: dotted;
	
border-width: 0px 0px 1px 0px;
	
border-color: transparent; }

th {background-color:#dee; color:#677;}

h1 {font-size:120%; color:#558;}
-->
</style>

</head>

<body>
Editing: Test<br><br>
<table>

<form action="implode.php" method="post">
Item:<br><input type="text" name="item" size="40" maxlength="80"><br>
Description:<br><textarea name="desc" rows="3" cols="32"></textarea><br>
Price:<br><input type="text" name="price" size="40" maxlength="80"><br><br>
<input type="hidden" name="section" value="test">
<input type="submit" value="Submit">
</form>
<br><br>

<table width="500px">
<form name="edit" action="process.php" method="post">
<?php

$file_name = '
test.txt';

// this reads the file into an array
$file_array = file($file_name);

if($_GET['
action'] == 'deleteline'){
    // a delete request was made, unencode the data
    $delete_value = base64_decode($_GET['
line']);

    // loop through the file array and only include
    // the lines that aren'
t the one we want to delete into
    
// a  new array
    
foreach ($file_array as $line => $value) {
        if(
$delete_value != $value){
            
$new_array[] = $value;
        }
    }
    
// replace the old array with our new one with a
    // line removed
    
$file_array $new_array;
    
    
// collapse the array to a string
    
$file_data implode('',$file_array);

    
// write the data back to the file
    
$handle fopen($file_name'w+');
    
fwrite($handle$file_data);
    
fclose($handle);
}

reset($file_array);

$item = array();
foreach (
$file_array as $line => $value) {
	
$item explode('|'$value);
echo 
"<tr><td class='td1'><input type='text' name='item' value='".$item['0']."'><a href=".$_SERVER['PHP_SELF']."?action=deleteline&line=".base64_encode($value).">Delete</a></td>
<td class='td2'><hr></td>
<td class='td3'><input type='text' size='4' name='price' value='"
.$item['2']."'</td>
</tr><tr>
<td class='td4' colspan=2><textarea name='desc' rows='4' cols='31'>"
.$item['1']."</textarea></td>
</tr>"
;
}  
?>
<tr><td colspan="3"><input type="submit" name="submit2 value="Submit">&nbsp;&nbsp;<input type="reset" value="Reset"></td></tr>
</table>
</form>
</body>
</html>


And this is the code that adds the info into the db:
PHP Code: [Select]
<?
$item 
$_POST['item'];
$desc $_POST['desc'];
$price $_POST['price'];

$section $_POST['section'];

switch (
$section){
	
case 
"app":
	
	
$file "appetizers";
	
	
break;
	
case 
"soup":
	
	
$file "soups";
	
	
break;
	

	
case 
"sides":
	
	
$file "sides";
	
	
break;
	

	
case 
"sandwiches":
	
	
$file "sandwiches";
	
	
break;
	
case 
"entrees":
	
	
$file "entrees";
	
	
break;
	
case 
"test":
	
	
$file "test";
	
	
break;
	

}

$line = array($item$desc$price);

$delimited implode("|"$line);

$myFile "test.txt";

$fh fopen($myFile'a+') or die("can't open file");

fwrite($fh"\r\n".$delimited);

fclose($fh);

header ("location:addedit.php");
?>





PS - I haven't coded anything in about 5yrs and I was still a noob when I left off... I'm sure there are tons of more efficient ways of accomplishing this, but I would like to use flat files

Any help would be greatly appreciated!
« Last Edit: July 21, 2010, 12:20:26 PM by x_pariah_x »