ansible Posted February 25, 2007 Share Posted February 25, 2007 Beginner here, trying to learn the basics of creating and handling arrays (which seems to be pretty tough for me). Anyway, I decided to practice by making a simple budget program. I'm starting by trying to get the program to let the user enter in expenses (as many as they want to). So far I've gotten it to take form data for an expense, store it into an array, and then print out the results... BUT when you try to enter a 2nd expense, the original array is lost and overwritten by the 2nd expense data from the form. How can I get it to append to the array instead of overwriting it? Or maybe my idea won't even work, and I need to figure out a way for it to make a new array for each expense? I'm really confused about arrays in general.. any help at all would be appreciated! Thanks. The code is below: <?php if(!isset($expenseNum)) { $expenseNum = 1;} echo"<h2>Budget Program</h2> <h3>Enter expense number $expenseNum</h3>"; $expenseNum++; echo"<form method = \"post\" action = \"budget.php\"> <select name=\"expenses[]\"> <option value=\"monthly\">Monthly </option> <option value=\"yearly\">Yearly</option> <option value=\"weekly\">Weekly</option> </select> - Time Period<br> <input type=\"text\" size=\"10\" name=\"expenses[]\"> - Expense Name<br> <input type=\"text\" size=\"10\" name=\"expenses[]\" value=\"$\"> - Expense Amount<br> <br><input type = \"submit\" value=\"Add Expense\"> <input type = \"hidden\" name=\"add\" value=\"add\"> <input type = \"hidden\" name=\"expenseNum\" value=\"$expenseNum\"> </form>"; if(isset($add)) { foreach($expenses as $values) { echo"$values<br>\n"; } } ?> Link to comment https://forums.phpfreaks.com/topic/40025-solved-very-simple-budget-program/ Share on other sites More sharing options...
Barand Posted February 25, 2007 Share Posted February 25, 2007 See my reply here: http://www.phpfreaks.com/forums/index.php/topic,128532.0.html Link to comment https://forums.phpfreaks.com/topic/40025-solved-very-simple-budget-program/#findComment-193582 Share on other sites More sharing options...
ansible Posted February 25, 2007 Author Share Posted February 25, 2007 Barand, Thanks for your reply, but I don't think your solution works for the problem I'm having. The code I posted already deals with multiple fields, passing the fields in arrays... and prints them out successfully. My question is really how to append to an existing array, not overwrite it. The sample code from your post also overwrites the array when you submit the form a 2nd time. Sorry to be a pain, and sorry I've I'm completely wrong about your solution not working! Link to comment https://forums.phpfreaks.com/topic/40025-solved-very-simple-budget-program/#findComment-193585 Share on other sites More sharing options...
ansible Posted February 25, 2007 Author Share Posted February 25, 2007 Just fixed my own problem. Notice the use of implode() and explode(), and also the use of array_merge. <?php if(!isset($expenseNum)) { $expenseNum = 1;} if(isset($arrayString)) { $allExpenses = explode("|", $arrayString);} echo"<h2>Budget Program</h2> <h3>Enter expense number $expenseNum</h3>"; $expenseNum++; echo"<form method = \"post\" action = \"budget.php\"> <select name=\"expenses[]\"> <option value=\"monthly\">Monthly </option> <option value=\"yearly\">Yearly</option> <option value=\"weekly\">Weekly</option> </select> - Time Period<br> <input type=\"text\" size=\"10\" name=\"expenses[]\"> - Expense Name<br> <input type=\"text\" size=\"10\" name=\"expenses[]\" value=\"$\"> - Expense Amount<br> <br><input type = \"submit\" value=\"Add Expense\"> <input type = \"hidden\" name=\"add\" value=\"add\"> <input type = \"hidden\" name=\"expenseNum\" value=\"$expenseNum\"><br><br>"; if(isset($add)) { foreach($expenses as $value) { echo"$value<br>\n"; } if(isset($allExpenses)) { foreach($allExpenses as $value) { echo"$value<br>\n"; } } $mergedExpenses = array_merge($expenses, $allExpenses); $arrayString = implode("|", $mergedExpenses); echo"<input type = \"hidden\" name=\"arrayString\" value=\"$arrayString\"></form>"; } ?> Link to comment https://forums.phpfreaks.com/topic/40025-solved-very-simple-budget-program/#findComment-193596 Share on other sites More sharing options...
ansible Posted February 25, 2007 Author Share Posted February 25, 2007 I forgot to add that the important thing in passing arrays through hidden form fields, is that it can't be done unless the array is first converted into string format. I used implode () to convert the array into a string separated by |'s, and then explode () on the next page to get the array back. At that point you can append to the array without overwriting it. Link to comment https://forums.phpfreaks.com/topic/40025-solved-very-simple-budget-program/#findComment-193750 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.