| re: Writing an array to a text file
Thanks for the feedback. However, I'd already tested the code before posting this, and without going into too much detail, it is definitely passing the array properly into the function. (I used the print_r() method to display it, so I know for sure). The problem seems to be that the fwrite method doesn't work for arrays, only strings, even though each element of the array IS a string. Are there any special methods for writing an array? I've already tested everything else and it all works except the last function. (By the way, ignore most of the comments; they were part of the code to start with).
And, here's the complete code:
?php
/*
loadData: returns an array of lines in a file
Parameters: the name of the file to be loaded, a string
the default is "employees.dat"
Returns: the array of lines in that file
*/
//write loadData here
function loadData($defaultFileName = "employees.dat")
{
if(!$defaultFileName = file("employees.dat"))
{
echo('Error while opening file');
}
return $defaultFileName;
}
/*
searchData: searches an array for a string
Parameters: $myFile - the array to look through
$oldName - the string to look for
Returns: the index where the string is found in the array or -1 if not found
*/
//write searchData here
function searchData($myFile, $oldName)
{
$a = $myFile;
$s = $oldName;
$numElements = count($myFile);
for($i = 0; $i < $numElements; $i++)
{
if((eregi($s, $a[$i])))
{
return $i;
}
else
{
if($i == $numElements-1)
{
return -1;
}
else
{
$i++;
}
}
}
}
/*
updateData: updates lines in an array
Parameters: $a - the array
$i - index in the array to update
$sOld - name in string to replace
$sNew - name to replace $sOld with
*/
//write updateData here
function updateData($myFile, $index, $oldName, $newName)
{
$a = $myFile;
$i = $index;
$sOld = $oldName;
$sNew = $newName;
$a[$i] = eregi_replace($sOld,$sNew, $a[$i]);
return array($a);
}
/*
saveData: saves array to a file, does not save empty array elements
Parameters: $a - array to be saved
$defaultFilename - name of file, default value "employees.dat"
*/
//write saveData here
function saveData($myFile)
{
echo("<br>");
print_r($myFile);
echo("<br>");
if($file = fopen("employees.dat", "w")):
$numElements = count($myFile);
for($i = 0; $i < $numElements; $i++)
{
fwrite($file, $myFile[$i]);
}
fclose($file);
else:
echo("Error in opening file. Please re-submit");
echo("<A HREF ='gradedLab3.htm'>Return to form</A>");
endif;
}
if(!empty($newName)):
$myFile = loadData();
$index = searchData($myFile,$oldName);
if($index == -1):
echo("<HTML><HEAD><TITLE>$name not found</TITLE></HEAD><BODY>");
echo("<B>$name</B> not found <BR>");
echo("<A HREF ='gradedLab3.htm'>Return to form</A>");
echo("<B>$name</B></HTML>");
exit();
else:
//update name and save data
//call updateData here
$myFile = updateData($myFile, $index, $oldName, $newName);
saveData($myFile);
?>
<HTML>
<HEAD>
<TITLE>Employee Updated</TITLE>
</HEAD>
<BODY>
<B><?php echo($oldName); ?></B> was updated <?php echo("to $newName"); ?><BR>
<A HREF = "gradedLab3.htm">Return to Form</A>
</BODY>
</HTML>
<?php
endif;
else:
?>
<HTML>
<HEAD>
<TITLE>No Name</TITLE>
</HEAD>
<BODY>
<B>No name was submitted</B><BR>
<A HREF = "gradedLab3.htm">Return to Form</A>
</BODY>
</HTML>
<?php
endif;
?>
|