deleting an entry in a text file via php?

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
EÜRENN
Forum Newbie
Posts: 5
Joined: Wed Jun 07, 2006 1:49 pm

deleting an entry in a text file via php?

Post by EÜRENN »

Hi,

I have a flash site that uses php and txt files to generate and manipulate information on the site. In this particular example, i have flash drop-down component, that loadsVariablesNum from a dropdown.txt file. The file would have code like this:

&items=Link 1, link 2, link 3&urls=http://www.yahoo.com/, http://www.2advanced.com/, http://www.euroblitz.org/,

The flash splits these items by their commas, creating an array that adds them into the drop down menu. Obviously the item id is what you see as the label, and the url is the value of what happens when you select that id from the drop down.

I had also created a form in flash that ads items to the array and writes them to that dropdown.txt file by calling the dropdown.PHP file, which takes the info from flash, and writes it to the txt file. That all works fine from there.

But here is where i am stumped. I want to creat a form in flash that also deletes an entry, or edits one. How in the world do i single out 1 entry in the txt file and then delete or edit? Perhaps its something i have to get in flash... beats me.... Im just not all incredible when it comes php syntax and commands.

Any helps is much appreciated. Thanks,

Noah
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

I dont know much about php and text files.

Maybe this would work: remove that entry from the array, delete everything in the textfile, rebuild it.
EÜRENN
Forum Newbie
Posts: 5
Joined: Wed Jun 07, 2006 1:49 pm

Post by EÜRENN »

For me and my site, that would be fine, but to someone who doesnt know an inch of code that would be a problem. I wouldn't want them to have to manually go into the txt file and delete it, resave, and then gets rid of the executable settings on that file set from ftp. The idea is to make this thing as easy as possible to use for someone else. So i have to go the extra mile.
jonra
Forum Newbie
Posts: 22
Joined: Thu May 25, 2006 9:35 am
Location: Iowa
Contact:

Post by jonra »

Well, it's much easier if you have a consistent list so you know exactly what you're looking for. I've done a little in this regard, writing XML files as straight text and removing certain entries based on form data. The only difference is we usually have MySQL on the backend powering the actual storage, so I just rewrite the file each time an update is made.

I don't know how easy this is, but you could use a C++ style algorithm here. If it were C, I'd probably do this:

1) Bring in the entire text file
2) Load the file into an array, where each index is a line in the text file
3) Scan the array for the item you need to remove
4) Kill that item, then use a sorting algorithm (quick sort maybe, but there are lots of them) to shift the remaining items up by running through the array in loops again
5) Repeat process as needed
6) Write each index of the array (each line essentially) back to a text file by looping through it again
7) Save over the original copy of the text file

The only part I would see troublesome in PHP is the sorting algorithm. Not sure how much that helps you :(
EÜRENN
Forum Newbie
Posts: 5
Joined: Wed Jun 07, 2006 1:49 pm

Post by EÜRENN »

That feels like a step in another direction, probably the one i should have used a long time ago. I had an idea tho.

In flash, i could set a couple variables:

Code: Select all

var itemToDelete;
var urlToDelete;
When you select the item from the drop down menu, it then stores that information in a dynamic text field in flash:

Code: Select all

loDel = new Object();
loDel.change = function (evtDel) {
    my_Del = evtDel.target.selectedItem.label;
	my_DelURL = evtDel.target.getValue();

//The below 2 lines are the dynamic text fields.
	itemDelInput = my_Del;
	urlDelInput = my_DelURL;

//Then here youd see the variables set earlier to the selected drop down information. 
	itemToDelete = my_Del;
	urlToDelete = my_DelURL
}
combo_cb.addEventListener("change", loDel);
In php, when i run the script that normally just writes the new item and url information, is there a way to run a duplicate of it that excludes the "itemToDelete" and "urlToDelete" from the fwrite to the textfile?

I know there is a $exclude = "" function in php, but i don't know exactly how to use it properly. Maybe thats where some of you php geniuses can help out.
EÜRENN
Forum Newbie
Posts: 5
Joined: Wed Jun 07, 2006 1:49 pm

Post by EÜRENN »

Here is the working example:

My.Control.Panel

Just click on the GUI Editor icon, login, select "Home" from the left menu, then "Drop Down" and youll see where im working in flash.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

Why don't you save yourself some hassle and use splice to delete the entry in the array via actionscript. You can then post it to the php file which saves it.

The reason behind it - as you're using a text file, you can't just access/modify a certain part of it.
You have to read it in... etc. So I'd restrict the use of php to the read/write part.
aerodromoi
Post Reply