Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Robert Walker
17,146 PointsDynamic pages from PHP results.
I have a small site that I am working on that compares two products using PHP and then outputs the differences of those two products.
During this process I store the PHP results into a text file using fopen, serialize, fwrite.
So I end up with a text file: 12345.txt now once that has been stored I want people to be able to then come back to my site and load that text file to then output those old results using a URL
http:www.examplesite.com/12345
I am totally stuck on how you would do that
4 Answers
Simon Sørensen
17,304 PointsDON'T bother using text files. Use MySQL. It's a bit harder, but cleans up your project. And may be more flexible in the future.
Andy Swinford
8,286 PointsI would go with Simon's suggestion personally. It is pretty easy to do if you use PDO or mysqli_connect.
If you are set on loading a text file, one option is to go with file_get_contents and file_put_contents.
<?php
$contents = file_get_contents("12345.txt");
echo $contents;
?>
If you want them to be able to modify this text file, you can load the file to a textbox value and once they save, you would use file_put_contents to save it again.
Robert Walker
17,146 PointsIts not really an option for me at the moment as it would require more work to do it through a database, the text files do the job and at the moment that is all that matters.
All I need to do currently is get it so people can enter the text file name in the URL and that text file is opened and the results output like it was originally.
Does anyone know a simple way to do that?
Simon Sørensen
17,304 PointsWell, I'm happy to write the script for ya'! If you need further explaining, just ask.
Robert Walker
17,146 Pointstl;dr
making dynamic pages from results
The options open to me and how I would go about these different options.
Sorry been away for a while and havnt had a chance to look over the forum posts.
I think what is happening is that I am not making my point clearly enough.
I have a compare page you input a number of things and it then sends that information through POST,
compare.php > sends to results.php
From here PHP does all the magic stuff of comparing the inputs and the array / arrays output the compared results but once you close this page all the results are deleted and if you ever wanted to check the same results again you would need to go back to compare.php enter all the same details which would then send over POST var to the results.php again.
What I want to do is make unique pages every time you compare so instead of http://www.examplesite.com/results you would get something like:
http://www.examplesite.com/results?=1 or (Which I would pref if possible) http://www.examplesite.com/results/1.
So basically making it so every result set is stored so that people can have links to those specific results, I am not using a database and thought maybe I could store the result array / arrays in a text file and then when people typed in
http://www.examplesite.com/results/245
The normal results.php would be called but because the URL has/245 at the end it would then open that file and load output the saved results.