Zyse Posted February 11, 2010 Share Posted February 11, 2010 so I got a start on it using a tutorial online, but stuck at a point of modifying it. <?php $File = "links.txt"; $Handle = fopen($File, 'a'); $Data = "Jane Doe"; fwrite($Handle, $Data); $Data = "Bilbo Jones"; fwrite($Handle, $Data); print "Data Added"; fclose($Handle); ?> what I cant get it to do right now is have an input box with a submit button to write to the file, as well as return each line. I tried a standard submit form but how tdo I make the content link to what was submitted? so far ive made a submit box that doesnt refresh the page saying data added, it just pops up with data added afetr the input box and the box does nothing >_< Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/ Share on other sites More sharing options...
WolfRage Posted February 11, 2010 Share Posted February 11, 2010 Please post the form, and any other scripts that you have. Then we can get a better picture of what you are doing and will be able to give you a more specific solution. Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010594 Share on other sites More sharing options...
Zyse Posted February 11, 2010 Author Share Posted February 11, 2010 ive gotten a bit farther with this so heres what I have but getting a parse error on line 4 of process.php html code is this <html> <head><title>Test Page</title></head> <body> <h2>Data Collection</h2><p> <form action="process.php" method="post"> <table> <tr><td>ID:</td><td><input type="text" name="ID" /><input type="submit" /></td></tr> </table> </form> </body> </html> <?php $File = "links.txt"; $Handle = fopen($File, 'a'); $Data = "http://www.site.com/account="($ID); fwrite($Handle, $Data); print "Data Added"; fclose($Handle); ?> Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010596 Share on other sites More sharing options...
WolfRage Posted February 11, 2010 Share Posted February 11, 2010 you forgot a "." in your code. Should look like this below: <?php $File = "links.txt"; $Handle = fopen($File, 'a'); $Data = [url=http://www.site.com/account=]http://www.site.com/account=[/url].($ID); fwrite($Handle, $Data); print "Data Added"; fclose($Handle); ?> Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010599 Share on other sites More sharing options...
Zyse Posted February 11, 2010 Author Share Posted February 11, 2010 Using that without the URL cause im just tring to get it to right a line to a file on a new line each time, I still get the same parse error using the code you showed me, it doesnt seem to be getting the form data data from the html page, or something else entirely >_< I editesd it and it says unidentified variable so it isnt getting the form data from the html page. Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010600 Share on other sites More sharing options...
WolfRage Posted February 11, 2010 Share Posted February 11, 2010 When I pasted your code over it removed the double qoutes and attempted to make it a url. Try it now. That is very annoying. <?php $File = "links.txt"; $Handle = fopen($File, 'a'); $Data = "http://www.site.com/account=".($ID); fwrite($Handle, $Data); print "Data Added"; fclose($Handle); ?> Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010601 Share on other sites More sharing options...
Zyse Posted February 11, 2010 Author Share Posted February 11, 2010 that accually exactly as I edited it already lol, it says unidentified variable, Also how can I make this all work on the same page using forms and such that way the variable will be present upon submition button, (not refreshing the page.) Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010604 Share on other sites More sharing options...
WolfRage Posted February 11, 2010 Share Posted February 11, 2010 SO you did not define $ID, did you perhaps mean SID? In that case you do not need the parenthesis either. In order to put it all on the same page you will either give a name and a value to your submit button or check to see if $_POST['ID'] isset. I think you are trying to use register globals which is a very bad pratice. Try this instead. <?php $File = "links.txt"; $Handle = fopen($File, 'a'); $Data = "http://www.site.com/account=".$_POST['ID']; fwrite($Handle, $Data); print "Data Added"; fclose($Handle); ?> Then if you want it on the same page then use a if and else statement to check if $_POST['ID'] isset(). Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010609 Share on other sites More sharing options...
Zyse Posted February 11, 2010 Author Share Posted February 11, 2010 Wow fantastic that worked, thank you very much, now I just need to do a php redirect back to the submit page, Also do you happen to know the php read commane to read a random line from a text file and goto it as a url? or in more detailed terms, direct it to a frame? Thanks again for that assistance. Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010611 Share on other sites More sharing options...
Zyse Posted February 11, 2010 Author Share Posted February 11, 2010 I also cant seem to find out ghow to make it do a new line each time instead of writing to the same line >_< Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010620 Share on other sites More sharing options...
jl5501 Posted February 11, 2010 Share Posted February 11, 2010 append a newline on the end of your data when you write write($Handle, $Data."\n"); Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010623 Share on other sites More sharing options...
Zyse Posted February 11, 2010 Author Share Posted February 11, 2010 when doing that it adds a box to the text file instead of putting it on a new line, if I have a php command read a random line will it take into consideration the boxes are new lines? Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010631 Share on other sites More sharing options...
WolfRage Posted February 11, 2010 Share Posted February 11, 2010 I believe that jl5501 meant fwrite(). other than that he is right. To add a new line simple change this line: <?php fwrite($Handle, $Data); ?> to: <?php fwrite($Handle, $Data."\n"); ?> to read a specific line from a file you will want to use fgets() http://php.net/manual/en/function.fgets.php Then simply echo the fgets() string to the frame. To redirect use header() http://php.net/manual/en/function.header.php Like: <?php header([font=consolas]'Location: [url=http://www.example.com/']http://www.example.com/'[/url]);[/font] ?> Make sure you have no output prior to that command. Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010644 Share on other sites More sharing options...
Zyse Posted February 11, 2010 Author Share Posted February 11, 2010 Ty for your replay, I just ignored the missing f and added it in thinking nothing of the typo so I already got that one, but im more looking for a sandom like rather then specific, my last thing I need is it to open a random line as url to a frame on the right,, but ty so much, ill be back tomorrow. Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010661 Share on other sites More sharing options...
WolfRage Posted February 11, 2010 Share Posted February 11, 2010 Well you will need to count the number of lines in the file and then pick a random number between zero and the number of lines. To loop through the file use a while like so: <?php [font=consolas]$fp = fopen("myfile.txt", "r"); while (!feof($fp)) { // increment your counter. } fclose($fp);[/font] ?> To learn more check out http://php.net/manual/en/function.feof.php & http://php.net/manual/en/function.fgets.php Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1010681 Share on other sites More sharing options...
Zyse Posted February 11, 2010 Author Share Posted February 11, 2010 where would I incorperate this style of code, by using it is there a echo I can use to tell me how many lines there are to make sure its reading correctly? Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1011046 Share on other sites More sharing options...
Zyse Posted February 12, 2010 Author Share Posted February 12, 2010 Bah im getting no luck in verifying its reading the file >_< im using the code as posted. Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1011099 Share on other sites More sharing options...
WolfRage Posted February 12, 2010 Share Posted February 12, 2010 Come on now try to learn. You know that this code was not complete I stated you need to increment your counter, which is not defined, plus you need to echo it. <?php $fp = fopen("myfile.txt", "r"); $count=0; while (!feof($fp)) { $count=$count+1; } echo 'The number of lines in this file is = '.$count.' +1 because the file and the counter start at line 0.'; fclose($fp); ?> Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1011263 Share on other sites More sharing options...
Zyse Posted February 12, 2010 Author Share Posted February 12, 2010 I am sorry, I'm trying to learn I have been staring at that page you linked me to figure it out, but my brain is full XD, ok im trying I promise. OMG I know what I was doing wrong why I couldnt get the last script to work... im such a tard. I was trying to open the file without using the browser >.>. ok ok im on the same page now. Hmm it seems to loop indefinitely, I also changed the echo to print, if thats ok in the coding for referencing later. Bah it then loops and errors an infinite amount of these on a page Warning: feof() expects parameter 1 to be resource, boolean given in C:\wamp\www\ran\process.php on line 13 while (!feof($fp)) Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1011490 Share on other sites More sharing options...
Zyse Posted February 12, 2010 Author Share Posted February 12, 2010 <?php $file = "links.txt"; $lines = count(file($file)); echo " " .$lines. " lines in total"; ?> I found a tut for this and it counts the lines, but is this to simple in order for it to grab a random line it counted? Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1011504 Share on other sites More sharing options...
Zyse Posted February 12, 2010 Author Share Posted February 12, 2010 <?php echo countLines("links.txt"); function countLines($filepath) { $handle = fopen( $filepath, "r" ); $count = 0; while( fgets($handle) ) { $count++; } fclose($handle); return $count; } ?> I also found that this can also be used as an increment, using this seems to avoind the constant loop or hang up, would this be a better method to count lines in order to grab a random one as an url? Edit Found out how to call a random line from file seems to work but pulls up the first line most of the time. Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1011517 Share on other sites More sharing options...
Zyse Posted February 12, 2010 Author Share Posted February 12, 2010 <?php srand ((double)microtime()*1000000); $f_contents = file ("links.txt"); $line = $f_contents[array_rand ($f_contents)]; print $line; ?> for the grabbing of random line. Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1011519 Share on other sites More sharing options...
Zyse Posted February 12, 2010 Author Share Posted February 12, 2010 <?php srand ((double)microtime()*1000000); $f_contents = file ("links.txt"); $line = $f_contents[array_rand ($f_contents)]; echo "<FORM METHOD="."LINK"." ACTION=".$line."> <INPUT TYPE="."submit"." VALUE="."Random"."> </FORM>"; ?> <form><input type=button value="refresh" onClick="window.location.reload()"></form> Ive got this for getting a random line and going to a url ith it, but cant figure out how to make it 1 button that refreshed after clicking, as well is how to set the target as a frame, was it target="framename"? Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1011552 Share on other sites More sharing options...
WolfRage Posted February 12, 2010 Share Posted February 12, 2010 Yes target = "framename" shoud do it. This method is probably the simplest method it enact however it is also the most resource intensive as it reads the entire file into an array. $lines = count(file($file)); I think the best appraoch is to use the while with the fgets() as it automatically advances the file pointer. Sorry about the infinite loop, I forgot to use fgets in my example along with incrementing the counter which caused your loop. So I would do it like this: $handle = fopen( $filepath, "r" ); $count =0; while( fgets($handle) ) { $count++; } $line=rand(0,$count); fseek($handle,0); fseek($handle,$line); $data=fgets($handle); fclose($handle); echo '<FORM METHOD="'."LINK".'" ACTION="'.$line.'"> <INPUT TYPE="'."submit".'" VALUE="'.$data.'"> </FORM>'; //Your echo statement was not producing valid HTML. Hope this advances your quest! Make sure you replace "LINK" with your actual link... Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1011563 Share on other sites More sharing options...
Zyse Posted February 13, 2010 Author Share Posted February 13, 2010 So I pretty much have it almost complete, ill post in a bti after a couple more tests. Link to comment https://forums.phpfreaks.com/topic/191735-got-a-start-on-writing-to-a-file/#findComment-1011630 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.