473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

New PHP coder having trouble writing 2 types of data into a file.

grassh0pp3r
Hello,

I'm trying to make a very simple comments page on my site using PHP and am having problems somewhere. I am very new to PHP. I was able to create one that works with comments appended, but I want the latest comment to be on top, and that's where I'm running into trouble. Since I know very little about PHP, I thought I was clever in what I came up with. I think it can work if I get the coding right. Let me know if my logic is wrong. I'm working on a mac OS X and writing it all in simple text, but when I have time at work, I am messing around in Notepad on a PC. I get almost all of my code from tutorials and some here and there from forums.

What I've done is start with a simple form. Then it goes to the first comments page, comments.php This form retrieves the form data, opens the first text file (comments.txt) and reads it. Then it opens the second file (comments2.txt) and is supposed to write the form data at the top and the already saved data (from comments.txt) underneath it. This file also writes the form data to a database (which seems to be working) and is also supposed to echo 'thanks for your comment, $name (which isn't working). Then after 5 seconds it sends you to the second PHP doc.

The second PHP doc (fanscomments.php) then opens the comments2.txt file and echos it and also opens the comments.txt doc and writes over it with comments2.txt In this way, I was thinking it should result in the latest comment posting on the top when the whole loop is completed. I know this is a clumsy way of doing this, but as I said, I'm new to coding and wanted to have something functional, and then replace it later with something more sophisticated.

Here is the first PHP page:

<?php

if(!empty($_SERVER['HTTP_REFERER']))
header('Refresh: 5,URL="/fanscomments.php"');

// retrieve form data
$input = $_POST['name'];
$input = $_POST['eaddress'];
$input = $_POST['comments'];

// define variables
$name = "$_POST[name]";
$comments = "$_POST[comments]";
$eaddress = "$_POST[eaddress]";

echo 'Thanks for your comment, $name!';

// set file to read
$file = '/home/content/s/c/o/scodeath/html/fans/comments.txt';
// open file
$fh = fopen($file, 'r') or die('Could not open file!');
// read file contents
$commentsdata = fread($fh, filesize($file)) or die('Could not read file!');
// close file
fclose($fh);


$out = fopen("/home/content/s/c/o/scodeath/html/fans/comments2.txt", "w");

if (!$out) {
print("Could not append to file. Please try again later, or send an email to fans@summercampofdeath.com");
exit;
}

fputs($out,"\"$_POST[comments]\"\n\n");
fputs($out,"-$_POST[name]\n\n\n\n");

fwrite($out, $commentsdata);

fclose($out);



$out = fopen("/home/content/s/c/o/scodeath/html/fans/commentsdb.txt", "a");

if (!$out) {
print("Could not append to file. Please try again later, or send an email to fans@summercampofdeath.com");
exit;
}

fputs($out,"$_POST[name]\t");
fputs($out,"$_POST[eaddress]\t");
fputs($out,"$_POST[comments]\t");
fputs($out,"$_SERVER[REMOTE_ADDR]\n\n");

fclose($out);


?>

And here is the second PHP page:

<html>
<head></head>
<body>

<?php

// set file to read
$file = '/home/content/s/c/o/scodeath/html/fans/comments2.txt';
// read file into array
$data = file($file) or die('Could not read file!');
// loop through array and print each line
foreach ($data as $line) {
echo nl2br($line);
}



$out = fopen("/home/content/s/c/o/scodeath/html/fans/comments.txt", "w");

if (!$out) {
print("Could not append to file. Please try again later, or send an email to fans@summercampofdeath.com");
exit;
}

fputs($out,"$data\n\n");

fclose($out);

?>

</body>
</html>

I sure appreciate the help! BTW, why is it printing the word - Array in my webpage? Thanks!
Feb 26 '07 #1
0 1688

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
87
by: ziliath | last post by:
I recently tried out the Google "top coder" contest, as a C++ coder. I noticed immediately that they expected me to know STL. To which I say, what the fuck?! I may be missing something, but at...
10
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not...
12
by: Winbatch | last post by:
Hi, I'm trying to learn multithreading and it doesn't seem to be working for me. I have a feeling it has to do with the fact that I'm writing to files rather than to printf, but maybe not. ...
0
by: harry12 | last post by:
Hello- I'm fairly new at using Microsoft Access and I'm having trouble getting a couple of things to work in my database. The first is that I have yet to find a way to get an append query to...
2
by: spidey12345 | last post by:
what i need this program to do is to read paragraphs like "st blah blh test ere se sit blha eere w" and then it will reformat to "st blah...
1
by: John Wright | last post by:
I am running a console application that connects to an Access database (8 million rows) and converts it to a text file and then cleans and compacts the database. When it runs I get the following...
12
by: Fozzi | last post by:
Hey all I have been working on a project which allows me to write to a file and retrieve from that file, but i having trouble with it These are my inputs: private int flightno; ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.