Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/52011-writing-data-from-an-array-to-a-file/
Share on other sites

 

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);

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);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.