0

I am trying to add a PHP code to my website so every week the text on the website would update from a stored file like quotes.txt I have a PHP code someone gave to me but I need some help I'm not understanding how I would do the html and put this on my website here is the code.

$text = file("quotes.txt");       

$search = array ("\r\n", "\r");
$text = str_replace($search, "\n", $text);

$array = explode("\n", $text);

$line = date("z");

echo $array[$line];

please help!

Martin
  • 22,212
  • 11
  • 70
  • 132
KstreakOG
  • 57
  • 1
  • 9

2 Answers2

0

First than all you will need to check first if you hosting provider allows cron-tab jos, that is, to automatically run a script every x time. If it does then you are half the way. Otherwise the only way to run that script would be by launching it yourself, and might be that is not the idea!

milton cedeno
  • 58
  • 1
  • 4
0

your question as it stands is very broad, there's a lot of question I'd ask such as is there any HTML in the quotes.txt or is it just content?

What you seem to be implying is that you have a single line from a list of lines that is displayed based on a date formula? So Quotes.txt has 52 lines of text one for each day and then a line number is picked at random?

I want to go through your question one part at a time.

$text = file("quotes.txt");       

It would be better here to use file_get_contents because that's all you need, the contents not the metadata or filedata.

$search = array ("\r\n", "\r");
$text = str_replace($search, "\n", $text);
$array = explode("\n", $text);

This part is misleading as the semi-standardisation of line endings (to \n) is wasted in the explode statement. I would use PHP_EOL and ignore the string replace.

$line = date("z");

z is the day of the year, however your question is weeks, and that is W as the ISO-8601 week number (0 to 52) .

echo $array[$line];

This echo should be the part outputting to your browser, but this will simply output the content rather than wrap it in valid HTML, you'd have to do that your own way, basically you're just dumping data to the browser with no tags, or anything beyond the contents of that line in the array.

So a rewrite:

$text = file_get_contents("quotes.txt");  
$text = trim($text); //This removes blank lines so that your 
//explode doesn't get any empty values at the start or the end.     
$array = explode(PHP_EOL, $text);
$lineNumber = date('W');

What this will output is only the data in the array line number. You can wrap it in cursory HTML as so:

    print "<!DOCTYPE html>
            <html lang="en">
                    <head>
                     <title>My Page!</title>
                   </head>
           <body>
                <p>";
    echo $array[$lineNumber];
print "</p>
          </body>
     </html>";

If you have a standard HTML page already that you want to insert the data into then you can simply print or echo the value as you have done already and exampled above. You would possibly need to add tags such as <?php ... ?> (to open and close the PHP section) and you would also need to save the page as .php if it is a .html file.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • where would i put the php code and the html can you make it so the echo is surrounded by p tags ? – KstreakOG Feb 07 '16 at 17:39
  • try using a tutorial to learn about PHP, http://php.net/manual/en/tutorial.php there are such a wide range of options in answering your question it would be wrong to give you a single answer, as there are many, depending on your preferred method of coding. `print` and `echo` are very similar commands in PHP – Martin Feb 07 '16 at 18:31
  • Im getting an error when using this code $array[$lineNumber]"; ?> – KstreakOG Feb 08 '16 at 03:44
  • what does the error say? What version of PHP are you running? – Martin Feb 08 '16 at 08:33
  • so now its just displaying numbers randomly everytime, look at this https://gyazo.com/fba5fc414228b1ab2a79bb877642477a thats a gif of what happens everytime i reload the page. My php verson is 5.3.28 my code is here : http://sandbox.onlinephpfunctions.com/code/44148c3be83cd08dc1b0564491bfbbc09eb5173e – KstreakOG Feb 10 '16 at 22:50