I have a function in my program to write an array to a text file, but every way I've tried results in the same result: when I open the file to check what's been written to it, all it displays is the word "Array" at the top. It's the last step I need to finish this assignment! I've tried a foreach loop, a listeach loop, and the simplest solution, shown below:
[PHP]function saveData($myFile)
{
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;
}[/PHP]
None of these has helped. Any advice?
10 49367
If all you are writing is "Array" I would look at that part of the script to see if that is all you are asking it to write there.
I know that you have done a lot of work but the output being the word "Array" makes me think that you want to look at that part.
Can you show your entire script? It may help us better to help you.
Good Luck.
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;
-
?>
I checked that one... my issue is that I need this to write to a file instead of displaying on the screen. How would I go about doing that using this method?
I checked that one... my issue is that I need this to write to a file instead of displaying on the screen. How would I go about doing that using this method?
Assign a variable to var_export() and then write the variable to a file.
Code:
$file = fopen("employees.dat", "w");
fwrite($file, var_export($myFile));
fclose($file);
resulted in a blank "employees.dat" file and displayed the original array as associated with : array[0]=>.
Code:
$file = fopen("employees.dat", "w");
fwrite($file, var_export($myFile));
fclose($file);
resulted in a blank "employees.dat" file and displayed the original array as associated with : array[0]=>.
It appears, then, that $myFile is an array with one element which is empty.
I was also trying to use var_export for arrays. What about this:
[PHP]
$FileName = "Some_File.txt";
$Data = file($FileName);
$Lines = count($Data);
...
for ($i=0; $i<$Lines; $i++){
$Array .= "$Data[$i]\n" ; // !!
}
[/PHP]
now you can write the $Array using fwrite() without problems.
@tnspc
Set the second arg to var_export to 1. This tells var_export to return its results as a string instead of writing to the terminal. - $file = fopen("employees.dat", "w");
-
fwrite($file, var_export($myFile,1));
-
fclose($file);
This worked for me
@Motoma
Kudos buddy. Worked well
Sign in to post your reply or Sign up for a free account.
Similar topics
by: sp |
last post by:
Hello everybody,
I have an xml doc and I am trying to write the values from xml file to a tab
delimited text file. Currently, what I am doing is I am reading xml file
through XpathNavigaotr and...
|
by: jigi via DotNetMonster.com |
last post by:
i have a compiled dll, and i want to see what values ar in certain methods
during run time. i have built the release build . is there a way to write to
a text file on my desktop or qa desktop?
...
|
by: cwbp15 |
last post by:
Using Visual Studio C#
When I ran the following code:
System.IO;
private void Button1_Click(object sender, System.EventArgs e)
{
//FileStream fs = File.Create(Server.MapPath("test.txt"));...
|
by: poldoj |
last post by:
Hi all, I am trying to write the content of a variable to a text file. I am
currently switching from VB 6 code to VB.NET. Here is the code in my vb 6
applications (this code works)...
|
by: poldoj |
last post by:
First I would thank you all for the replies at my previous post. Say sorry
for my poor english also. I have found where my problem came from. I am
writing a string to a text box and write the...
|
by: Yunus's Group |
last post by:
Yunus's Group May 23, 3:36 pm show options
Newsgroups: microsoft.public.dotnet.languages.vb
From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this
author
Date: 23 May 2005...
|
by: Art |
last post by:
Hi,
I have a fairly large text file with data that I need to put into an Access
Database. The text file is not uniform and I have to read it line by line in
order to derive some of the fields. ...
|
by: Marc |
last post by:
Hi,
I have a button on a form that, when pressed, adds additional user
defined buttons to the form. I need to be able to save the user
preferences and thought the best way would be to write the...
|
by: psbasha |
last post by:
Hi,
Is it necessary in Python to close the File after reading or writing the data to file?.While refering to Python material ,I saw some where mentioning that no need to close the file.Correct me...
|
by: Gilles Ganault |
last post by:
Hello
I'm stumped as to why PHP fails writing into a text file, with the
script being called through Apache:
==============
# pwd
/usr/local/www/data/
==============
# ll
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |