Jump to content

Recommended Posts

Hello there,

 

I'm trying to update a tab delimited file using $_POST and var_dump.

 

Here's my form code:

<?php
$array = file('game.txt');
foreach($array as $key){
$data = explode(',',$key);
echo '<input name="game[][opponent]" type="hidden" value="'.$data[0].'" />
<input name="game[][score]" type="text" />';
}

 

But, updating it is what I'm having difficulties with. I want to completely overwrite the content of the txt file and replace it with the updated data.

 

Here's what I've got so far:

// Get posted Data
$data = var_dump($_POST['game']);
$fp = fopen("game.txt","a"); // $fp is now the file pointer to file $filename
if($fp){
fwrite($fp,$data);      //      Write information to the file
fclose($fp);  //        Close the file
echo "File saved successfully";
} else {
echo "Error saving file!";
}

 

How can I modify the posted data to be in the format of:

opponent,score
opponent,score

etc?

 

 

I would use a DB, but I'm doing it for a friends football team and he's not keen on using databases for some strange reason.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/191244-var_dump-tab-delimited-files/
Share on other sites

You'll want to loop through $_POST['game'] array

 

$data = '';
foreach($_POST['game'] as $gameResult)
{
    list($oppenent, $score) = array_values($gameResult);

    $data .= "$oppenent,$score\n";
}

$fp = fopen("game.txt","w"); // $fp is now the file pointer to file $filename
if($fp)
{
    fwrite($fp,$data);      //      Write information to the file
    echo "File saved successfully";
}
else
{
    echo "Error saving file!";
}

Something like this

 

<?php
// Get posted Data
$fp = fopen("game.txt","a"); // $fp is now the file pointer to file $filename
if($fp)
{
foreach($_POST['game'] as $data)
{
	$outline = $data['opponent'].','.$data['score'];
	fwrite($fp,$outline);      //      Write information to the file
}
fclose($fp);  //        Close the file
echo "File saved successfully";
}
else 
{
echo "Error saving file!";
}
?>

Thanks for that wildteen88 & jl5501 :)

 

It's now updating everything, but it's adding a new line after each variable (so its:

opponent,
score
opponent,
score

 

I added a separate bit in, where it has a drop down list but it can select something.

 

It's only one option per line eg:

1-0,
1-1

etc

 

It's done in the same way as selecting the opposition.

 

How would I stop it from adding a new line?

Thanks in advance

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.