How to edit a row -- not using database

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
chicOut
Forum Newbie
Posts: 3
Joined: Fri Mar 27, 2009 6:13 am

How to edit a row -- not using database

Post by chicOut »

Hello,

I am a beginner in programming and I would like to build a PHP address book function where I can edit and delete single entry. I am using a flat file.

What I have done so far is I could populate all the data from the file into a html table. I added 2 additional column for the edit and delete. Refer to my screen shot. Part of my code is as shown:

Please guide me (I appreciate a detailed explanation as my logic is not good) of how to achieve this:

a) When I click the edit or delete link, it would select all the information on that row and then send it to another PHP
page

b) After a), how do I select the information and populate the data back to the page so the user can edit it

I think it could be achieved with URL parameter but I am not sure how to do it. Can you guide me on that.

Any hints or web page that able to solve this.

***************************************************************************************

<?php

$fp = fopen('contact.txt','r');

print "<table border=1><tr><td>First Name</td><td>Last Name</td><td>Email Address</td><td>Phone Number</td><td>Street Address</td>
<td>City</td><td>State</td><td>ZIP</td><td>Access Code</td><td>Action</td><td>Action</td></tr>";

while (!feof($fp)) {

$data = fgetcsv($fp,'1024',','); //get a row at a time
$num = sizeof($data);


print '<tr>';
foreach ($data as $value) {
//for($a=0;$a<sizeof($data);++$a) {
//if (!$data[$a]) {
if(!$value) {
echo "<td>&nbsp</td>";

}
else {
echo "<td>$value</td>";

}

}
print '<td><a href = "abc.php?FirstName=".">Edit</a></td>';
print '<td><a href = "abc.php">Delete</a></td>';
print '</tr>';

}

print "</table>";

?>

**********************************************************************************
Attachments
link.jpg
link.jpg (20.99 KiB) Viewed 110 times
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: How to edit a row -- not using database

Post by andyhoneycutt »

I would need to see how you are storing your data before I could give you a decent answer. Any chance you can post a line from your file here so I can determine what you need to do?

-Andy
chicOut
Forum Newbie
Posts: 3
Joined: Fri Mar 27, 2009 6:13 am

Re: How to edit a row -- not using database

Post by chicOut »

My file is a simple flat file. So after filling in the form, I store the variables from POST then concatenate it and put it as a line in the text file.

And then I use fgetcsv to read line by line. I think I need to use counter, but I am not sure how. I hope I can be quick but I isn't, hope you can point the way :)
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: How to edit a row -- not using database

Post by php_east »

it is rather involved what you are asking, though the question is simlpe.
you would need ...
1. FORMs ( put your table inside the form ).
2. INPUTs
3. $_POST on the receiving php end.

and finally you need to merge the $_POST inside the INPUTs so that the current entry by the user is presented properly.

those are pointers for you. the flat file is fine, except that when reading the csv values, for the INPUTs you would need to give them names, like $name, $email so when you read your csv you need have the fields named.

you would probably end up with something like...

foreach ($data as $key=>$value)
{
if ($value) echo "<td><input name=".$key." value=".$value." /></td>";
}
chicOut
Forum Newbie
Posts: 3
Joined: Fri Mar 27, 2009 6:13 am

Re: How to edit a row -- not using database

Post by chicOut »

Yes, I managed to do that. But I think the trick now is to put the counter within that code, so when I select the Edit link (refer to the attachment earlier), it will pick up with row is that and match it with the file to delete.

Any hint on how to do it
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: How to edit a row -- not using database

Post by php_east »

ah, ok. just add a row number input to your form <tr.

Code: Select all

<input name='row_number_<?php echo $counter; ?>'  id='row_number_<?php echo $counter; ?>' value='0' />
 
then add a javascript to the delete which will give a new value to your row number input.

Code: Select all

onclick="element=document.getElementById('row_number_'+<?php echo $counter; ?>);
if (element) element.value='111';
";
 
if you see this value 111 ( for example ), then you delete the line.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to edit a row -- not using database

Post by califdon »

Is there a real need for you to use a flat file? It would be so much easier to use an open source database, like MySQL or SQLite. It would take less learning to use that than it would to program your own awkward csv based database.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: How to edit a row -- not using database

Post by php_east »

flat file has advantages. simplicity, speed, small and above all, less code overhead for simple tasks. once you get into mysql for simple things, it gets real messy, not to mention a nighmare for a beginner needing to achieve somethings first.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to edit a row -- not using database

Post by califdon »

php_east wrote:flat file has advantages. simplicity, speed, small and above all, less code overhead for simple tasks. once you get into mysql for simple things, it gets real messy, not to mention a nighmare for a beginner needing to achieve somethings first.
Respectfully, I disagree, at least partially. If you compare one small application, programmed by someone who will never again use a database for anything, you're probably correct. But there's absolutely no need for a small application to be "messy" and there's no need for learning simple database operations to be a nightmare; indeed, it should be simpler than all the low-level hacking required to extract data in any form except precisely the way it was stored. The "nightmares" that some people have when learning how to design and use databases is entirely due to the way they approach learning, very similar to programming. If you learn such skills methodically, starting at the beginning (what a concept!) and proceed to progressively more complex constructs, it is easy for most people to learn. Unfortunately, many people are impatient to learn how to do the task of the moment and are frustrated when they find it difficult to understand later techniques before learning earlier ones.
Post Reply