Jump to content

Flat File Question


smordue

Recommended Posts

I have a user form that populates a flat file with about a dozen items that seldom need to be changed.

 

But every now and then the user wants to update an item or two. Right now they have to go back to the form and refill all the items including the ones they are not changing as it overwrites the file. It is not a real big deal, but I would like to know if there is a way that the form could be populated with the existing data, if any exists, so all they need to do is change the items they want and submit. I understand that all of this would be easy with a mysql db, but for 12 items, it is too much bother.

 

Is there a piece of code I could add to my form fields that would grab the existing data, by line # and insert it in the input field?

 

 

Link to comment
https://forums.phpfreaks.com/topic/185141-flat-file-question/
Share on other sites

Data.txt

joe|blow|windy|[email protected]|1

jane|doe|bucky|[email protected]|10

 

data from a login or from session in this example I'll use session

 

 

 

$file=file('data.txt');

 

foreach($file as $line)

{

list($fname,$lname,$username,$email,$access_level)=explode("|","$line");

if ($_SESSION['username'] == $username)

{ break;}

}

 

 

<form>

<input type="text" name=fname value="<?php echo "$fname"; ?>">

</form>

 

 

You may also see <?=$fname?>, it will output the same as in the form above but you need short tags turned on and not all servers do. If you have control over the server and the code will not be distributed then you can use short tags.

 

 

HTH

Teamatomic

Link to comment
https://forums.phpfreaks.com/topic/185141-flat-file-question/#findComment-977398
Share on other sites

So this would populate my form with the data previously entered, and a user could just type over this data and submit to change the data?

 

BTW my flat file is just for one user at a time so it looks like:

 

A Company Name
123 Pine Street
Suite 100
Atlanta
GA
10028

 

 

Link to comment
https://forums.phpfreaks.com/topic/185141-flat-file-question/#findComment-977402
Share on other sites

same thing except you would not need to explode the line just $file['0'] for the first line and $file['1'] for the second line,etc. You would however need to trim the line $line=trim("$line") to knock of the \n cause acompanyname\n does not == acompanyname unless you trim the \n

Link to comment
https://forums.phpfreaks.com/topic/185141-flat-file-question/#findComment-977407
Share on other sites

Thanks Teamatomic, I am 90% there from your advice. I have three other items on this form that have me stumped.

 

How do I show whether a checkbox was checked?

 

<label for="about">About Us :</label><input name="about" type="checkbox" value="about" />

 

What about a field where I am indicating a default value?

 

<label for="abouttab">Page Name :</label><input type="text" name="abouttab" value="About Us"/>

 

And the trickiest, I think, a select where the value stored is different than the display name?

 

<select name="theme" style="width: 200px">
<option selected="selected" >Select Theme</option>
<option value="styles1.css">Autumn</option>
<option value="styles2.css">Winter</option>
<option value="styles3.css">Summer</option>
<option value="styles4.css">Spring</option>
</select>

 

I really appreciate your help.

 

Steve

Link to comment
https://forums.phpfreaks.com/topic/185141-flat-file-question/#findComment-977792
Share on other sites

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.