0

First time here so apologies if I'm doing something wrong.

I have the following php code:

<?php

 $quoteFile = "quotes.txt";  //File holding qoutes

 $fp = fopen($quoteFile, "r");   //Opens file for read
 $content = fread($fp, filesize($quoteFile));
 $quotes = explode("\n",$content);   //Put quotes into array
 fclose($fp);   //Close the file

 srand((double)microtime()*1000000);  // randomize
 $index = (rand(1, sizeof($quotes)) - 1); //Pick random qoute

 ?>

The code fetches a random quote from a text file by randomly choosing one of the lines of the .txt file.

I then echo out the result using:

echo $quotes[$index];

However what I want to achieve and don't seem to be able to is to have a button (html) that when clicked executes the

echo $quotes[$index];
to the current page. So that each time the button is clicked it prints/echo's out a random quote from the .text file.

I did mess about with just setting a button up to refresh the page which by default made a new random quote display but it sometimes just reloaded a blank so I'm hoping someone can help me achieve this better or prompt me in the right direction. Thank tou.

Debuggler
  • 3
  • 3
  • I believe you may be confused about server-side vs client-side languages. By the time a user clicks a button, your PHP code has already run. So, if you want to use your PHP function, it must be on a separate page than the button. When the button is clicked, use an AJAX call to the page with the PHP function. – devlin carnate Jul 27 '16 at 18:03
  • You will need that button to use ajax to call a php file to echo that item. – Spencer Wieczorek Jul 27 '16 at 18:03
  • Off topic: See http://stackoverflow.com/a/12119002 for an easier way to get a random line from file – u-nik Jul 27 '16 at 18:08
  • 1
    Possible duplicate of [How to Call a PHP Function on the Click of a Button](http://stackoverflow.com/questions/20738329/how-to-call-a-php-function-on-the-click-of-a-button) – devlin carnate Jul 27 '16 at 18:13

2 Answers2

0

You can try saving that variable into a session variable like this:

$_SESSION['quote'] = $quote['index'];

Then create an anchor element that redirects to current page:

<a href="current_page.php">Refresh</a>

And print the result on the page:

<span><?php echo $_SESSION['quote']; ?></span>

To do all of this, you need to set a session. At top of your php file write:

session_start();

Hope that helps. :)

Petyor
  • 389
  • 3
  • 12
-2

Your TXT file might have an empty line in it at the end or anywhere else. A second explanation of this, is that the way you are generating randomness is quite questionable.

Check out this simple example by W3 Schools.

$a=array("red","green","blue","yellow","brown");
$random_keys=array_rand($a,1);
echo $a[$random_keys[0]]."<br>";

The array_rand() function returns a random key from an array, or it returns an array of random keys if you specify that the function should return more than one key.

Or, simply:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
print_r(array_rand($a,1));
?>

Full Post: http://www.w3schools.com/php/func_array_rand.asp

Happy coding !

Wahib Zakraoui
  • 1,269
  • 1
  • 8
  • 4