Saving data to a file - Please Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Saving data to a file - Please Help

Post by phphelpme »

Hi, Jason here,

I need to get the string data from $sid and $nam to save into a file. The coding echo's the values at the moment and also I have attempted to save the data into a file. For some reason the echo $sid','$nam','; data does what I want from the coding but when I try to save the values that have been echoed it only seems to save one entry from each string and not them all.

It currently echoes the data like this;

sid, nam, sid, nam, sid, nam, sid, nam,

This is what I need to be saved into a file so the rest of my system can then call the saved file and explode the data for future use within a select form.

Here is my coding so far, and you can see at the bottom my attempts to save this data:

Any help would be greatful, as the whole system grabs data that it needs but this section I have to use this code to display the sid and nam values, copy and paste into a .php file and the upload that .php file to my selected directory on the hosting space. If I can get this coding to extract the sid and nam then save it to a file on the host that would cancel out the need for me always having to upload manually etc which can be cery time consuming.

Cheers for any help,

Jason.
Last edited by phphelpme on Tue Nov 23, 2010 8:03 am, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Saving data to a file - Please Help

Post by Celauran »

You're opening the file with 'w' when you want 'a+'. To avoid opening the file a million times, though, why not store everything in a variable or an array, then write once?

Code: Select all

$savedata = '';
for ($i = 1; $i <= substr_count($alltext, $find); $i++)
{

    $pos = strpos($store, $find) + 16;   // gets position of the profile link

    $lestring = substr($store, $pos, 60);
    $sid= substr($lestring, 0, strpos($lestring, "\""));
    $lestring = explode($sid."\">", $lestring);
    $nam = substr($lestring[1], 0, strpos($lestring[1], "<"));
    $store = substr($store, $pos+1);

    echo $sid.",".$nam.",";
    $savedata .= $sid.",".$nam.",";

}

// file saving starts here
$file = "save.php";

$fp = fopen($file, 'w');
fwrite($fp, $savedata);
fclose($fp);

// file saving ends here
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Saving data to a file - Please Help

Post by phphelpme »

Hello Celauran,

Thank you so much for your reply. You know I tried saving it to an array in the coding but as you clearly have shown I missed out:

Code: Select all

$savedata = '';
From the top. I can not believe this. You have been a life saver as now you have just saved me so much time because I was having to save the results and upload a file via ftp.

Have youany advice on how I would attempt to allow a user to change the order in which they are saved because at this moment in time, the just get saved in the order they come from the site. I would really like to be able to give the option to choose which order the values are save to file in... Not really sure whether I need to use a sql database or use the array and give each one a value or something which can be changed etc.

Thanks again for your great input. I am very please that this is now done without me uploading all the time... :mrgreen:
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Saving data to a file - Please Help

Post by Celauran »

I'd opt for a database table over an array. You can have a column specifically to keep track of each row's position that way.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Saving data to a file - Please Help

Post by phphelpme »

Do you know how I would attempt to complete this action?

Not sure where to start with the code getting it to save to a table and how to get it to give the user an option to change the order they are saved in... lol

Sorry, but I am a junior programmer and my senior has retired so now I have to step upto the mark and I am a little bit taken back really... lol :?
Post Reply