hey guys,
im new to this forum and new to php and was hoping someone out there can help me out.
what i need to do is be able to delete or modify a specific line from the .csv file when the person clicks the delete or modify button next to the state and abbreviation (which is on state_form.php, the code is on state_manager.php).
i have some of the code, and as you can see, it deletes the first row of the .csv file no matter what delete button i push.
$filename = "states.csv"; // File which holds all data
$rowToDelete = 1; // This is line need to be deleted
$arrFp = file( $filename ); // Open the data file as an array
$numLines = count( $arrFp ); // Count the elements in the array
$fp = fopen( $filename, "w" ); // Open the file for writing
for($i=0; $i<$numLines; $i++) // Overwrite the content except the line to be deleted
{
if($i != ($rowToDelete-1) )
fwrite($fp, $arrFp[$i]);
}
fclose( $fp ); // Close the file
i have tried to modify this code to get it to come up with the specific line, but i can't seem to come up with something that works. i hope you guys can help me out. thanks
http://www.the-silvas.com/cmis310.html
that is a hardcoded html file of what the php page looks like.
this is the code for the state_form.php
<form action="state_manager.php" method="POST">
NEW RECORD
<table>
<tr>
<td>
Abbr: <input type="text" name="abbr">
</td><td>
State: <input type="text" name="state_name">
</td>
<td>
<input type="submit" name="insert_state" value="Insert">
</td>
</tr>
</table>
</form>
<?php
$row = 1;
$handle = fopen("states.csv", "r");
?>
<form action="state_manager.php" method="GET">
MODIFY RECORDS
<table>
<?
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
?>
<tr>
<td>
Abbr: <input type="text" name="abbr" value="<?=$data[1]?>" readonly="readonly">
</td><td>
State: <input type="text" name="state_name" value="<?=$data[0]?>">
</td>
<td>
<input type="submit" onClick="return delete()" name="delete_state" value="Delete">
<input type="submit" name="modify_state" value="Save">
</td>
</tr>
<?
}
?>
when the user clicks the delete button, it should delete that row of the file.
the teacher gave me this information, but i don't know how to do it.
once the user clicks the DELETE button, your state_manager.php
should look at the Abbreviation $_POST[$abbr] (or whatever that field is called)...
Find that element on your associative array that holds all the elements of the state.csv file....
When you find it, delete that element from the array (research ways to delete items from an array)
Then, write the contents of the array back into the state.csv file
redirect user back to the state_form.php
im guessing that this way can be used for modifying the state also? (that is when the user clicks the save button.)