Humpty Posted April 10, 2006 Share Posted April 10, 2006 G'day Oh great PHP'ers,I am having issus with arrays of Radio Buttons and passing the data throughHere is the code, I think it is pretty straight forward what I am doing but hey you never know. Let me know if you need more details:This is the Form: (well the important part...the form works fine and the data is being passed I just can't access it to work with it in PHP but I can view it by using print_r($_REQUEST); and it is all there correctly)[code] $i=0;$totalnum=5;while ($i < $totalnum) {?> <tr class="AdminProductDetails"> <td>Status:</td> <td><input name="mkPerform<? echo "$i" ?>[]" type="radio" value="Active"> Make Active </td> <td><input name="mkPerform<? echo "$i" ?>[]" type="radio" value="Inactive"> Make Inactive </td> <td><input name="mkPerform<? echo "$i" ?>[]" type="radio" value="Delete"> Delete For Good </td> <td><input name="mkPerform<? echo "$i" ?>[]" type="radio" value="DoNothing" checked> Do Nothing </td> </tr> <? $i++;}?>[/code]This is the PHP that the form posts to:[code]$i=0;$totalnum=5;while ($i < $totalnum) {$Frankie = serialize($_POST['mkPerform' . $i]);echo "<BR><BR> $Frankie";$i++;}[/code]This is the output to the page: (ugly)[code]a:1:{i:0;s:6:"Active";}a:1:{i:0;s:8:"Inactive";}a:1:{i:0;s:6:"Delete";}a:1:{i:0;s:9:"DoNothing";}a:1:{i:0;s:6:"Active";}[/code]If I remove the serialize() command/funtion I get 5 lines of the word Array. What I want is the word or a string for Active, InActive, Delete or DoNothing.Any help greatly appreciated. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 10, 2006 Share Posted April 10, 2006 I believe this is what you're trying to do. I added the form tags and the submit button for my testing.[code]<?phpif (isset($_POST['submit'])) { echo '<pre>' . print_r($_POST,true) . '</pre>'; foreach ($_POST['mkPerform'] as $i => $status) echo 'Status ' . $i . ': ' . $status . '<br>';} ?><form action="<? echo $_SERVER['PHP_SELF']?>" method="post"><table><?php$totalnum=5;for ($i=0; $i<$totalnum;$i++) {?> <tr class="AdminProductDetails"> <td>Status:</td> <td><input name="mkPerform[<? echo "$i" ?>]" type="radio" value="Active"> Make Active </td> <td><input name="mkPerform[<? echo "$i" ?>]" type="radio" value="Inactive"> Make Inactive </td> <td><input name="mkPerform[<? echo "$i" ?>]" type="radio" value="Delete"> Delete For Good </td> <td><input name="mkPerform[<? echo "$i" ?>]" type="radio" value="DoNothing" checked> Do Nothing </td> </tr> <?}?></table><input type="submit" name="submit"></form>[/code]I made the name mkPerform into the array with the index of $i. For radio buttons you only need one array since you can only have one value per row. I also changed the "while" loop into a "for" loop.Ken Quote Link to comment Share on other sites More sharing options...
Humpty Posted April 10, 2006 Author Share Posted April 10, 2006 Ken, we meet again. ...and you certainly haven't gotten dumber...good work thanks.I don't understand the:[code]if (isset($_POST['submit'])) { echo '<pre>' . print_r($_POST,true) . '</pre>'; foreach ($_POST['mkPerform'] as $i => $status) echo 'Status ' . $i . ': ' . $status . '<br>';}[/code]But on this occasion, (i was just coming in to flag this as solved), I didn't need to do that. I am now dynamically changing the names themselves instead.as follows:[code] <td><input name="mkPerform<? echo "$i" ?>" type="radio" value="Active"> Make Active </td> <td><input name="mkPerform<? echo "$i" ?>" type="radio" value="Inactive"> Make Inactive </td> <td><input name="mkPerform<? echo "$i" ?>" type="radio" value="Delete"> Delete For Good </td> <td><input name="mkPerform<? echo "$i" ?>" type="radio" value="DoNothing" checked> Do Nothing </td>[/code]Which is working good for me, dunno if it is better / same / worse but it works.I do need to know how to do it the otherway though for other things in future.Could I have some sort of explanation or brake that code you gave into a more dumbified way of following it? I've never reapped my head arround for...each before in any BASIC language i've used or PHP. I can only follow a for each in the **SIMPLEST** terms possible.If you are too busy to explain it now that is fine I will ask agian when the time comes again. At least then I'd retain it too. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 10, 2006 Share Posted April 10, 2006 It's much easier to use arrays than to dynamically change the variable name. At least IMHO. Also, if you use an array here and you want to change how you display the results it is very easy to do. Let's say the original display is a column:[code]<?phpfor ($i=0;$i<count($_POST['mkPerform']);$i++) echo $_POST['mkPerform'].'<br>';?>[/code]and you want to display it as a comma seperated list, you would simply do [code]<?php echo implode(', ',$_POST['mkPerform']) ?>[/code]With dymanically generated variables, the first is almost the same, but the second is much harder. Whenever I do a script, I always look to see if there is a way I can make PHP do the work instead of me. :-) Most of these techniques involve the use of arrays in one way or another.Ken 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.