Jump to content

var_dump, tab delimited files


peter_anderson

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.