danscreations Posted May 18, 2007 Share Posted May 18, 2007 Hi Everyone! I'm new to php and am writing a script to basicly function as a news script but will be modified/made for anoter purpose. Anyways, I have been sucessful in opening the data file, seporating each (item; in an array), and then dividing each item up (item1||item2||item3, etc.) for use in the script and display purposes. When each item is printed out and "delete" link/button is printed under each. When clicked will pass an action (delete) and an item_key (the items ID). From there I would like it to do the following: Unset the item that was clicked for deletion (via item_key) Open the data file Write the remaining items in the array back to the data file Close the data file I set the following up at first for testing so that it wouldn't actually do anything with the data file but just unset the delete item and print (echo) the rest to make sure it was doing want I wanted it to do. And everything seemed fine. When I changed it to open, write, etc to the file something must me wrong. It just delete's everything. Can anyone tell me what I need to do to fix this buy the code? $items = file("items.txt"); $action = $_GET['action']; $item_key = $_GET['item_key']; if ($action == "delete"){ $fh = fopen("items.txt","w"); echo '<b>Item <i>'; echo $item_key; echo '</i> has been deleted</b><br><br>'; unset ($fh[$item_key]); fopen ($fh,"w"); foreach ($fh as $which_left){ fwrite ($fh,$which_left); } fclose ($fh); } else { foreach($items as $key => $sort_items) { $sep_items[$key] = explode("|", $sort_items); echo '<b>Item: </b>'; echo $sep_items[$key][1]; echo '<br>'; echo '<b>Code: </b>'; echo $sep_items[$key][0]; echo '<br>'; echo '<b>Price: </b>'; echo $sep_items[$key][2]; echo '<br>'; echo '<a href="?action=delete&item_key='; echo $key; echo '">Delete Item</a></br><br>'; }} Thanks! Dan Quote Link to comment https://forums.phpfreaks.com/topic/52011-writing-data-from-an-array-to-a-file/ Share on other sites More sharing options...
hitman6003 Posted May 18, 2007 Share Posted May 18, 2007 I found this to be helpful... http://www.stroz.us/php/index.php?option=com_smf&Itemid=40&topic=97.0 Quote Link to comment https://forums.phpfreaks.com/topic/52011-writing-data-from-an-array-to-a-file/#findComment-256330 Share on other sites More sharing options...
danscreations Posted May 18, 2007 Author Share Posted May 18, 2007 I found this to be helpful... http://www.stroz.us/php/index.php?option=com_smf&Itemid=40&topic=97.0 Thanks! Figured out what was wrong. Didn't relize that once you unset something out of an array you can't just write it to a file due to the fact that array is stored with that array name. Needed to make another array name to print to the file....guess thats how it works. New Code unset ($items[$item_key]); $fh = fopen("items.txt","w"); foreach ($items as $whats_left){ fwrite ($fh,$whats_left); } fclose ($fh); Quote Link to comment https://forums.phpfreaks.com/topic/52011-writing-data-from-an-array-to-a-file/#findComment-256563 Share on other sites More sharing options...
per1os Posted May 18, 2007 Share Posted May 18, 2007 Instead of the foreach, lookinto the implode function. www.php.net/implode. That way if you want you can have each item seperated by like \n (linebreak) etc. Or just on one line. Maybe easier and friendlier looking. <?php unset ($items[$item_key]); $fh = fopen("items.txt","w"); fwrite ($fh,implode("\n", $whats_left)); fclose ($fh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52011-writing-data-from-an-array-to-a-file/#findComment-256567 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.