Inserting a 'Random Text PHP Script

Gringoflash / 2008-12-22 17:08:30   

Hello wonderful Indexhibit community,

I'm fondling my way around my first MAMP-testversion of indexhibit, and I'm loving it so far. I'm quite good with basic css and html, but completely inexperienced when it comes to php.

What I'm trying to do is include a random sentence in the menu after the website (or exhibit) name.
(i.e. 'Gringoflash is working hard', 'Gringoflash doesn't know here his head's at' etc)

Googling around i came across the following script.I can get it to work in a standalone index.php file in a seperate folder, but I'm at a loss as to where to insert it inside indexhibit.

I put the two files ('random.txt' and 'GetRandomText.php' inside the ndxz-studio/site/sample folder) and as far the script:

<?php include_once"GetRandomText.php" $MPTextFile  "random.txt" $MPSepString  "*divider*" $MPTextToHTML  false MPPrintRandomText$MPTextFile $MPSepString $MPTextToHTML ?>

I tried putting it right after the body tag (inside a div) in the ndxz-studio/site/sample/index.php file, but to no avail.

Once again, my php-knowledge is close to none (as in copy-pasting stuff found on the net) so if I'm missing some very obvious stuff here, i apologize.

Greetings and thanks in advance,
Sander

Vaska A / 2008-12-22 21:30:55   

There is a tutorial about writing plugins. You might also need to review some PHP fundamentals on this one.

Gringoflash / 2008-12-22 21:33:56   

so basically, it's better to write a plugin in order to incorporate php, or is just the only possible way?

Either way, thank you i will look into it!

Sander

Vaska A / 2008-12-22 21:37:59   

It's the only way. A plugin is merely PHP. The template parser does not parse PHP directly in the templates for security reasons.

Gringoflash / 2008-12-22 22:54:23   
Ok thanks to your help I've gotten this far: i made a plugin called "plugin.random_text.php", called it inside my 'main' page like -> and previewed it.

But nothing shows up. The thing is, it's actually working, because when i check my source code, the random line shows up at the very beginning of the file, even before the doctype declaration. My question now is: how do i format this floating piece of php?

I won't be offended at all when the answer is: "Get lost and go learn some php first" because i get the feeling i'll be bumping into a lot more stuff like this on the way. But truth be told I just need this one thing to work and then it's back to jolly old css for me.

Thanks in advance once again!

PS I wish i could show you the live website but i don't have php-enabled hosting yet.

Vaska A / 2008-12-22 22:55:58   

Use "return ..." to get the data out of the function.

Use code tags so you can post code here...show us what's up.

Gringoflash / 2008-12-22 23:15:11   

Ok I'll try to be as thorough as possible:

The set-up is something like this: the plugin.random_text.php is located in the plugin folder, inside is another folder called 'randomtextfiles', where 'random.txt' and 'GetRandomtext.php' are.

"plugin.random_text.php" looks like this:

  1. include_once"ndxz-studio/site/plugin/randomtextfiles/GetRandomText.php"
  2. $MPTextFile  "ndxz-studio/site/plugin/randomtextfiles/random.txt"
  3. $MPSepString  "*divider*"
  4. $MPTextToHTML  false
  5. MPPrintRandomText$MPTextFile $MPSepString $MPTextToHTML

"GetRandomtext.php" (which i took from this website, looks like this:

  1. class MPRandomText
  2.     {
  3. ¬†¬†¬†¬†var $filepath;
  4. ¬†¬†¬†¬†var $sepstring;
  5. ¬†¬†¬†¬†var $fixchar;
  6. ¬†¬†¬†¬†var $textfix;
  7. ¬†¬†¬†¬†var $contents;
  8. ¬†¬†¬†¬†var $random;
  9. ¬†¬†¬†¬†var $errors;
  10. ¬†¬†¬†¬†// initiate object and start functions
  11. ¬†¬†¬†¬†function MPRandomText($filepath, $sepstring, $textfix)
  12.         {
  13. ¬†¬†¬†¬†¬†¬†¬†¬†$this->filepath = $filepath;
  14. ¬†¬†¬†¬†¬†¬†¬†¬†$this->textfix = $textfix;
  15. ¬†¬†¬†¬†¬†¬†¬†¬†$this->sepstring = $sepstring;
  16. ¬†¬†¬†¬†¬†¬†¬†¬†$this->errors = "";
  17. ¬†¬†¬†¬†¬†¬†¬†¬†$this->contents = "";
  18.         $this->FileToString();
  19.         }
  20. ¬†¬†¬†¬†// read file contents into string variable
  21. ¬†¬†¬†¬†function FileToString()
  22.         {
  23. ¬†¬†¬†¬†¬†¬†¬†¬†if (!$this->filepath || !file_exists($this->filepath))
  24. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†$this->errors = "Could not find text file at ".$this->filepath;
  25. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†else {
  26. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†@$filePointer = fopen($this->filepath, "r");
  27. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†if (!$filePointer) $this->errors = "Text file could not be opened.";
  28. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†if ($this->errors == "")
  29.                 {
  30. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†$this->contents = fread($filePointer, filesize($this->filepath));
  31.                 fclose($filePointer);
  32. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†if (!$this->textfix) $this->HTMLContent();
  33.                 $this->MakeArray();
  34.                 }
  35.             }
  36.         }
  37. ¬†¬†¬†¬†// if importing an HTML page, drop everything outside of (and including) the  tags
  38. ¬†¬†¬†¬†function HTMLContent()
  39.         {
  40. ¬†¬†¬†¬†¬†¬†¬†¬†$test = stristr($this->contents, "");
  41. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†if ($test !== false)
  42.                 {
  43. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†$test = str_replace("", "", $test);
  44. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†$test = explode("", substr($test, 1));
  45. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†if (count($test) > 1) $this->contents = $test[0];
  46.                 }
  47.             }
  48.         }
  49. ¬†¬†¬†¬†// convert the file text into a list using separation character
  50. ¬†¬†¬†¬†function MakeArray()
  51.         {
  52. ¬†¬†¬†¬†¬†¬†¬†¬†$array = explode($this->sepstring, $this->contents);
  53. ¬†¬†¬†¬†¬†¬†¬†¬†if (count($array) > 0)
  54.             {
  55. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†$this->contents = $array;
  56.             $this->CleanTextList();
  57. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†} else $this->errors = "Text file contents are empty or could not be read.";
  58.         }
  59. ¬†¬†¬†¬†// clean up the list of empty values and extra white space
  60. ¬†¬†¬†¬†function CleanTextList()
  61.         {
  62. ¬†¬†¬†¬†¬†¬†¬†¬†$result = array();
  63. ¬†¬†¬†¬†¬†¬†¬†¬†if (is_array($this->contents))
  64.             {
  65. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†for ($n=0; $ncontents); $n++)
  66.                 {
  67. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†$string = trim($this->contents[$n]);
  68. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†$test = trim($string."test");
  69. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†if (!empty($string) AND $test != "test") $result[] = $string;
  70.                 }
  71. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†if (count($result) > 0)
  72.                 {
  73. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†$this->contents = $result;
  74.                 $this->RandomString();
  75.                 }
  76.             }
  77.         }
  78. ¬†¬†¬†¬†// get random text string from list
  79. ¬†¬†¬†¬†function RandomString()
  80.         {
  81.         reset($this->contents);
  82. ¬†¬†¬†¬†¬†¬†¬†¬†srand((double) microtime() * 1000000);
  83.         shuffle($this->contents);
  84. ¬†¬†¬†¬†¬†¬†¬†¬†$this->random = $this->contents[0];
  85.         }
  86. ¬†¬†¬†¬†// send finished results to be printed into HTML page
  87. ¬†¬†¬†¬†function GetResults()
  88.         {
  89. ¬†¬†¬†¬†¬†¬†¬†¬†if ($this->errors != "") return $this->errors;
  90.             else
  91.             {
  92. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†if ($this->textfix == true) $this->random = htmlentities($this->random);
  93. ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†return $this->random;
  94.             }
  95.         }
  96.     }
  97. //-------------------------------------------------------------------------------------------------
  98. //  FUNCTION AND VARIABLE TO CREATE RANDOM TEXT INSTANCE
  99. //-------------------------------------------------------------------------------------------------
  100. // create variable to store instance references
  101. $MPRandomTextHandles = array();
  102. // function to create new handle and import random text
  103. function MPPrintRandomText($MPTextFile = "random.txt", $MPSepString = "*divider*", $MPTextToHTML = false)
  104.     {
  105. ¬†¬†¬†¬†global $MPRandomTextHandles;
  106. ¬†¬†¬†¬†for ($n=0; $nGetResults());
  107. ¬†¬†¬†¬†return $MPRandomTextHandles[$n];
  108.     }

Any help is very much appreciated, I'm baffled at the quick responses anyhow. Makes me feel like a nagging kid :)

Greetings from Belgium,

Sander

Gringoflash / 2008-12-22 23:16:09   

the code tags apparently got mixed up, i hope it's still clear as it is.

Vaska A / 2008-12-22 23:23:47   

We're in Brussels...where are you at?

You have already created the plugin file...add the code above to the file directly. Then, create a new function like this for calling things up...

  1. function random_text()
  2. {
  3. $MPTextFile¬†=¬†$_SERVER['DOCUMENT_ROOT'] . '/ndxz-studio/site/plugin/randomtextfiles/random.txt';
  4. $MPSepString = "*divider*";
  5. $MPTextToHTML = false;
  6. return MPPrintRandomText($MPTextFile, $MPSepString, $MPTextToHTML);
  7. }

I'm going super fast here...see where this gets you.

Looking back on things...it appears there are things missing...code not shown...

Gringoflash / 2008-12-22 23:36:19   

I'm in Antwerp up north :) Greetings fellow no-government-at-the-moment-er.

About the code: a bit got cut off in the process of posting, don't know how that happened. HERE is the script as i found it on the mindpalette site (zip file).

Vaska A / 2008-12-22 23:49:31   

Oi vey...will they ever learn how to work together?

Ok, I looked at the full file...just take the full contents of that file and add it to the plugin file you are creating...and of course add this too...

  1. function random_text()
  2. {
  3. $MPTextFile = $_SERVER['DOCUMENT_ROOT'] . '/ndxz-studio/site/plugin/randomtextfiles/random.txt';
  4. return MPPrintRandomText($MPTextFile, "*divider*", false);
  5. }

I'm heading off to sleep...I'll check in the morning...tell us how it goes.

Gringoflash / 2008-12-23 00:03:04   

Ok I've found the means to put it live on the interwebs, here is the address:

Gringoflash random text TEST

as you can see the random text shows up at the complete top of the source code.

Here's the plugin:
http://www.scoutshove.be/gringoflash/ndxz-studio/site/plugin/plugin.random_text.php

Here's the text file:
http://www.scoutshove.be/gringoflash/ndxz-studio/site/plugin/randomtextfiles/random.txt

Here's the php source file:
http://www.scoutshove.be/gringoflash/ndxz-studio/site/plugin/randomtextfiles/GetRandomText.php

I tried putting all the scripts in one file and using the function like you said but I'm afraid that that was some 'way over my head sh*t' (excuse the language)

Gringoflash / 2008-12-23 00:03:07   

Ok I've found the means to put it live on the interwebs, here is the address:

Gringoflash random text TEST

as you can see the random text shows up at the complete top of the source code.

Here's the plugin:
http://www.scoutshove.be/gringoflash/ndxz-studio/site/plugin/plugin.random_text.php

Here's the text file:
http://www.scoutshove.be/gringoflash/ndxz-studio/site/plugin/randomtextfiles/random.txt

Here's the php source file:
http://www.scoutshove.be/gringoflash/ndxz-studio/site/plugin/randomtextfiles/GetRandomText.php

I tried putting all the scripts in one file and using the function like you said but I'm afraid that that was some 'way over my head sh*t' (exuse the language)

Gringoflash / 2008-12-23 00:04:43   
I put the right below the pre-nav text by the way.
Gringoflash / 2008-12-23 00:06:23   

RETRY:
I put the plugin code right below the pre-nav text by the way.
Excuse the posting cacaphony, the internet is acting up apparently.

Eloisa A / 2008-12-23 09:49:04   

it's rebelling against the non government!

arsondpi / 2008-12-23 11:33:03   

revolt ;-)

Gringoflash / 2008-12-23 11:46:44   

Haha, you're probably in the right position to say that. I just returned from Greece, it made the Belgian problem seem entirely obsolete.

But anyway, if anyone has any suggestions, spit them out!

good day!

Vaska A / 2008-12-23 11:55:39   

I said put the functions into the script...not the random text file as well.

Those links to put up there...we can't see the code...turn them into txt files if we are to see the code...

It's very hard doing this...I see nothing...

Vaska A / 2008-12-23 12:01:23   

Here is my own version of the same plugin...from scratch...

  1. function random_text()
  2. {
  3. $texts = array(
  4. "First string",
  5. "Second string",
  6. "Third"
  7. );
  8. $total = count($texts); // total texts
  9. if ($total < 0) return; // if no texts
  10. $text¬†=¬†rand(0,¬†$total - 1);¬†//¬†random¬†element
  11. return $texts[$text];
  12. }

Just add the strings you want...as many as you want...

Gringoflash / 2008-12-23 15:41:55   

Thank you very very very much!
I'll keep you guys informed of the site progress as i move along.

Once again, praise to the creators and thanks to Vaska in particular for helping me out.

Result

This thread has been closed, thank you.