Sure:
$type = "debt"; //can be others but one is enough
// here is the code that generates the array
if (!session_is_registered($type)) {
session_register($type);
}
$key = count($_SESSION[$type]);
$_SESSION[$type][$key]['creditor'] = $_POST['creditor'];
$_SESSION[$type][$key]['balance'] = preg_replace("![^0-9\.]+!s", "", $_POST['balance']);
$_SESSION[$type][$key]['payment'] = preg_replace("![^0-9\.]+!s", "", $_POST['payment']);
$_SESSION[$type][$key]['current'] = $_POST['current'];
$_SESSION[$type][$key]['joint'] = $_POST['joint'];
/* this will register the session, if it doesn't already exist and generate the array $_SESSION['debt'][0].
as the user adds more entrys, [0] will increment accordingly.
*/
/* now that there are some entries in the array $_SESSION['debt'], I want to list them.
this will create a select field that contains the entries, so far.
This part partially works. the select box is generated and the items are listed.
*/
function showCreditor($type)
{
echo ("<table align=\"center\">\n<tr>\n<td align=\"center\">\n<form action=\"creditor.php?type=$type\" method=\"POST\">\n");
echo ("<select name=\"creditors\" size=\"5\">\n");
if (!$_SESSION[$type]) {
echo (" <option>Add a creditor</option>\n");
} else {
$creds = $_SESSION[$type];
foreach ($creds as $k) {
$name = $k['creditor'];
$balance = $k['balance'];
echo (" <option value=\"$k\">" . $name . " -- $" . $balance . "</option>\n");
}
}
echo ("</select>\n<br>\n<input name=\"action\" type=\"submit\" value=\"add\"> ");
if (!$_SESSION[$type]) {
echo ("\n</form>");
} else {
echo ("<input name=\"action\" type=\"submit\" value=\"delete\">\n</form>\n");
}
echo ("</td>\n</tr>\n</table>\n");
}
showCreditor();
as you can see in the function above, the value in each <option> tag, should be set to the array element 0, 1, 2, 3, etc. as the loop continues until it runs out of elements. However, when I view source on the output, it shows:
<option value="Array">
This is very simplified. None of the field validation is listed, just the items that apply to my problem... I hope. 😉