viion Posted April 1, 2009 Share Posted April 1, 2009 I need to make an array of $post values. I thought something like $value[$i] = $_POST['$checkboxName[$i]']; ...Would work, by putting it in a for loop. My code is: $x = 0; for (; ; ) { if ($x > "$possible_choice_array_totalx" -1) { break; } $result[$x] = $_POST['$possible_choice_arrayx[$x]']; echo "$x possible = $possible_choice_arrayx[$x] | $x choicex = $result[$x] <br />"; $x++; } Not working, any help on how to get a post array working. Quote Link to comment https://forums.phpfreaks.com/topic/152098-using-an-array-for-_post/ Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 Why not just use foreach to iterate through the $_POST array. As far as what you are trying to get it, I cannot see. But your for statement is not valid. I would look up at the manual for proper usage of that. Since it seems as though you have multiple check boxes here is an example: foreach ($_POST['checkboxesname'] as $key => $val) { echo "Checkbox: {$key} contains a value of {$val}<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/152098-using-an-array-for-_post/#findComment-798781 Share on other sites More sharing options...
manny Posted April 1, 2009 Share Posted April 1, 2009 try this it should work for ($x = 0; $x > ($possible_choice_array_totalx-1) ; $x++ ) { $result[$x] = $_POST["$possible_choice_arrayx[$x]"]; echo "$x possible = $possible_choice_arrayx[$x] | $x choicex = $result[$x] <br />"; } Let me know how it goes Quote Link to comment https://forums.phpfreaks.com/topic/152098-using-an-array-for-_post/#findComment-798784 Share on other sites More sharing options...
viion Posted April 1, 2009 Author Share Posted April 1, 2009 Hmm, neither worked. They didn't show a report. How do I post a PHP tag so it's coloured? All the check box's are different names. depending on their value. So the Name is exactly same as the value like: <input name='$v' type='checkbox' value='$v' /> $v I tried bit snippets and adjusted them accordingly but both do not post anything. When a check box is True. Quote Link to comment https://forums.phpfreaks.com/topic/152098-using-an-array-for-_post/#findComment-798791 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 Post a few checkboxes (the actual html) of your form, so we can see what you are working with. As for the pretty colors you can use without the <?php or with the <?php should work for coloring. Quote Link to comment https://forums.phpfreaks.com/topic/152098-using-an-array-for-_post/#findComment-798801 Share on other sites More sharing options...
viion Posted April 1, 2009 Author Share Posted April 1, 2009 Check box's are pulled from a function known as showChoices(); Which includes this: // The values for possible_choice_array and users_choice_array have already been determind and so the variables are ready. $qwert = array_values($possible_choice_array); $qwert2 = array_values($users_choice_array); foreach ($qwert as $v) { if (in_array("$v", $qwert2)) { echo "<input name='$v' type='checkbox' value='$v' checked/> $v"; } else { echo "<input name='$v' type='checkbox' value='$v' /> $v"; } } An example output would be: <input name="KirinsOsode" value="KirinsOsode" type="checkbox"> KirinsOsode <input name="Kirins Pole" value="Kirins Pole" type="checkbox"> Kirins Pole Then when the user hits submit it calls another function called editWishlist(); The code for this is so far: function editWishlist() { // Pull all SQL tables, the below values are ready to work. while ($possiblelistx = mysql_fetch_array($possible_choice_sqlx) ) { // PULL $possible_choicex = $possiblelistx[MONSTER_DATABASE_MOBDROP]; // SPLIT $possible_choice_arrayx = explode("|", $possible_choicex); // COUNT $possible_choice_array_totalx = count($possible_choice_arrayx); // For statements pasted/edit here and tested. // EG below for ($x = 0; $x > ($possible_choice_array_totalx-1) ; $x++ ) { $result[$x] = $_POST["$possible_choice_arrayx[$x]"]; if ($result[$x]=="True") { echo "$possible_choice_arrayx[$x] Selcted <br />"; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/152098-using-an-array-for-_post/#findComment-798804 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 Ok, well for one, the name has a space in it. That usually is not kosher. Second you could make it an array of checkboxes by doing this: foreach ($qwert as $v) { if (in_array("$v", $qwert2)) { echo "<input name='checkboxname[$v]' type='checkbox' value='$v' checked/> $v"; } else { echo "<input name='checkboxname[$v]' type='checkbox' value='$v' /> $v"; } } Then using this: foreach ($_POST['checkboxname'] as $key => $val) { echo "Checkbox: {$key} contains a value of {$val}<br />"; } Should give you an idea how to use that to do your tests. Quote Link to comment https://forums.phpfreaks.com/topic/152098-using-an-array-for-_post/#findComment-798809 Share on other sites More sharing options...
viion Posted April 1, 2009 Author Share Posted April 1, 2009 Okay that works =D, the outcome is rather odd though. It's rendering the results 5 times. So an outcome of select 3 box's would come out like this: Checkbox: KirinsOsode contains a value of KirinsOsode Checkbox: Neptunal Abjuration: Body contains a value of Neptunal Abjuration: Body Checkbox: Scroll of Raise III contains a value of Scroll of Raise III Checkbox: KirinsOsode contains a value of KirinsOsode Checkbox: Neptunal Abjuration: Body contains a value of Neptunal Abjuration: Body Checkbox: Scroll of Raise III contains a value of Scroll of Raise III Checkbox: KirinsOsode contains a value of KirinsOsode Checkbox: Neptunal Abjuration: Body contains a value of Neptunal Abjuration: Body Checkbox: Scroll of Raise III contains a value of Scroll of Raise III Checkbox: KirinsOsode contains a value of KirinsOsode Checkbox: Neptunal Abjuration: Body contains a value of Neptunal Abjuration: Body Checkbox: Scroll of Raise III contains a value of Scroll of Raise III Checkbox: KirinsOsode contains a value of KirinsOsode Checkbox: Neptunal Abjuration: Body contains a value of Neptunal Abjuration: Body Checkbox: Scroll of Raise III contains a value of Scroll of Raise III Quote Link to comment https://forums.phpfreaks.com/topic/152098-using-an-array-for-_post/#findComment-798822 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 It looks like it is just looping through the same checkboxes. Post your fully generated form code so we can look at it. Quote Link to comment https://forums.phpfreaks.com/topic/152098-using-an-array-for-_post/#findComment-798834 Share on other sites More sharing options...
viion Posted April 1, 2009 Author Share Posted April 1, 2009 If you go here: http://encorels.com/v2/?mode=u03character&id=963643388 Click "Kirin" then tick a few box's and then click Save Changes and you'll see the out come, you can view the source. Here is the full functions that control the checkbox and the submit: Showing Checkboxes: function showChoices() { global $possible_choice_array, $users_choice_array; $currentID = $_GET[id]; $users_choicex = mysql_query("SELECT ". CHARACTER_CONFIG_WISHLIST .", ". CHARACTER_CONFIG_OBTAINED ." FROM ". CHARACTER_DATABASE ." WHERE ". CHARACTER_CONFIG_ID ."='$currentID'"); if (!$users_choicex) { echo("<div id='error'><img src='img/icons/icon_cross.png'>Users wishlist could not be loaded: " . mysql_error() . "</div>"); } while ($prls = mysql_fetch_array($users_choicex)) { // PULL SQL $users_wishes = $prls[CHARACTER_CONFIG_WISHLIST]; $users_obtained= $prls[CHARACTER_CONFIG_OBTAINED]; // SPLIT SQL $users_choice_array = explode("|", $users_wishes); $users_obtained_array = explode("|", $users_obtained); // GATHER ARRAY VALUES FROM SPLIT $qwert = array_values($possible_choice_array); $qwert2 = array_values($users_choice_array); $qwert3 = array_values($users_obtained_array); foreach ($qwert as $v) { if (in_array("$v", $qwert3)) { echo "<img src='img/icons/icon_tick.png'> <strong>Obtained: $v!</strong>"; } elseif (in_array("$v", $qwert2)) { echo "<input name='checkbox[$v]' type='checkbox' value='$v' checked/> $v"; } else { echo "<input name='checkbox[$v]' type='checkbox' value='$v' /> $v"; } } } } On Submitting of form it calls this function: function editWishlist() { $currentID = $_GET[id]; // Pull Possible Choices and filter through which is true. $possible_choice_sqlx = mysql_query("SELECT * FROM ". MONSTER_DATABASE .""); if (!$possible_choice_sqlx) { echo("<div id='error'><img src='img/icons/icon_cross.png'>Could not load items from data base: " . mysql_error() . "</div>"); } while ($possiblelistx = mysql_fetch_array($possible_choice_sqlx) ) { // PULL $possible_choicex = $possiblelistx[MONSTER_DATABASE_MOBDROP]; // SPLIT $possible_choice_arrayx = explode("|", $possible_choicex); // COUNT $possible_choice_array_totalx = count($possible_choice_arrayx); foreach ($_POST['checkbox'] as $key => $val) { echo "Checkbox: {$key} contains a value of {$val}<br />"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/152098-using-an-array-for-_post/#findComment-798845 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 You have the foreach in the while. That is where you are getting the issue at. If you call $_POST['checkbox']['itemnamehere'] that will solve the problem. I am not sure how you get the checkbox name value but you can use that to pull out the item you want to update. Hope that makes sense, as I would not even know where to start on your code to show you how to implement it. Quote Link to comment https://forums.phpfreaks.com/topic/152098-using-an-array-for-_post/#findComment-798853 Share on other sites More sharing options...
viion Posted April 1, 2009 Author Share Posted April 1, 2009 the item names runs under a for. example: $possible_choice_arrayx[$i] will convert into w/e it is. The possible_choicex looks like this: item1|item2|item3|item4 the possible_choice_arrayx splits it in "|" which then creates the [1][2][3] etc array. I'm guessing I'd have to put the foreach in a for loop to generate the name? Maybe I can live with it for now as it'll just end up inserting the exact same information into the database. Quote Link to comment https://forums.phpfreaks.com/topic/152098-using-an-array-for-_post/#findComment-798860 Share on other sites More sharing options...
viion Posted April 1, 2009 Author Share Posted April 1, 2009 How do I make $val into a string variable. If i echo $val| and I have selected say 3 items, it would outcome it as: item1|item2|item3| but when I try SQL insert this, it doesnt. it just does item1| Quote Link to comment https://forums.phpfreaks.com/topic/152098-using-an-array-for-_post/#findComment-798887 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.