Looking for the correct Syntax?

I would like to have the $x change as the value of x changes in the loop. What is the correct syntax to do this?

for ($x = 0; $x <= 3; $x++) {?>
    <input type="hidden" name="a_line_array1['.$x.']" value="<?=$v_resource_id; ?>"/>
    <input type="hidden" name="a_line_array1['.$x.']" value="<?=$v_category_name; ?>"/>
    <input type="hidden" name="a_line_array1['.$x.']" value="<?=$v_website_name; ?>"/>
    <input type="hidden" name="a_line_array1['.$x.']" value="<?=$v_webaddress; ?>"/>
<?php 
} 
?>

Thanks

Is that a typo? Should that be $v?

I would like to have the $x change as the value of $x changes in the loop. What is the correct syntax to do this?

$v_ is part of the variable's name
1 Like

The for loop should increment $x by one each time.
But you could try this syntax instead:-

for ($x = 0; $x <= 3; $x++) : ?>
    <input type="hidden" name="a_line_array1['.$x.']" value="<?=$v_resource_id; ?>"/>
    <input type="hidden" name="a_line_array1['.$x.']" value="<?=$v_category_name; ?>"/>
    <input type="hidden" name="a_line_array1['.$x.']" value="<?=$v_website_name; ?>"/>
    <input type="hidden" name="a_line_array1['.$x.']" value="<?=$v_webaddress; ?>"/>
<?php endfor ?>

What does the : mean?

http://php.net/manual/en/control-structures.alternative-syntax.php

Thank you with being patient with and I will test this out later and post the result for other to learn from

You are using $x right here but are not in PHP mode. If you intended to show the number, you’ll need those short open tags here too. ['<?=$x;?>']

1 Like

Totally missed that. :blush:

There’s also one more problem because all the input fields in each loop iteration have the same name, which means each one will overwrite another and only one will be sent to the server - probably the last one. So you need to use a different name for each input.

2 Likes

Maybe try using PHP heredoc

$v_resource_id   = '$v_resource_id';
$v_category_name = '$v_category_name';
$v_website_name  = '$v_website_name';
$v_webaddress    = '$v_webaddress';
$a_line_array1   = array( 'John', 'Jack', 'Fred', 'Harry', 'Charles' );


for ($x = 0; $x <= 3; $x++) : 

    $tmp = <<< ____TMP
        <br>  
	<label> {$a_line_array1["$x"]} </label>
        <br> 
	<input type="text" name="{$a_line_array1[$x]}" value="$v_resource_id$x"  />
        <br> 
	<input type="text" name="{$a_line_array1[$x]}" value="$v_category_name$x" />
        <br> 
	<input type="text" name="{$a_line_array1[$x]}" value="$v_website_name$x" />
        <br> 
	<input type="text" name="{$a_line_array1[$x]}" value="$v_webaddress$x" />
        <br> 
____TMP;
echo $tmp;

endfor; 

Code that I am now using:

for ($x = 1; $x <= 1; $x++) { ?>
<input type="hidden" name="a_line_array1['<?=$x;?>']" value="<?=$v_line_array1; ?>"/>
<?php }?>

OK this looks good going into the post array. Now how to I get a value out?

$a=$_POST['a_line_array1']['1'];

Getting “$a” as a Undefined variable
When I use print_r($_POST);
I see the following:
Array ( [a_line_array1] => Array ( [‘1’] => one ) [submit] => Submit )
The value would be (“one”)

I think it would be a lot easier if you explain what you are trying to achieve rather than supply script with errors.

Try this:

# $_POST = [];
echo '<form action="?" method="post">';
  for ($x = 0; $x <= 3; $x++) :
  	echo '<input type="text" name="a_line_array1[' .$x .']" value="' .$v_line_array1 .'_' .$x .'"/>';
  	echo '<br>';
  endfor;
  echo '<input type="submit" name="SUBMIT" value="Submit" />';
echo '</form>';
 
echo '<pre>';
  echo '$_POST => ';	
  print_r( $_POST );
echo '</pre>';

  	
  echo '$_POST["a_line_array1"] [0] ====> ';
  echo  $_POST['a_line_array1'] ['0'];
  echo '<br>';

  echo '$_POST["a_line_array1"] [1] ====> ';
  echo  $_POST["a_line_array1"] ["1"];
  echo '<br>';

  echo '$_POST["a_line_array1"] [2] ====> ';
  echo  $_POST["a_line_array1"] [2];
  echo '<br>';

  echo '$_POST["a_line_array1"] [3] ====> ';
  echo  $_POST["a_line_array1"] [3];
  echo '<br>';

### Output: ``` $v_line_array1_0 $v_line_array1_1 $v_line_array1_2 $v_line_array1_3 Submit $_POST => Array ( [a_line_array1] => Array ( [0] => $v_line_array1_0 [1] => $v_line_array1_1 [2] => $v_line_array1_2 [3] => $v_line_array1_3 )
[SUBMIT] => Submit

)
$_POST[“a_line_array1”] [0] ====> $v_line_array1_0
$_POST[“a_line_array1”] [1] ====> $v_line_array1_1
$_POST[“a_line_array1”] [2] ====> $v_line_array1_2
$_POST[“a_line_array1”] [3] ====> $v_line_array1_3

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.