Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing a flat file (newbie)

Status
Not open for further replies.

specterge

Programmer
Joined
Oct 10, 2002
Messages
2
Location
US
ok, so i have been trying to edit a flat file with something like a visitor log that tracks some info about the user and saves it to log.txt this file looks something like
Code:
time|agent|IP|referer
time|agent|IP|referer
07-15-03 01:45:30|Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)|123.213.123.321|[URL unfurl="true"]http://www.site.com[/URL]
07-15-03 01:45:31|Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)|123.213.123.321|[URL unfurl="true"]http://www.site.com[/URL]

and i wrote some script to read the file and edit the file. view.php simply just reads the file and outputs it through a HTML table for view. Right now edit_data.php takes the array and makes it so that it is all displayed in a form field for each entry (i would like to change it so that i can simply have a link to click on the view page so that i can edit a specific entry.)

reader(view.php)
Code:
<?PHP
/* ( GET FILE CONTENTS )----------------------------------------------- */
$file = 'log.txt';
$fhandle = fopen( $file, &quot;r&quot; );
$contents = fread( $fhandle, filesize ( $file ) );
fclose( $fhandle );
clearstatcache();

/* ( CONSTRUCT ENTRIES )----------------------------------------------- */
$entries = explode('/n', $contents );
array_pop( $entries );

/* ( START HTML )------------------------------------------------------ */
?>
<html>
<head>
<title>Page</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<style type=&quot;text/css&quot;>
<!--
body {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	color: #FFFFFF;
	margin: 0px;
	background-color: #000000;
	cursor: default;
}
.head {
	background-color: #355CC1;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	color: #CCCCCC;
	padding: 3px 6px;
	font-weight: bold;
	border-top: 1px solid #000000;
	border-right: 1px solid #000000;
	border-bottom: 3px solid #000000;
	border-left: 1px solid #000000;
	cursor: default;
}
.mid {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	color: #355CC1;
	background-color: #C0C0C0;
	border: 1px solid #000000;
	padding: 2px 6px;
	cursor: default;
}
.lower {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	color: #000000;
	background-color: #808080;
	border: 1px solid #000000;
	padding: 6px;
	cursor: default;
}
-->
</style>
</head>
<body>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table width=&quot;85%&quot; border=&quot;0&quot; align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
<tr> 
<td width=&quot;25%&quot; height=&quot;18&quot; class=&quot;head&quot;><B>DATE / TIME</B></td>
<td width=&quot;50%&quot; class=&quot;head&quot;><B>AGENT</B></td>
<td width=&quot;25%&quot; class=&quot;head&quot;><B>IP ADDRESS</B></td>
</tr>
<?php
/* ( BUILD LOG )-------------------------------------------------------- */
foreach ( $entries as $key=> $event )
	{
	$data = explode( '|', $event );
?>
<tr>
<td height=&quot;24&quot; class=&quot;mid&quot;>
<?PHP
echo stripslashes( $data['0'] );
?>
</td>
<td class=&quot;mid&quot;>
<?PHP
echo stripslashes( $data['1'] );
?>
</td>
<td class=&quot;mid&quot;>
<?PHP
echo stripslashes( $data['2'] );
?>
</td>
</tr>
<tr> 
<td height=&quot;30&quot; colspan=&quot;3&quot; class=&quot;lower&quot;>
<?PHP
echo stripslashes( $data['3'] );
?>
</td>
</tr>
<tr>
<td height=&quot;18&quot; colspan=&quot;3&quot;>&nbsp;</td>
</tr>
<?PHP
}
?>
</table>
<p>&nbsp;</p>
</body>
</html>

Data Editor (edit_data.php)
Code:
<?PHP
/* ( CHECK REQUEST FOR POST OR GET )----------------------------------- */
if ( isset ( $_POST['submit'] ) )
	{
	$file = 'log.txt';
	$fhandle = fopen( $file, &quot;w&quot; );
	fwrite( $fhandle, '' );
	fclose( $fhandle );
	$fhandle = fopen( $file, &quot;a&quot; );
	$x = $_POST['count'];
	$i = '1';
	while ( $i <= $x )
		{
		$time = $_POST['time'.$i];
		$agent = $_POST['agent'.$i];
		$ipadd = $_POST['ipadd'.$i];
		$referer = $_POST['referer'.$i];

		$data = $time.'|'.$agent.'|'.$ipadd.'|'.$referer.'\n';
		fwrite( $fhandle, $data );
		$i++;
	}

fclose( $fhandle );
clearstatcache();

header( 'location: view.php' );
exit;
}

/* ( GET FILE CONTENTS )----------------------------------------------- */
$file = 'log.txt';
$fhandle = fopen( $file, &quot;r&quot; );
$contents = fread( $fhandle, filesize ( $file ) );
fclose( $fhandle );
clearstatcache();

/* ( CONSTRUCT ENTRIES )----------------------------------------------- */
$entries = explode( &quot;*&quot;, $contents );
array_reverse( $entries );
$count = count( $entries );

/* ( START HTML )------------------------------------------------------ */
?>
<html>
<head>
<title>Edit Page</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<style type=&quot;text/css&quot;>
<!--
body {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	color: #FFFFFF;
	margin: 0px;
	background-color: #000000;
	cursor: default;
}
.head {
	background-color: #355CC1;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	color: #CCCCCC;
	padding: 3px 6px;
	font-weight: bold;
	border-top: 1px solid #000000;
	border-right: 1px solid #000000;
	border-bottom: 3px solid #000000;
	border-left: 1px solid #000000;
	cursor: default;
}
.mid {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: #000000;
	background-color: #C0C0C0;
	border: 1px solid #000000;
	padding: 2px 6px;
	cursor: default;
}
.lower {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: #000000;
	background-color: #808080;
	border: 1px solid #000000;
	padding: 6px;
	cursor: default;
}
-->
</style>
<style type=&quot;text/css&quot;>
<!--
.fields {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: #000000;
	border: none;
}
.button {
	background : #355CC1;
	border : 1 solid #000000;
	color : #FFFFFF;
	font-family : Verdana, Arial, Helvetica, sans-serif;
	font-size : 9px;
	font-weight : bold;
}
-->
</style>
</head>
<body>
<p>&nbsp;</p>
<p>&nbsp;</p>
<form action=&quot;<?PHP echo $_SERVER['PHP_SELF']; ?>&quot; method=&quot;post&quot;>
<input name=&quot;count&quot; type=&quot;hidden&quot; value=&quot;<?PHP echo $count; ?>&quot;>
<table width=&quot;85%&quot; border=&quot;0&quot; align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
<tr> 
<td height=&quot;39&quot; class=&quot;head&quot;>There is a total of <?PHP echo $count; ?> events logged in the DB.</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table>
<table width=&quot;85%&quot; border=&quot;0&quot; align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
<?php
/* ( BUILD INTERFACE )---------------------------------------------------- */
$x = '1';
foreach ( $entries as $key=> $event )
	{
	$data = explode( '|', $event );
	$clean = str_replace( '\n', '', $data['3'] );
?>
<tr> 
<td width=&quot;15%&quot; height=&quot;31&quot; class=&quot;mid&quot;>Date:</td>
<td width=&quot;85%&quot; class=&quot;mid&quot;><input name=&quot;time<?PHP echo $x; ?>&quot; type=&quot;text&quot; class=&quot;fields&quot; id=&quot;time<?PHP echo $x; ?>&quot; value=&quot;<?PHP echo stripslashes( $data['0'] ); ?>&quot; size=&quot;45&quot;></td>
</tr>
<tr> 
<td height=&quot;30&quot; class=&quot;lower&quot;>Agent </td>
<td height=&quot;30&quot; class=&quot;lower&quot;><input name=&quot;agent<?PHP echo $x; ?>&quot; type=&quot;text&quot; class=&quot;fields&quot; id=&quot;agent<?PHP echo $x; ?>&quot; value=&quot;<?PHP echo stripslashes( $data['1'] ); ?>&quot; size=&quot;45&quot;></td>
</tr>
<tr> 
<td height=&quot;31&quot; class=&quot;mid&quot;>IP Address:</td>
<td height=&quot;31&quot; class=&quot;mid&quot;><input name=&quot;ipadd<?PHP echo $x; ?>&quot; type=&quot;text&quot; class=&quot;fields&quot; id=&quot;ipadd<?PHP echo $x; ?>&quot; value=&quot;<?PHP echo stripslashes( $data['2'] ); ?>&quot;></td>
</tr>
<tr> 
<td height=&quot;18&quot; class=&quot;lower&quot;>Refferer</td>
<td height=&quot;18&quot; class=&quot;lower&quot;><input name=&quot;referer<?PHP echo $x; ?>&quot; type=&quot;text&quot; class=&quot;fields&quot; id=&quot;referer<?PHP echo $x; ?>&quot; value=&quot;<?PHP echo stripslashes( $data['3'] ); ?>&quot; size=&quot;45&quot;>
</td>
</tr>
<tr> 
<td height=&quot;18&quot; colspan=&quot;2&quot;>&nbsp;</td>
</tr>
<?PHP
$x++;
}
?>
</table>
<table width=&quot;85%&quot; border=&quot;0&quot; align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
<tr> 
<td width=&quot;15%&quot; class=&quot;mid&quot;>&nbsp;</td>
<td width=&quot;85%&quot; height=&quot;40&quot; class=&quot;mid&quot;><input name=&quot;submit&quot; type=&quot;submit&quot; class=&quot;button&quot; id=&quot;submit&quot; value=&quot;.:submit:.&quot;></td>
</tr>
</table>
</form>
<p>&nbsp;</p>
</body>
</html>

so whats wrong with my script? it won't really do anything, i'm not sure but is it something to do with the way i am trying to explode '/n'?? also if you could give me a help with making the edit link so that i can edit one entry at a time. if anyone could try to help me i would greatly appreciate it.
 
I would certainly start with your explode statement.
I see two things that give me pause:
First, I think that the proper escape character is a backslash &quot;\&quot;, not a forward slash &quot;/&quot;...
Second, I know from experiance that strings with thus-escaped characters need to be enclosed with double quotes (e.g &quot;\n&quot;), not single quotes (e.g. '\n').

Try fiddling with these and let us know.
 
yeah, that was it, it was the double quotes that i was missing, i thought i had tried that, but it worked now. thanks, and the forwardslash was my bad, just a brain fart i had i guess. thanks a ton sjpatrick. ok, so now that that is changed, is there any simple way of making a link in view.php for each set that would allow the editing of only one entrie at a time??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top