-1

I am currently having the problem of how I might be able to have a .txt file with multiple lines and each week the php code would grab the line for the corresponding week so first line from the .txt for first week second line for second week and so on...

And I am also trying to use that php code and except of echoing it, to use html p tag to execute the line from the txt file

KstreakOG
  • 23
  • 6

1 Answers1

0
  <?php 
    date_default_timezone_set('America/Los_Angeles'); //set your timezone
    $file = file_get_contents('file.txt'); //Get the file
    $splitLines = explode("\n", $file); //use \r\n if \n doesn't work (linux and windows differences)
    $weeknumber = date("W", strtotime("now")); //Get current weeknumber

    $line = $splitLines[($weeknumber -1)]; //get the right line, -1 because week 1 is 0, week 2 = 1, etc. etc.

    echo "<p>$line</p>"; //echo the line in the <p> tag
    ?>

IF you want to do this within html instead of php, do it like this:

<body>
  <p><?= $line ?></p>
</body>

Tho I do feel like I should tell you, that this is a very bad idea. It's a lot smarter to simply make an array in a seperate php file which you could include or something. Eventually, you might bump into problems with a line-break based structure.

-- You asked me how to execute it instead of doing an echo. I don't know what you mean, but here's a try:

       <?php 
function getWeekLine(){
        date_default_timezone_set('America/Los_Angeles'); //set your timezone
        $file = file_get_contents('file.txt'); //Get the file
        $splitLines = explode("\n", $file); //use \r\n if \n doesn't work (linux and windows differences)
        $weeknumber = date("W", strtotime("now")); //Get current weeknumber

        $line = $splitLines[($weeknumber -1)]; //get the right line, -1 because week 1 is 0, week 2 = 1, etc. etc.

        Return $line;
    }
            ?>

IF you want to do this within html instead of php, do it like this:

<body>
  <p><?= getWeekLine() ?></p>
</body>

Now, when you're in need of "execution", just say <?php getWeekLine(); ?> Use echo if you want to actually show the results.

NoobishPro
  • 2,539
  • 1
  • 12
  • 23
  • How would i execute the code instead of echoing on html – KstreakOG Apr 10 '16 at 02:00
  • What do you mean by executing it? You mean turning it into a function? You have no context in your question, so I do not know what you mean exactly. – NoobishPro Apr 10 '16 at 02:02
  • @KstreakOG I think I understand now. I've edited my answer. – NoobishPro Apr 10 '16 at 02:12
  • I might make a php file and use an array couldnt I do this in javascript @Babydead ? – KstreakOG Apr 10 '16 at 02:16
  • @KstreakOG You could do this in javascript, but your question is posted in php. I assumed you needed this function in PHP. – NoobishPro Apr 10 '16 at 02:17
  • I didnt want to post a question for both languages ... Could you show me in javascript... if you can – KstreakOG Apr 10 '16 at 02:31
  • @KstreakOG there is no need for me to do that, you can find teh answer here: http://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php – NoobishPro Apr 10 '16 at 02:37
  • will this automatically do this everyweek? – KstreakOG Apr 10 '16 at 03:34
  • @KstreakOG Yes, as my script gets the current date (today) and then the weeknumber of today. So, say today is week 30 (It's not, just an example), monday it will be week 31. This will then grab line 31 in your text file, etc. You can test this out by replacing `$weeknumber` with any number between 0 and 52 you want. – NoobishPro Apr 10 '16 at 03:36
  • im confused on what you are doing with the week what week will it be and how will i do this in the text file? @Babydead – KstreakOG Apr 10 '16 at 04:12
  • @KstreakOG You said you wanted every line in the text-file to be another week. I say get the week we are in right now. Then I grab the .txt file and I say "grab the text file's line #weeknumber" To explain it simply. - so every "enter" you place in your textfile is a new weeknumber. – NoobishPro Apr 10 '16 at 04:17
  • So How would i test it ? in like each minute or second so just for testing... – KstreakOG Apr 10 '16 at 04:18
  • @KstreakOG in my code you see `$line = $splitLines` etc. etc. -- put `//` before it (so you don't remove it) and underneath it put `$line = $splitLines[23];` -- you can put 23, or 25, or 10, or 3, or 50... that is the weeknumber. So the line number of your file. – NoobishPro Apr 10 '16 at 04:20
  • yes but wouldnt i have to change the week number then each week? – KstreakOG Apr 10 '16 at 04:21
  • can you show me it in javascript its much easier for me ... like in your way! – KstreakOG Apr 10 '16 at 04:22
  • @KstreakOG no... if you remove your testing `$line` again and remove the `//` before my `$line` (after testing), my script automatically takes the correct week. – NoobishPro Apr 10 '16 at 04:23
  • Thank you ! it works but what if I wanted to do this for everyone not just LA! – KstreakOG Apr 10 '16 at 04:25
  • @KstreakOG That's good mate, I'm glad =) - This is actually extremely complicated. It's very hard to make it work around the world with different time zones. -- but, you should know, this doesn't "just work for la". It works for the world, only in china, it might be 12 hours behind. Because it's 12 hours earlier/later in china (I don't even know), but the time zone doesn't change that it will work everywhere. It just changes the current time ;) – NoobishPro Apr 10 '16 at 04:32