Jump to content

Newbie Help - Is it possible to do this? Think so, but not working properly.


j0mby

Recommended Posts

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!

Link to comment
Share on other sites

I was assuming it would be harder (and take longer) to accomplish, since I don't know much about mysql. I was only trying to "throw it up there", and then probably, down the road, replace it with one that uses a mysql database. but if it's just a few more steps to do that, and the way I'm trying  to do it now is silly, I'd take your advice and do some mysql tutorials, etc.

 

But I was also curious as to why what I'm trying to do isn't working. I've learned a lot about php by trying it, so it's not all a loss.

Link to comment
Share on other sites

OK. So you recommend I take the time to code this using a mysql database, so I'll check out some tutorials.

 

Thanks.

 

However, can anyone tell if what I was trying to do would work if I could get the coding right? I don't know why it won't write the post data along with string/array data into the file...

 

anyway, thanks for the help.

Link to comment
Share on other sites

Sorry to be annoying but:

 

I get almost all of my code from tutorials and some here and there from forums.

 

I'm new to coding

 

If your new to coding and you piece your work together then:

 

1) You will always come here for help because you don't know how your scripts work

2) You will always piece things together and never learn

Link to comment
Share on other sites

Mysql is extremely simple, it was the first thing I really learned how to do in PHP, and its easy to order things how you want them:

 

lets say I have a list in a mysql table called...list_table, and two rows inside called id and list:

 

[ id ]     [ list ]

1          php rules

2          I'm cool

3          apples are yummy

 

A very dumb list, but a list nonetheless, to pull this out of the database, you would have to first connect to the database, which I will name db_test:

 

$link = mysql_connect("mysql hostname", "username", "password") or die(mysql_error());
mysql_select_db("db_test") or die(mysql_error());

 

Ok, you're connected.  Now you need to pull out the data from the table called list, do this by executing a mysql query:

 

$data = mysql_query('SELECT * FROM `list_table`');

 

This stores all the information in a resource identifier called $data, in ascending order (biggest bottom, smallest top), to get it in descending order, (smallest bottom, biggest top) do this:

 

$data = mysql_query('SELECT * FROM `list_table` ORDER BY `id` DESC');

 

Now to print it out, do this:

 

while($info = mysql_fetch_array($data)) {
  echo $info['list']."<br />";
  }

 

If you used the ascending order one, you will get this output:

 

php rules

I'm cool

apples are yummy

 

if you used the descending order one, you will get this:

 

apples are yummy

I'm cool

php rules

 

I hope this helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.