writer Posted April 3, 2008 Share Posted April 3, 2008 I need some help with multidimensional arrays. I'm currently working on a PHP application that will create and store something like Benjamin Franklin's "<a href="http://www.diyplanner.com/files/franklin_industry.gif">plans</a>" of his 13 virtues. I figure that the application database should pull from the following data structure: <img src="http://stanford.edu/~buley/crits/datahelp.jpg"> I have a GET form returning values from checkboxes, which take the following form: VirtueAbbrev_DayOfTheWeek For example, the form could return the following: <code>Te_Sunday: NO S_Monday: NO O_Tuesday: NO R_Wednesday: NO F_Thursday: NO I_Friday: NO</code> Using basic logic, if it's not a "NO" then the program assumes it s a "YES". (Small note here: Franklin started by using the reverse system, tallying his yes responses instead of penalizing his no responses. He later switched the system, surmising that he was being conditioned to game the system and reward himself with too many yes responses.) I have a <a href="http://taylorbuley.com/sandbox/franklin/virtues.php">basic form</a> that returns the values as formatted above. But I'm not sure on how to translate these values into multidemensional arrays necessary to create a MYSQL database as modeled above. If anyone could walk me through the general framework about how I would tackle this problem, or point to some resources that would set me on the right path, I would very much appreciate it! Quote Link to comment Share on other sites More sharing options...
devstudio Posted April 3, 2008 Share Posted April 3, 2008 There's always lots of ways to do things like this. The easiest way to get your input to build the proper multi-dimensional array is to name your inputs so that the submitted form data auto-generates an array. <input type="checkbox" name="Te[sunday]" value ="YES"> <input type="checkbox" name="Te[monday]" value ="YES"> <input type="checkbox" name="Te[...]" value ="YES"> <input type="checkbox" name="S[sunday]" value ="YES"> <input type="checkbox" name="S[monday]" value ="YES"> <input type="checkbox" name="S[...]" value ="YES"> When the data is submitted you will get an array like this: [use: <?php print("<pre>"); print_r($_REQUEST); print("</pre>"); ?> to see it] Array ( [Te] => Array ( [monday] => YES ) [s] => Array ( [sunday] => YES ) ) *NOTE:* You will only get "YES" values for checked items. Now for the database component, I have no idea how much help you need with this, but to point you in the right direction ... You can add another layer to your array. Example: <input type="checkbox" name="formData[Te][sunday]" value ="YES"> <input type="checkbox" name="formData[Te][munday]" value ="YES"> etc... Then you can loop through your new array: foreach($_REQUEST[formData] AS $virtueTitle => $weekDayArray) { foreach($weekDayArrray AS $dayOfWeek => $value) // Insert into database: $dayOfWeek, $value } } *NOTE: Also you can just serialize() the $_REQUEST[formData] array and insert that into the database and then unserialize() when you pull it back out. Finally, when you pull it back out, you can pre-fill the form easily (you will want to add a $checkedItems print to your input): /* $formData is what you got from the database, you can rebuild a multi-dimensional array from the database, or if you go the serialization route, you can just use the results from unserialize */ foreach($formData AS $virtueTitle => $weekDayArray) { foreach($weekDayArrray AS $dayOfWeek => $value) $checkedItems[$virtueTitle][$dayOfWeek] = "CHECKED"; } } <input type="checkbox" name="formData[Te][sunday]" value ="YES" <?=$checkedItems[Te][sunday] ?>> <input type="checkbox" name="formData[Te][sunday]" value ="YES" <?=$checkedItems[Te][monday] ?>> <input type="checkbox" name="formData[Te][sunday]" value ="YES" <?=$checkedItems[Te][...] ?>> For even more fun, you can build a multidimensional array that defines the grid. Once the grid is defined all of your inputs can be built dynamically. $gridData[Te][sunday] = 1; $gridData[Te][monday] = 1; $gridData[Te][...] = 1; $gridData[s][sunday] = 1; $gridData[s][...] = 1; Then: <form> <table> <!-- Insert your code for Table Headings ** Not going to write the whole thing for you! --> <?php foreach($gridData AS $virtueLabel => $dayOfWeekArr) { print("<tr>"); print("<td>$virtueLabel</td>"); foreach($dayOfWeekArr AS $dayOfWeek => $trash) { print("<td><input type=\"checkbox\" name=\"formData[$virtueLabel][$dayOfWeek]\" value=\"YES\" ".$checkedItems[$virtueLabel][$dayOfWeek]."></td>"); } print("</tr>"); } ?> </form> </table> *phew* Didn't Expect to write a book! - Questions? Quote Link to comment Share on other sites More sharing options...
writer Posted April 4, 2008 Author Share Posted April 4, 2008 *phew* Didn't Expect to write a book! - Questions? No questions so far -- just a statement: Thank you! I really appreciate your time. Thanks to devstudio's insightful advice and some mentoring from a friend, I came up with the following loops to create the array I was looking for, returning a format similar to the suggestions above, while still creating the checkboxes for the form. for($i=1;$i<8;$i++) { $days[date($i)] = $ar_time[$i];} for ($k = 1; $k < 8; $k++) { $comb = "$virtue".'['."$days[$k]"."]"; echo "<td><input name='".$comb."' type='checkbox' value='GIG'></td>\n"; } echo "</tr>\n"; } As you can test out yourself here, this produces a $_POST array in the following format (again, similar to the formatting devcenter suggested). Array ( [Te] => Array ( [1206946800] => GIG ) => Array ( [1207033200] => GIG ) [O] => Array ( [1207119600] => GIG ) [R] => Array ( [1207206000] => GIG ) [submit] => Submit ) (NB: Franklin used a reverse system, noting deviances from the virtues rather then their successful execution. So that's what the GIG is: it comes from the Army, and basically means missing the mark. The numbers are unix timestamps based on the first day of the week.) THE NEW PROBLEM: I've got everything passing correctly into the process.php file, but unfortuantely I am not able to store the values in my MYSQL database -- the query is returning blank variables. I think the problem is in the FOR loops, which have to process the multidimensional $_POST array I created. I thought at first that it was a problem with referencing the variables, but since the query command is within the loops, and since they're arrays and not vars, that shouldn't be a problem. Here's the process.php file, which contains both the query string and the for loops I used to process the multidimensional arrays: <?php include 'config.php'; $connect = mysql_connect($host, $user, $pass); mysql_select_db($db) or die ('Could not connect to the database.'); $query = "INSERT INTO virtuevalues (virtueValuesID, virtueValuesDate) VALUES ($gig, $current_date)"; $query2 = "INSERT INTO virtues (virtuesID) VALUES ($key)"; if ($_SERVER['REQUEST_METHOD'] == 'POST') { // handle a POST request } else { die("You may only POST this page."); } if($_POST) { echo "<h3>Post data</h3>"; print_r($_POST); echo "<hr>"; } foreach($_POST as $key => $val) { if($key = "submit") {break;} if($key != "submit") { foreach($val as $date => $gig) { mysql_query($query) or print "First: " and die(mysql_error()); mysql_query($query2) or print "Second: " and die(mysql_error()); } } } ?> What's going on here? Can anyone tell me why my arrays are shooting blanks? --- PS: For even more fun, you can build a multidimensional array that defines the grid. Once the grid is defined all of your inputs can be built dynamically. I have no idea what this means. Quote Link to comment Share on other sites More sharing options...
devstudio Posted April 4, 2008 Share Posted April 4, 2008 foreach($_POST as $key => $val) { if($key = "submit") {break;} if($key != "submit") { foreach($val as $date => $gig) { mysql_query($query) or print "First: " and die(mysql_error()); mysql_query($query2) or print "Second: " and die(mysql_error()); } } } if($key = "submit") {break;} - Will always evaluate to true because you are using an assignment operator (successfully, which returns true/1). if($key == "submit") {break;} As far as what you didn't understand. Sounds like you ended up implementing it in the end anyways. Best, Nathan Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.