Jump to content

Help With foreach loop


limitphp

Recommended Posts

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

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";
   }
}

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.