Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Author Topic: PHP > Read from PHP or TXT  (Read 7075 times)

Tkaspers

  • Newbie
  • *
  • Karma: 0
  • Posts: 30
PHP > Read from PHP or TXT
« on: June 05, 2006, 02:15:31 pm »

I am creating a website for a game.. and it should have a creature encyclopedia.. the game has 130 creatures, so I want others to be able to help me and it should be easy to enter all the information..

Now I was thinking if the following is possible..
-------------------------------------------------------------------
A creature page will have several things.. like:

------------
Demon

"a demon is blahblahblah etc.. description"

Experience: 6000
Hitpoints: 8000
Attacks: Ranged Combat / Close Combat
Specials: Summons Fire Elementals

Loot: Fire Axe, Demon Armor and Golden Legs

Yells: I SMELLL FEAARRRR, GRRROOARRR, LET ME PUNISH THE SINNERS..
--------------

So in my layout page.. on the name/description/experience/hitpoints/attacks etc.. spot.. I want to add something like ;[name] (no idea if its correct.. just an example)

and then in a textfile or on the same php file only then at the top.. i want to have like:
$[name]; = "Demon"
$[description]; = "a demon is blah~"

etc... and that it will enter that on the correct spots..
This will save me time and allow other ppl with no php experience to enter those things while editing the page in a notepad.. then they send it to me and I upload it.


Even easier is if those things are loaded from a textfile (demon.txt).. then I include the textfile in the php file and the name/description etc.. gets readen from there..
So the other guys just edit the textfile and give it to me and I upload it..


Hope you guys understand what I mean..
So.. is this possible? if yes.. how can I do this easily.. my PHP knowledge is real basic.


Greetings,
Thijs
Logged
~ No act of kindness, no matter how small, is ever wasted ~

Luka

  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 36
  • Posts: 1717
    • http://www.snewscms.com
PHP > Read from PHP or TXT
« Reply #1 on: June 05, 2006, 02:29:20 pm »

There's a function to include txt files, I don't know if you're farmiliar with it.

[include]myfile.txt[/include]

Now the real question is: Do you want your file to behave like a database to use some kind of fields?

An expirienced PHP coder could easily make a function for this :)
Logged

Tkaspers

  • Newbie
  • *
  • Karma: 0
  • Posts: 30
PHP > Read from PHP or TXT
« Reply #2 on: June 05, 2006, 02:38:06 pm »

It could be a database yes.. or textfiles.. its easier as textfiles so that others can just edit the file in notepad.. add the experience/attacks thingy.. and then send it back to me

So it has to read the content from the textfile.. and put it into the layout..

like the textfile =

Name: "..."
Description: "..."
Attack: "..."

It reads the name/description/attack values.. and then adds them into my .php layout file.. Im PHP-BASIC... so I hope this can be done without too much programming experience..

Thijs~


[edit] I think its a bit like the language part of sNews.. where the words are placed on the spots.. [/edit]
Logged
~ No act of kindness, no matter how small, is ever wasted ~

Tkaspers

  • Newbie
  • *
  • Karma: 0
  • Posts: 30
PHP > Read from PHP or TXT
« Reply #3 on: June 05, 2006, 05:02:36 pm »

http://img406.imageshack.us/img406/9695/amazon4oc.jpg

That is an example of the Amazon page.. but next to the Amazon, there are 130 more creatures.. and they all need a page like that..

So I wanted others to be able to help me, but since they dont have programs to edit the php with and dont have the knowledge, I was thinking to let them create text files..

Name= .....
Experience = .....

etc... and then they would give them to me and the php file would automatically load the info from there..

I thought this was easy to do :)...

Or at least load the info from on top of the same php file, so they dont have to browse all the code, but can just open it with notepad and enter the info at the top of the amazon.php page..


Greetings,
Thijs
Logged
~ No act of kindness, no matter how small, is ever wasted ~

Mika

  • Hero Member
  • *****
  • Karma: 9
  • Posts: 1377
    • http://www.ni5ni6.com/
PHP > Read from PHP or TXT
« Reply #4 on: June 06, 2006, 02:07:17 am »

buckle up, Tkaspers :D

here's some rules of engagement for your editors:

1. file name to include MUST have a prefix, like "daemon_your_text_here.txt" or something else (in my example prefix is rather obvious - "daemon_")
2. file type MUST be .txt

3. format your daemon_your_text_here.txt files as you see fit (your previous suggestion is quite ok)
************************************
and some rules for you: :D
1. MAKE A BACKUP FIRST

2. add this line to //global settings
Quote
//GLOBAL SETTINGS
...
$s['my_file_prefix'] = "daemon_"; //prefix for daemon files
...
3. find // PHP files inclusion routine in function left(), function center(), function right() (this is an example for function center()... other functions are 99% similar and i hope you'll have no truble modifying them)
Quote
// CENTER
function center($article_limit) {
....
// PHP files inclusion routine
$fulltext = $r['text'];
$findme  = "[include]";
$pos = strpos($fulltext, $findme);
$findme  = "[/include]";
$pos2 = strpos($fulltext, $findme);
$file = substr($fulltext, $pos + 9, $pos2 - 9);
if ($pos2 > 0) {
   $text = str_replace("[include]", "|&|", $fulltext);
   $text = str_replace("[/include]", "|&|", $text);
   $text = explode("|&|", $text);
   $num = count($text);
   for ($i = 0; ; $i++) {
      if ($i == $num) {
         break;
      }
      if (strpos($text[$i], '.php') === false AND strpos($text[$i], '.txt') === false AND strpos($text[$i], '.inc') === false) {
         echo substr(stripslashes($text[$i]), 0, $shorten);
      } else {
         include $text[$i];
   }}} else {
   echo substr(stripslashes($fulltext), 0, $shorten);
...
4. replace blue with red code (be extra carefull with surrounding parenthesis!!)
Quote
//---------- daemon file begin
   if (false !== (strpos($text[$i], s('my_file_prefix'), 0)) AND strpos($text[$i], '.txt'))
   {
      //basic html formatting
      echo '
';
      $file_array = file($text[$i]);
      for($j = 0; $j < count($file_array); $j++)
         {echo htmlspecialchars($file_array[$j]).'
';}
      //basic html formatting
      echo '
';
   }
//---------- daemon file end
   else
include $text[$i];
5. see this topic... i think that you'll find it helpful...
just tested on localhost (php 5) and found no problem... tell me how it works

EDIT: after a short and trubled sleep, i realised that i haven't fixed a serious security hole... it shoud be okay now, i hope

note: this chunk of code is asking for a second opinion from a more expirienced coder
Logged
http://www.ni5ni6.com/ - Tutorials, Mods and How-To's about sNews CMS
sNews 1.6 Developers Edition - commented sNews 1.6 version

Tkaspers

  • Newbie
  • *
  • Karma: 0
  • Posts: 30
PHP > Read from PHP or TXT
« Reply #5 on: June 06, 2006, 11:43:33 am »

Ive got it running now :)..      Works perfect.

Thank you for the help.. This kind of code wasn't really what I had in mind, but it works :P..


Kind Regards,
Thijs
Logged
~ No act of kindness, no matter how small, is ever wasted ~

Mika

  • Hero Member
  • *****
  • Karma: 9
  • Posts: 1377
    • http://www.ni5ni6.com/
PHP > Read from PHP or TXT
« Reply #6 on: June 06, 2006, 12:57:58 pm »

stand by for update and some security fixes...i'm going to post it later today

(i'm curious, what did you had in mind?)
Logged
http://www.ni5ni6.com/ - Tutorials, Mods and How-To's about sNews CMS
sNews 1.6 Developers Edition - commented sNews 1.6 version

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • I'm a self-made man and worships my creator.
    • p-ahlqvist.com
PHP > Read from PHP or TXT
« Reply #7 on: June 06, 2006, 01:02:38 pm »

Mika... Never leave this place :) You, Bram and analyzerx are really good at this and helpful. I thank you on behalf of all, hehe...

*Mr brown-nose, that's me ;)*
Logged
"It's only dead fish that goes with the flow... "
Updated

Tkaspers

  • Newbie
  • *
  • Karma: 0
  • Posts: 30
PHP > Read from PHP or TXT
« Reply #8 on: June 06, 2006, 01:14:41 pm »

Quote from: mika
stand by for update and some security fixes...i'm going to post it later today

(i'm curious, what did you had in mind?)
I had something more simple in mind :P..

Like:

Code: [Select]
if(file_exists($_GET['xd'].".inc"))
{
   include ($_GET['xd'].".inc");
}
else
{
       die('errormessage');
}
?>
So if you go to www.mysite.com/creatures.php?xd=demon .. then the variables gets readen from demon.inc..

like:
Code: [Select]
and then demon.inc contains:
Code: [Select]
$Name = "Demon";And then my friends just edit the premade inc file all the time for the other creatures and send it back.. but its less advanced..


Greetings,
Thijs~
Logged
~ No act of kindness, no matter how small, is ever wasted ~

Mika

  • Hero Member
  • *****
  • Karma: 9
  • Posts: 1377
    • http://www.ni5ni6.com/
PHP > Read from PHP or TXT
« Reply #9 on: June 06, 2006, 01:59:21 pm »

well, i've tried (and hopefully succeeded) to keep complete [include] capabilities (including php/txt/inc types of files directly to article), with an extra feature added especially for this purpose:

- including files with predetermined name prefixes and file types,

- editors without any scripting knowledge can produce content instantly, and

- disabling all of the