scl
scl

Reputation: 93

array blowing my mind

$va_fields = array();
 $c = 0;
$field_num = 'field-'.$c;
settype($c,"integer");

while (isset($_POST[$field_num])){
    $posted_field = $_POST[$field_num];
    $va_fields = array( $c => $posted_field);
    echo $c.': ';
    echo '$posted_field: '.$posted_field;
    $c+=1;
    $field_num = 'field-'.$c;

    echo ' <br /> va_fields - c:'.$va_fields[$c];
    echo ' <br /> ';

}

For some reason, I cannot for the life of me get the variable $posted_fields into an array.

The values of $posted_field are what I need them to be, so I'm getting the data from the post. Then I try to store them in an array, and check the array and they aren't there. What am I doing wrong?

Edit: here's my form:

      <form method="post" action="<?php echo get_site_url(); ?>/wp-admin/admin-post.php">
        <input type="hidden" name="action" value="cpt_field_opts" />
     <!-- some inputs here ... -->
     <?php wp_nonce_field( 'cpt_field_opts', '_wp_nonce' ); ?>
     <input type="hidden" name="post_type" value="<?php echo $post_type; ?>">
     <input type="hidden" name="origin" value="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>" />
<?php
    $c = 0;
    foreach ($fields as $field){
         ?>
    <input type="textarea" id="field-<?php echo $c; ?>" name="field-<?php echo $c; ?>" value="<?php echo $field; ?>" size="25" /></br>
        <?php
        $c += 1;
        }
?>
    <input type="submit" value="Submit" >
</form>
<form method="post" action="<?php echo get_site_url(); ?>/wp-admin/admin-post.php">
         <?php wp_nonce_field( 'cpt_field_opts_new', '_wp_nonce_2' ); ?>
    <input type="hidden" name="post_type" value="<?php echo $post_type; ?>" />
            <input type="hidden" name="action" value="cpt_field_opts_new" />
            <input type="hidden" name="origin" value="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>" />
    <input type="submit" value="New" />
</form>

Many people are telling me to rewrite the array as this line:

    $va_fields[$c] = $posted_field;

Which I have done, but I also started out with that and it still wasn't working

Upvotes: 0

Views: 60

Answers (2)

Joy Biswas
Joy Biswas

Reputation: 6527

It is simple just iterate the $_POST array

<?php
  $va_fields = array();
  $c=0;      
  foreach($_POST as $val){
    echo '$posted_field: '. $val;
    $va_fields[$c] = $val;
  }
  var_dump($va_fields);
?>

Upvotes: -1

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

If you have a bunch of values in $_POST with names like "field-0", "field-1", etc, and you're trying to get them into an array:

$c = 0;
while (isset($_POST["field-$c"])){

    // This creates a new element in $va_fields with key=$c and value=$_POST["field-$c"]
    $va_fields[$c] = $_POST["field-$c"];

}

var_dump($va_fields); // should show you the array you want

But really, it would be easier to just modify your form to use inputs with name=field[] instead of numbering them. Then you would already have the array in $_POST['fields'] without having to do anything.

Upvotes: 2

Related Questions