dmacman Posted April 16, 2008 Share Posted April 16, 2008 Hi everyone, I am can do this with a one dim. array, but when I get back what I need, like this.. [Class] => Array ( [0] => Array ( [1] => 1 [2] => 2 ) [1] => Array ( [3] => 3 [4] => 4 ) [2] => Array ( [5] => 5 [6] => 6 ) ) I get messed up. With my other arrays, I would do this... foreach($_POST['Class'] $BallotID => as $ClassID) { //insert ratings data into MySQL $sql ="insert into classes"; $sql .="(BallotID, ClassID, Date)"; $sql .="values('$BallotID', '$ClassID', '$Date')"; if(!mysql_query($sql)) { echo 'insert into classes error<br><br>' . mysql_errno().'<br>'.mysql_error().'<br><br>'; } } But that of course, gets me only the last item chosen. I appreciate the help, Don Quote Link to comment https://forums.phpfreaks.com/topic/101414-foreach-loop-for-multidimensional-array/ Share on other sites More sharing options...
craygo Posted April 16, 2008 Share Posted April 16, 2008 Not sure what you want to do. Do you want to just echo the array result out or do you want to create a query to insert the data. Quote Link to comment https://forums.phpfreaks.com/topic/101414-foreach-loop-for-multidimensional-array/#findComment-518699 Share on other sites More sharing options...
dmacman Posted April 16, 2008 Author Share Posted April 16, 2008 I want to insert the data. What I need to get is: (and if I am totally wacko on this, please let me know, since I am not a multi dim guru). The BallotID was 1 (from the query before this, and I just need to use that for this insert, and not change it) And I need to insert the for the chosen checkboxes array index, and which ones they picked. so... BallotID ClassID ClassesID 1----------0--------1 1----------0--------2 1----------1--------3 1----------1--------4 1----------2--------5 1----------2--------6 And this is from the POST data of [Class] => Array ( [0] => Array ( [1] => 1 [2] => 2 ) [1] => Array ( [3] => 3 [4] => 4 ) [2] => Array ( [5] => 5 [6] => 6 ) ) Does that make sense? Thanks, Don Quote Link to comment https://forums.phpfreaks.com/topic/101414-foreach-loop-for-multidimensional-array/#findComment-518734 Share on other sites More sharing options...
Barand Posted April 16, 2008 Share Posted April 16, 2008 do you mean <?php $class = Array ( Array ( 1 => 1, 2 => 2 ), Array ( 3 => 3, 4 => 4 ), Array ( 5 => 5 , 6 => 6 ) ); $ballotid = 1; foreach ($class as $classID => $classesArray) { foreach ($classesArray as $classesID) { $sql = "INSERT INTO classes (ballot, classID, classesID) VALUES ($ballotid, $classID, $classesID)"; echo $sql, '<br>'; # mysql_query($sql); // commented to test } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/101414-foreach-loop-for-multidimensional-array/#findComment-518793 Share on other sites More sharing options...
dmacman Posted April 21, 2008 Author Share Posted April 21, 2008 Hi Barand, That worked great, but, for my use, I had to change it to: foreach ($_POST['Class'] as $ProductID => $classesArray) { foreach ($classesArray as $ClassID) { //insert ratings data into MySQL $sql ="insert into classes"; $sql .="(BallotID, ProductID, ClassID, Date)"; $sql .="values('$BallotID', '$ProductID', '$ClassID', '$Date')"; if(!mysql_query($sql)) { echo 'insert into classes error<br><br>' . mysql_errno().'<br>'.mysql_error().'<br><br>'; } { { But the question I have is, the array of course, start with zero, as they all do. I need the $ProductID to start with 1, how would I do that in this instance? Thanks, Don Quote Link to comment https://forums.phpfreaks.com/topic/101414-foreach-loop-for-multidimensional-array/#findComment-522946 Share on other sites More sharing options...
Barand Posted April 21, 2008 Share Posted April 21, 2008 I don't which index is $product, but if you want an array to start at 1 <?php $months = array (1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'etc'); echo $months[4]; // Apr Quote Link to comment https://forums.phpfreaks.com/topic/101414-foreach-loop-for-multidimensional-array/#findComment-522969 Share on other sites More sharing options...
dmacman Posted April 21, 2008 Author Share Posted April 21, 2008 I get what your saying, but that is if I am defining the array. This array is from $_POST['Class'] data from the previous page, which is set to zero by default. So how can I have it start at 1 or at least, add 1 to all the indexes in the loop. Or is that too tough to do? Thanks, Don Quote Link to comment https://forums.phpfreaks.com/topic/101414-foreach-loop-for-multidimensional-array/#findComment-522995 Share on other sites More sharing options...
Barand Posted April 21, 2008 Share Posted April 21, 2008 try <?php if (isset($_POST['btn'])) { $classes = array_merge(array('dummy'), array_values($_POST['class'])); // put dummy value in pos 0 unset($classes[0]); // remove dummy foreach ($classes as $k => $v) { echo "$k $v <br>"; // remaining array starts at 1 } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/101414-foreach-loop-for-multidimensional-array/#findComment-523016 Share on other sites More sharing options...
dmacman Posted April 21, 2008 Author Share Posted April 21, 2008 Thanks barand, I will put your fix in my saved code library. I made a stupid mistake. I forgot to go back and get the ProductID from the first form from the database and I had hard-coded it with the ID's starting at 0 <input name="Products[0]" //etc And I went back here and changed it to get the data from the table, and now I am in sync (starting with 1 as I should be. But you fix is something I can still use in another thing I was thinking of, thanks. I owe you one. regards, Don Quote Link to comment https://forums.phpfreaks.com/topic/101414-foreach-loop-for-multidimensional-array/#findComment-523030 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.