• 0

Learning PHP, so lost


Question

Hey all I just read most of the PHP tutorials on w3schools and I still have no idea how to use this or anything. What else should I read? Right now I want to do something which I assume is simple. Have a page that has a text box and when I type something in the text box and click a button, the text appears on some other page.

EDIT- Hopefully improved problem

I wanna do a kind of a blog thing. Where I put in the text into the form, ex-Hey all new beta coming soon!. And on the homepage it will popup for everyone, always. Then if I go back to that form and type ex-Whoops I lied. Then this new text will appear above the old one for everyone, always.

Link to comment
Share on other sites

21 answers to this question

Recommended Posts

  • 0

I started out at w3schools too, and it worked out very well for me. I had to read through them a few times though.

Anyway, what you're trying to do is very easy.

file1.php

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

<form action="file2.php" method="GET">
<input type="text" name="inputfield" />
<input type="submit" value="click here" />
</form>

</body>
</html>

file2.php

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

<?php

echo $_GET['inputfield'];

?>

</body>
</html>

I did this all in my head, but I'm pretty sure it does what you ask. :)

Link to comment
Share on other sites

  • 0

I find the best place to go is the official PHP manual:

http://php.net/manual/en/index.php

What you want to do it pretty simple, keeping it as simple as possible:

File1.php

<form action="file2.php" method="get">
<input type="text" name="txt" />
<input type="submit" value="Send" />
</form>

File2.php

<?php
echo $_GET['txt'];
?>

Link to comment
Share on other sites

  • 0

Thanks for the help. But I think I should rephrase my goal. I wanna do a kind of a blog thing. Where I put in the text into the form, ex-Hey all new beta coming soon!. And on the homepage it will popup for everyone, always. Then if I go back to that form and type ex-Whoops I lied. Then this new text will appear above the old one for everyone, always.

Link to comment
Share on other sites

  • 0

Well you're going to need some form of storage then, i.e. an xml file, or more likely, a database!

Well darn! PHP can't edit the html file? I was thinking PHP File, but I can't figure out how, if it could work.

Link to comment
Share on other sites

  • 0

Well darn! PHP can't edit the html file? I was thinking PHP File, but I can't figure out how, if it could work.

PHP can too edit a file using the Filesystem Functions but you don't want that. Databases are infinitely faster, more secure and easier to manage. It may seem overwhelming at first but do yourself a huge favor and learn some basic PHP and MySQL interaction before you go any further.

Link to comment
Share on other sites

  • 0

PHP can too edit a file using the Filesystem Functions but you don't want that. Databases are infinitely faster, more secure and easier to manage. It may seem overwhelming at first but do yourself a huge favor and learn some basic PHP and MySQL interaction before you go any further.

+1

You're in for a treat thatguyandrew1992. This is not something you can do in 5 minutes. Even if you know PHP and MySQL very well.

Link to comment
Share on other sites

  • 0

Hey, I did a teach-myself on PHP and MySQL. It isn't too difficult, but you might want to get a book from a bookstore for reference, and with some instructional content.

There are a lot of helpful people here on Neowin that will help. And, yes, going with a database is what you will want, long-term. Store your blog entries, and comments/replies in the database, and let your single display php page fetch the appropriate blog entry and comments for you. You really don't want to muck with having PHP modify individual blog files. Yikes! :omg:

Link to comment
Share on other sites

  • 0

I don't see why so many people are suggesting a database. You could do what you want by simply appending to the front of a text file.

Since you're are just starting out with PHP, SQL might be biting off more than you can chew. Though, I would suggest trying your hand at SQL after your more comfortable with PHP.

Link to comment
Share on other sites

  • 0

I don't see why so many people are suggesting a database.

Because databases are actually quite easy to implement, and are quite well-suited to a blog where others can post comments.

Link to comment
Share on other sites

  • 0

I don't see why so many people are suggesting a database. You could do what you want by simply appending to the front of a text file.

Since you're are just starting out with PHP, SQL might be biting off more than you can chew. Though, I would suggest trying your hand at SQL after your more comfortable with PHP.

Could you elaborate about "appending?" I would like to try that too.

Link to comment
Share on other sites

  • 0

Because databases are actually quite easy to implement, and are quite well-suited to a blog where others can post comments.

He didn't mention having others comment, or keeping track of who said what. Correct me if I?m wrong, but I get the impression that this is more of a learning experience. IMO this is a simple problem and a database would add a level of complexity that doesn't need to be there, especially for a beginner.

However, if you are looking to make this into a full blown blog or messaging website, a database would be more appropriate.

Could you elaborate about "appending?" I would like to try that too.

Something like:

$File = "File.txt";
$Data = $_REQUEST["message"];
$Prev = file_get_contents($File);


$Handle = fopen($File, 'w');
fwrite($Handle, $Data);
fwrite($Handle, "<br>");
fwrite($Handle, $Prev);
print "Data Written";
fclose($Handle);

Link to comment
Share on other sites

  • 0
He didn't mention having others comment, or keeping track of who said what. Correct me if I'm wrong, but I get the impression that this is more of a learning experience. IMO this is a simple problem and a database would add a level of complexity that doesn't need to be there, especially for a beginner.

Mate, that is precisely why the OP should spend his time trying practical and commonplace techniques such as MySQL databases. Every CMS, blog, forum and shopping cart out there will have him faced with a relational database. Modifying document text, on the other hand, is something he will neither ever encounter nor ever need at a beginner level.

And you don't need to go as far as a comment system to see how impractical flat file storage is. What if he wants to edit or delete a post? "UPDATE `post_table` SET `post_body` = '$new_content' WHERE `post_id` = $primary_key" in a MySQL implementation, or a huge jumble of string search and replace code with a text document.

Link to comment
Share on other sites

  • 0

Mate, that is precisely why the OP should spend his time trying practical and commonplace techniques such as MySQL databases. Every CMS, blog, forum and shopping cart out there will have him faced with a relational database. Modifying document text, on the other hand, is something he will neither ever encounter nor ever need at a beginner level.

And you don't need to go as far as a comment system to see how impractical flat file storage is. What if he wants to edit or delete a post? "UPDATE `post_table` SET `post_body` = '$new_content' WHERE `post_id` = $primary_key" in a MySQL implementation, or a huge jumble of string search and replace code with a text document.

I disagree. One of the major challenges for database managers should be to decide if a database is even needed. I think for a simple system to posts messages above previous messages, it's not needed. I would even argue that, unless you wanted to add features like management of posts, it would be counter productive to use a database.

My suggestion to OP is to finish what he's currently working on with PHP and file manipulation. Then, if he feels comfortable with PHP move on to making a more featured project using a database.

Link to comment
Share on other sites

  • 0

I disagree. One of the major challenges for database managers should be to decide if a database is even needed. I think for a simple system to posts messages above previous messages, it's not needed. I would even argue that, unless you wanted to add features like management of posts, it would be counter productive to use a database.

My suggestion to OP is to finish what he's currently working on with PHP and file manipulation. Then, if he feels comfortable with PHP move on to making a more featured project using a database.

No offense, mate, but you are simply biting your nose to spite your face harder and harder. First you argue that a database is not needed because he didn't mention a commenting system, now you are arguing a database is not needed because he didn't say he wants the ability to correct a typo in a post? I hope you are not in our trade professionally with that kind of outlook on scalability. Yes, you could build a house with straw for bricks and manure for mortar, but why would you?

Link to comment
Share on other sites

  • 0

A database would be way more flexible compared to text files. Storing data in text files was my first idea when I wanted a custom blog. It was way more complication. With databases, its easier to display, add, edit and delete data.

To store data using text files, you'll need a hell lot of functions but using databases(eg. Mysql), its as easy as this.

$sql = "INSERT INTO posts (title, data) VALUES ('".stripslashes($_POST['title'])."', '".stripslashes($_POST['data'])."')";
$result = mysql_query($sql);
if($result) { echo 'Well Done!'; } else { echo 'Something went wrong :('; }

Anyway, good luck. Just remember, take it easy and never give up. Just keep trying and trying over and over again until you get it right. It may sound hard but after a while, you'll be wondering "Heck, this was a lot more easier than I thought.".

Link to comment
Share on other sites

  • 0

Well I'm interested in learning everything so I will do both just so I know how to do both! :D

Good luck (Y) . Let us know if you run into any trouble.

No offense, mate, but you are simply biting your nose to spite your face harder and harder. First you argue that a database is not needed because he didn't mention a commenting system, now you are arguing a database is not needed because he didn't say he wants the ability to correct a typo in a post? I hope you are not in our trade professionally with that kind of outlook on scalability. Yes, you could build a house with straw for bricks and manure for mortar, but why would you?

With the intent to of a simple website intended for learning, scalability isn't important. You're not taking into account the intended use of this site and blindly recommending a database. Databases have their advantages and, for most applications, I would agree, are the way to go. But for this situation, you're not going to beat simple file manipulation for easy of set up and use.

Why build a building out of reinforced titanium when you could build it out of bricks and mortar?

Link to comment
Share on other sites

  • 0

I have posted this about three dozen times, because it's so easy and a very very good tutorial. It's how I learned (took me a weekend to learn PHP) and a week to learn MySQL. I am still learning new things every time I code, and php.net might be confusing and not helpful right now, but once you start looking for methods on how to do stuff, php.net is the best resource to use :D

http://www.tizag.com/phpT/

here is there MySql tutorial when you are ready:

http://www.tizag.com/mysqlTutorial/

Link to comment
Share on other sites

  • 0

Database driven is the way to go. It's much more flexible and theres a lot more people who use this method so if you run into a problem its a lot easier to figure out. Using a filesystem method also presents a lot more confusion. It will take some time especially if your just learning. I would suggest learning more about PHP and how to use it before dipping into SQL though.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.