Hello and thank you for this great forum. What I am trying to do is get it where when the user clicks on the delete icon, that he or she is prompted with "ok" or "cancel" I have placed the code below. Information is pulled from a flat file, so you may need to create one and rename it to test it. Another thing I need help with is instead of $Last being the "key" I would like a number. So for every new contact, the next number will show next to listing. Last thing I am trying to do is have it where there will be a print button by each contact where the computer will only print that line of information. Thank you for your help.
<?php
# initialise variables
$Last = $First = $Company = $Phone = $Website = "";
$data = array();
$changed = false;
# define your data file and its path, ensure you have write permission to folder
$myTextFile = 'data/data_contacts.txt';
# get the data from current file
if (file_exists($myTextFile))
$data = parse_ini_file($myTextFile, true);
# check if user data form submitted
if (isset($_POST['userdata'])) {
if (!empty($_POST['Last']) ) {
$data[$_POST['Last']]['First'] = $_POST['First'];
$data[$_POST['Last']]['Company'] = $_POST['Company'];
$data[$_POST['Last']]['Phone'] = $_POST['Phone'];
$data[$_POST['Last']]['Website'] = $_POST['Website'];
$changed = true;
}
else echo "<CENTER><DIV CLASS='error'>Please enter a Last Name.</DIV></CENTER>";
}
# do we want to delete a record
if (isset($_GET['delid'])) {
# remove deleted item from the array
unset($data[$_GET['delid']]);
$changed = true;
}
# or do we want to edit a record
if (isset($_GET['changeid'])) {
# get the items we want to edit so they can appear in the form
$Last = $_GET['changeid'];
$First = $data[$Last]['First'];
$Company = $data[$Last]['Company'];
$data[$_POST['Last']]['Phone'] = $_POST['Phone'];
$Website = $data[$Last]['Website'];
}
if ($changed) {
# now create new output file, writing in ini file format
$fp = fopen($myTextFile, 'w');
ksort($data);
foreach ($data as $key=>$dataArray) {
fwrite($fp, "[$key]\n");
foreach ($dataArray as $k => $v) {
fwrite($fp, "$k=$v\n");
}
fwrite($fp, "\n");
}
fclose($fp);
}
# function to list the current data
function listData($data) {
if (count($data) > 0) {
echo "<TABLE border='0' cellspacing='5' cellpadding='0'>\n";
# headings
echo "<tr CLASS='heading'><th>Last Name</th> <th>First Name</th> <th>Company</th> <th>Phone</th> <th>Website</th>";
list ( $key, $dataArray) = each($data);
foreach($dataArray as $k=>$v) {
echo "";
}
echo "</tr>";
#data
$i = 0;
foreach ($data as $key=>$dataArray) {
$bg = ++$i % 2 ? "#E9E9E9" : "#DDDDDD";
echo "<tr style='background: $bg'>
<TD CLASS='data'>
$key
</td>";
foreach ($dataArray as $v) {
echo "<TD CLASS='data'>$v</td>";
}
echo "<TD CLASS='options'><A HREF='$_SERVER[PHP_SELF]?changeid=$key'><IMG SRC='images/edit.png' BORDER='0' TITLE='Edit'></A> <A HREF='$_SERVER[PHP_SELF]?delid=$key'><IMG SRC='images/delete.png' BORDER='0' TITLE='Delete'></A></td></tr>";
}
echo "</TABLE>";
}
else echo "<CENTER><DIV CLASS='nodata'>You do not have any contacts.</DIV></CENTER>";
}
?>
<DIV CLASS="edit">
<FORM NAME='contacts' method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<INPUT TYPE="HIDDEN" name="userdata" value="1">
<?php
if (!empty($Last))
echo "<INPUT TYPE=\"HIDDEN\" name=\"Last\" value=\"$Last\">";
?>
<TABLE CLASS="form" CELLSPACING="0" CELLPADDING="0">
<TR CLASS="bottom">
<TD>
<B>Last Name</b><br>
<INPUT CLASS="field" TYPE="text" name="Last" size="24" value='<?php echo $Last ?>' <?php echo empty($Last) ? '' : ' DISABLED ' ?> >
</TD>
<TD>
<B>First Name</b><br>
<INPUT CLASS="field" TYPE="text" name="First" size="24" value='<?php echo $First ?>'>
</TD>
<TD>
<B>Company</b><br>
<INPUT CLASS="field" TYPE="text" name="Company" size="24" value='<?php echo $Company ?>'>
</TD>
<TD>
<B>Phone Number</b><br>
<INPUT CLASS="field" TYPE="text" name="Phone" size="24" value='<?php echo $Phone ?>'>
</TD>
<TD>
<B>Website</b><br>
<INPUT CLASS="field" TYPE="text" name="Website" size="24" value='<?php echo $Website ?>'>
</TD>
</TR>
<TR>
<TD>
<INPUT TYPE="submit" value="Save">
<?php
if (!empty($Last))
echo "<INPUT TYPE=\"BUTTON\" value=\"Cancel\" onClick=\"history.go(-1);\">";
?>
</TD>
</TR>
</TABLE>
</FORM>
</DIV>
<DIV CLASS="content">
<?php
# if we are not editing a record, list the data
if (!isset($_GET['changeid'])) {
listData($data);
}
?>
</DIV>