Jump to content

Got a start on writing to a file


Zyse

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

<?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
Share on other sites

<?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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.