limitphp Posted May 26, 2009 Share Posted May 26, 2009 I have multiple check boxes. <input name='c[]' type='checkbox' value='$id'> I grab the value of the selected check boxes with if ($_POST['mode']=='1') { foreach ($_POST['c'] as $value) { //do stuff here }; } its working great. I just need it to grab another value for me.....another id....a commentID I'm not sure how to do that. My first method: Would I make a hidden input in the form like: <input name='commentID[]' type='hidden' value='$songCommentID'> My second method: Could I just add a comma and the commentID value to the checkbox? <input name='c[]' type='checkbox' value='$id,$songCommentID'> Or is there a better easier method? With the first method, I don't know how I would grab the value of commentID. I would only want the value if the c[] checkbox is checked. Would I add another foreach loop inside the foreach ($_POST['c'] as $value loop? With the second method, how would I modify the foreach loop to grab both values? thanks.... Link to comment https://forums.phpfreaks.com/topic/159747-help-with-foreach-loop/ Share on other sites More sharing options...
Axeia Posted May 26, 2009 Share Posted May 26, 2009 The first method is better imo. I'd do something like: if( isset($_POST['commentID']) && isset($_POST['c']) ) { if( ($cap = count( $_POST['commentID'] )) == count( $_POST['c'] ) ) { for( $i = 0; $i < $cap; $i++ ) { echo $_POST['commentID'][$i]." <-related->".$_POST['c'][$i]; } } else { echo "Sorry invalid data"; } } Link to comment https://forums.phpfreaks.com/topic/159747-help-with-foreach-loop/#findComment-842585 Share on other sites More sharing options...
limitphp Posted May 26, 2009 Author Share Posted May 26, 2009 The first method is better imo. I'd do something like: if( isset($_POST['commentID']) && isset($_POST['c']) ) { if( ($cap = count( $_POST['commentID'] )) == count( $_POST['c'] ) ) { for( $i = 0; $i < $cap; $i++ ) { echo $_POST['commentID'][$i]." <-related->".$_POST['c'][$i]; } } else { echo "Sorry invalid data"; } } What is: if( ($cap = count( $_POST['commentID'] )) == count( $_POST['c'] ) ) for? Link to comment https://forums.phpfreaks.com/topic/159747-help-with-foreach-loop/#findComment-842609 Share on other sites More sharing options...
Ken2k7 Posted May 26, 2009 Share Posted May 26, 2009 To check if the number of commentIDs equal the number of 'c's otherwise there will be an issue with $_POST['c'][$i]. I guess they don't have to be equal. As long as commentIDs is smaller than or equal to cs, it should work. Link to comment https://forums.phpfreaks.com/topic/159747-help-with-foreach-loop/#findComment-842612 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.