johnwayne77 Posted September 13, 2008 Share Posted September 13, 2008 i have a list of codes: 0000001 0000002 0000003 0000004 0000005 i want to read first line, cut the code (so it won't remain in the list anymore) and put it in a variable. the list would be either in the php file itself or an attached .txt file how do i do that? tnx in advance Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 13, 2008 Share Posted September 13, 2008 If the the codes are stored in the text file then use $codes = file('codes.txt'); // get first code $first_code = array_shift($codes); echo $first_code // write remaining codes to codes.txt $codes = array_map('trim', $codes); file_put_contents('codes.txt', implode(PHP_EOL, $codes)); Quote Link to comment Share on other sites More sharing options...
johnwayne77 Posted September 13, 2008 Author Share Posted September 13, 2008 it echoes the first line but on 2nd refresh it echoes the same first line here is the error i get: [sat Sep 13 17:07:07 2008] [error] PHP Warning: file_put_contents() expects at least 2 parameters, 1 given in /home/xxx/public_html/test/code.php on line 11 line11: file_put_contents(implode(PHP_EOL, $codes)); Quote Link to comment Share on other sites More sharing options...
johnwayne77 Posted September 13, 2008 Author Share Posted September 13, 2008 i've replaced the line with: file_put_contents($codes, implode(PHP_EOL, $codes)); but i still get an error: [sat Sep 13 17:12:59 2008] [error] PHP Warning: file_put_contents() expects parameter 1 to be string, array given in /home/xxx/public_html/test/code.php on line 11 Quote Link to comment Share on other sites More sharing options...
johnwayne77 Posted September 13, 2008 Author Share Posted September 13, 2008 it workd with this line: file_put_contents('codes.txt', implode(PHP_EOL, $codes)); Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 13, 2008 Share Posted September 13, 2008 That's exactly what he had in his post. Why didn't you just copy it? =/ Quote Link to comment Share on other sites More sharing options...
johnwayne77 Posted September 13, 2008 Author Share Posted September 13, 2008 ah, right.. dunno how i switched it.. my bad cheers! Quote Link to comment Share on other sites More sharing options...
johnwayne77 Posted September 13, 2008 Author Share Posted September 13, 2008 how could i do the same thing with a list located in sql? (cut value and update table) any ideas? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 13, 2008 Share Posted September 13, 2008 how could i do the same thing with a list located in sql? (cut value and update table) any ideas? How is the list stored in your database? Quote Link to comment Share on other sites More sharing options...
johnwayne77 Posted September 13, 2008 Author Share Posted September 13, 2008 i have a table: codes with a field name: code where there added the values (i was thinking storing the codes in the db is safer than in plain txt) Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 13, 2008 Share Posted September 13, 2008 If all you're doing is storing your codes and nothing else, then you can use a text file. To prevent any one publicly accessing your text file move it outside of your sites document root, This will then only allow PHP to access your text file. Quote Link to comment Share on other sites More sharing options...
johnwayne77 Posted September 14, 2008 Author Share Posted September 14, 2008 yeah, true, i could do that and that codes txt file will be safe.. but just for my knowledge, how could i do it with the sql database? also, i need this php script to be ran only by the server (restrict ip access).. any ideas? thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.