PDA

View Full Version : Deleting From A File



mobilebadboy
12-15-2008, 02:27 AM
Edit: Got it sorted for the most part. I was way off, heh.

I've been working on this for a while, and I've gotten close, just not close enough. So far it'll delete one line but not multiple lines. Basically I have a listbox:



<select name='merchants[]' multiple="multiple">
<option value="8334">8334 - Playful Plush Toys</option>
<option value="4742">4742 - Nature Hills Nursery</option>
</select>


In a txt file I have:



8334,Playful Plush Toys
4742,Nature Hills Nursery


And I'm trying to open that file and delete any lines that match what's been selected in the listbox. Here's my, probably horribly, code.



// horrible code removed

Again, it works with one line, but not multiple. Selecting multiple lines only removes the first line, and rewrites the file with an initial comma delimited blank line, followed by the second entry.

Chris
12-16-2008, 07:16 PM
Whatcha building with linkshare's merchandiser system?

And why not use a database? Databases are built for exactly this kind of operation you know.

mobilebadboy
12-17-2008, 12:13 AM
Yeah, DBs are always easier, but for the little bit of info I'm storing it's not worth it. Plus I want it as portable, with little if any footprint, as possible. It's actually for ShareASale, it's a script that automatically downloads my merchants' feeds from SAS' FTP and imports them to the database.

I originally wrote it where you had to fill in all the info (DB info, SAS FTP info, merchant ID, etc) for each merchant. Now I can add/delete merchants (from the txt file) and all the rest of the stuff is stored in a config file. Now it's simply selecting the merchant from a dropdown and hitting Go.

Chris
12-17-2008, 08:04 AM
When I did a similar thing I did it all with cron. I really don't know how to do text file editing to the degree you want with PHP. open, read, append, write, thats all I've ever done.

Maybe open the file into an array. Then rewrite the file in php with a simple conditional statement that doesn't let the one you want deleted to be part of the rewrite.

So technically your script is doing more heavy lifting, copying the entire file, then writing over it with one small change, but the programming is fairly straightforward.

mobilebadboy
12-17-2008, 10:53 AM
Yeah, I do have many of my larger feeds setup on cron. This script is part for myself, part for others, those who are 'clueless' when it comes to feeds. Another reason I'm using a text file for the merchant IDs, to keep it simple. One day I got tired of post after post at ABestWeb -- 'how do I download a feed,' 'how do I get it in my database', etc.

It'll connect to SAS' FTP, download the feed to the server, connect to the database and create a temp table, import the feed into that table, then rename it to whatever main table name is used for actually calling the queries from (to minimize downtime for the main table).

After that they're on their own.

Its downloadable from shawnkerr.com/sasftp/. But since I just rewrote the entire thing, I haven't had time to clean it up since I have other things to do, so the code looks nasty.