Jump to content
xisto Community
Sign in to follow this  
matak

Help Php: How To Load String From Text File (solved) Loading string from text file when you click on your link

Recommended Posts

I learned the way to load other files with the code posted on this forum.

Now i wanted to try something for my side menu.

 

I am calling this a string, --> $tekst , maybe it's called something else i'm not sure

 

Now let's say i have a file called details.txt

 

In that file i would like to have something like this

 

$detailsaboutphp1 = ("details details details 1");

$detailsaboutphp2 = ("details details details and even more details 2");

 

How to make a code that loads those $strings on click of a mouse.

 

[REMINDER:]

When we tried to load external file we used this:

 

<?phpif ( !isset($_REQUEST['content']) ) {	$content = "home";	$menulhome="menulhome";	$menurhome="menurhome";}else { $content = $_REQUEST['content']; }?>

Than we included other files with a href

 

<a href="index.php?content=details">Details</a>

For loading details.xxx in place where we placed this code

 

<div><?php include("$content.xxx");?></div>
[/END OF REMINDER]

 

All i need now is a way for that Details to load other $string that is defined in text file..

Thank you

Edited by matak (see edit history)

Share this post


Link to post
Share on other sites

First and foremost, always sanitize user-provided data. Search this forum for many a thread in which it has been discussed.Anyway, including the file containing that code should assign it to the respective variables. Included files are evaluated within the same scope as from where the include() function was called, meaning that you should have access to the variables immediately after including them. Functions, classes, and global variables are, of course, declared on a global scope no matter where they come from.

Share this post


Link to post
Share on other sites

So basicly you wanna say that if i included my details.txt anywhere in page with this

<?php include("details.txt");?>
which contains string $detailsabout, and $detailsabout1 that i can call them separatly with <a href>

Does that mean that all of this time i've been doing it wrong?! :P

I thought when file was included that it shows just the files name, i didn't know that that file can contain variables that can be shown separatly on click..

well, this makes more sense now. thanks, i'm going to try something and if it works i'm just gonna be massively happy!
Thanks

First and foremost, always sanitize user-provided data. Search this forum for many a thread in which it has been discussed.

Sorry for not searching, i just don't know how to search for this question, beacouse something like this is hard to explain and even harder to ask at second hand, i probably read this answer million times before but it makes sense only when you get to the point when it can make sense. I hope you understand and don't mind for asking something that's been asked for houndered time...

Share this post


Link to post
Share on other sites

If you want to use external file to store your variables that is in PHP format, like your example, I found the following methods to be the best use.

 

1) Store them as PHP format when using INCLUDE

when you save an external file, insert <?PHP and end with ?> such that

<?PHP $var1 = "string1";$var2 = "string2";?>
This will simplify your include "filename"; within your PHP script. As far as I can tell, if you insert PHP header you only have to say INCLUDE command. If you do not insert PHP header, then you have to open the file, read line by line and address them as per variable. Either way it will work. I just found that when saving external file with PHP variables, it's easier to recall when I save it as above sample.

 

2) If you're not going to save external file with PHP header use REQUIRE

require is used when you have external files that contains functions, let's say, and you want to seperate many functions from all other script codes.

 

Just for fun, if you save HTML codes as text file format and use INCLUDE command, it will behave just like the regular HTML command. I have been using this method for GFXTrap affiliation insert.

Share this post


Link to post
Share on other sites

And further to what BH has inserted into this topic, "included" files are always treated as HTML files, BUT, if you save the file as a php extension, the contents will be more secure than a regular text file, due to the fact that with the php extensin, the file would be parsed through the php parser if someone tries to link directly to it. This also works to "hide" your file contents of "included" files. Some coders double up their extensions as thusly: "filename.inc.php" : the 'inc' tells them they are Includes and the 'php' causes them to be secured as above.

Share this post


Link to post
Share on other sites

Interesting on that little security feature, in which case most likely that is used for login, store,admin scripts for added security.

I did find an additional way, a bit more complex but does the same thing.

$current_url = $_SERVER['PHP_SELF'];echo "<a href="$current_url">http://totaldream.org; .$current_url. "</a>";

With this if I look at this right when you click on the link that uses this set up the server will load regardless of where it is located, you click the link and it will bring you there. so in a way you could echo your whole nav system like that.

Share this post


Link to post
Share on other sites

OT Question, problem is solved before this post

Thanks for those security tips, i was just about to ask that question but you beat me with answer.. Oh well few credits less :P

So what is the proper way to arrange files so that you don't get into trouble and conflicts later. Is it the same to put all of the content in one content.inc.php file, or is it better to make separate file for each categorie you have on site

[EXAMPLE:]

If i have News, which display in my index, and than maybe Stories which display on href=stories. Is it better way to store variables in same file, beacouse some of them are going to be displayed on both links so that i don't need to copy/paste them in two separate files.

[/EXAMPLE]

 

I feel that I'm getting a bit closer to understanding the proper way to build small custom CMS system, so i wouldn't want to go in wrong direction with directory and file structure. And good tut's about that, or do you arrange structures based on your feeling (like after Format C: :P )

Edited by matak (see edit history)

Share this post


Link to post
Share on other sites

I would use a structured database system for a CMS rather than flatfiles, such as MySQL (which is available via Xisto). In my opinion, flatfiles are useful for storing data where a database or even just a single table in a database is a bit of 'overkill' - in that the data you are storing is so small or insignificant that it isn't worth interfacing with a database engine (which is remarkably easy with MySQL in PHP anyway). Otherwise, I would recommend a database better suited for dealing with larger amounts of dynamic data.

Share this post


Link to post
Share on other sites

What makes PHP fun is that you can have many includes as you want one file, just have to make sure they are coded correctly.my website uses a combination of mysql tables and flatfiles for different stuff although I had made a booboo on moving some files and messed everything up.Examples would for flat files would be counters, logging IP'sMySQL forums,, guestbooks, mail, or other large applications (see fantastico for those examples)What makes PHP fun is that you can have many includes as you want one file, just have to make sure they are coded correctly.my website uses a combination of mysql tables and flatfiles for different stuff although I had made a booboo on moving some files and messed everything up (not worried havn't done much with it anyways).Examples would for flat files would be counters, logging IP'sMySQL forums,, guestbooks, mail, or other large applications (see fantastico for those examples)

Share this post


Link to post
Share on other sites

What makes PHP fun is that you can have many includes as you want one file, just have to make sure they are coded correctly.

Yes, I know!! I just figured it out yesterday. I knew that it is possible, but i didn't realize it would be this easy. Of course, for now i'm just going to stick by echo-ing html code when clicking hyperlinks, but now i understand the real power of PHP (or any other program). I just didn't know that it works that way, i always thought that i would need to make some powerfull function to do such a thing. With understanding of this, really simple thing i can get to programing smart and fun functions by the way. I'm not saying that is easier, just it's more fun.

 

My worst problem is that i'm messy when saving and testing files, and i lose code all the time. You always have that feeling that it could be done better, more neat and simple. Not always is that true but i guess now that i learned this basic principle that i am going to be more neat when arranging files. Currently I'm developing template Made in pure CSS without IE Hacks, simple but functional, it's almost done. I even made small tut about it so i'll post it. Although it changed a bit from start, beacouse in half of making that tut i learned great thing about CSS so It's really simple but functional code.

 

Also some of you mention database, yes it is great thing but i just don't feel i'm ready for storing data into tables 'couse i haven't learned that much yet. After almost 5 months wrestling with HTML, CSS, and some PHP finally i learned something worth mentioning. I just hope that rest of the stuff isn't going to be that hard, and that this great support on Xisto is going to be that great if not even better if that is even possible.

Have fun coding, i know i am!!

 

:P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.