CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2004
    Posts
    187

    Post 2 arrays and combine them

    I have a form like the following

    Code:
    <form action="" method="post" name="fcds">
    <table border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>TYPE</td>
        <td>PHONE</td>
      </tr>
      <tr>
        <td>
        <select name="myCheckbox[]" >
          <option value="">Selecione</option>
          <option value="1">Mobile</option>
          <option value="2">Job</option>
          <option value="3">Other</option>
          <option value="4" selected="selected">Home</option>
        </select>
    	</td>
        <td><input type="text" name="fone[]" /></td>
      </tr>
      <tr>
        <td><select name="myCheckbox[]" >
           <option value="">Selecione</option>
         <option value="1" selected="selected">Mobile</option>
          <option value="2">Job</option>
          <option value="3">Other</option>
          <option value="4">Home</option>
                </select></td>
        <td><input type="text" name="fone[]" /></td>
      </tr>
      <tr>
        <td><select name="myCheckbox[]" >
          <option value="">Selecione</option>
          <option value="1">Mobile</option>
          <option value="2" selected="selected">Job</option>
          <option value="3">Other</option>
          <option value="4">Home</option>
                </select></td>
        <td><input type="text" name="fone[]" /></td>
      </tr>
      <tr>
        <td><select name="myCheckbox[]" >
          <option value="">Selecione</option>
          <option value="1">Mobile</option>
          <option value="2">Job</option>
          <option value="3" selected="selected">Other</option>
          <option value="4">Home</option>
                </select></td>
        <td><input type="text" name="fone[]" /></td>
      </tr>
    </table>    
      <input value="ok" type="submit" /> 
    </form>
    I want the final result to be an array with only the not empty values posted like this:
    result[0] = type,fonevalue -> 1, 8765-8976
    result[1] = 2, 543-3654
    result[2] = 3, 342-3456

    I&#180;ve been using
    Code:
    foreach($_POST['myCheckbox'] as $type) {
      if($type<>'') echo $type;
    } 
    foreach($_POST['fone'] as $phone) {
      if($phone<>'') echo $phone;
    }
    But that just gives me the arrays separate.
    I want to combine them so I&#180;d have the type together with the corresponding phone value.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Post 2 arrays and combine them

    Maybe I'm misunderstanding, but your code is so close...

    PHP Code:
    $checkboxes = array();
    foreach (
    $_POST['myCheckbox'] as $index => $checkbox) {
      
    $checkboxes[$index] = $checkbox;
    }

    print_r($checkboxes); 
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Nov 2004
    Posts
    187

    Re: Post 2 arrays and combine them

    that just gives me Array ( [0] => 4 [1] => 1 [2] => 2 [3] => 3 ) but where are the phone values?
    See?

    I´d need something like this:

    Array ( [0] => 4, 8765-8976 [1] => 1, 333-2222 [2] => 2, 456-0987 )

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Post 2 arrays and combine them

    You have the $index...just add them in there.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Nov 2004
    Posts
    187

    Re: Post 2 arrays and combine them

    This was what I did:

    Code:
    	$checkboxes = array();
    	foreach ($_POST['myCheckbox'] as $index => $checkbox) {
    	  $checkboxes[$index] = $checkbox.",".$_POST['fone'][$index];
    	}
    	
    	print_r($checkboxes);
    Array ( [0] => 4,(33) 333 [1] => 1, [2] => 2,(11) 1111 [3] => 3, )

    I was still getting empty ones, so to avoid avoid them I did:
    Code:
    	$checkboxes = array();
    	foreach ($_POST['myCheckbox'] as $index => $checkbox) {
    	  if($_POST['fone'][$index]!='') $checkboxes[$index] = $checkbox.",".$_POST['fone'][$index];
    	}
    	
    	print_r($checkboxes);
    Array ( [0] => 4,(33) 333 [2] => 2,(11) 1111 )

    Instead of having [0] and [2] I&#180;d like to have [0] and [1]. How can I do that?
    Last edited by rogernem; January 20th, 2011 at 08:32 AM.

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: Post 2 arrays and combine them

    Sort the array or add a fake index scheme that only gets incremented when the value is not empty.

    And, you should be using !empty() instead of != ''.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Nov 2004
    Posts
    187

    Re: Post 2 arrays and combine them

    Thanks.

    This is my final code
    Code:
    	$checkboxes = array();
    	$i=0;
    	foreach ($_POST['myCheckbox'] as $index => $checkbox) {
    	  if(!empty($_POST['fone'][$index])){
    	  	 $checkboxes[$i] = $checkbox.",".$_POST['fone'][$index];
    	  	 $i++;
    	  }
    	}
    Array ( [0] => 4,(33) 333 [1] => 2,(11) 1111 )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured