user1932612
user1932612

Reputation: 1

php and mysql variables

i have a web called Infomundo and under the site i have a problem with php:

$c=1;
  while($c!=17)
  { $fecha_semana$c=$_POST['fecha_semana$c'];
    $interes_semana$c=$_POST['interes_semana$c'];
    $capital_semana$c=$_POST['capital_semana$c'];
    $recargos_semana$c=$_POST['recargos_semana$c'];
    $iva_semana$c=$_POST['iva_semana$c'];
    $pagado_semana$c=$_POST['pagado_semana$c'];
    $c=$c+1;
  }

but the variables $fecha_semana$c, $interes_semana$c, etc. are wrong how can i fix it?

Upvotes: 0

Views: 60

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173562

You're using single quotes in the array dereference:

$_POST['fecha_semana$c'];

That will not evaluate the value of $c; use double quotes:

$_POST["fecha_semana$c"];

See also: string

Additionally, you need to use variable variables for the left hand of the assignment:

${"fecha_semana$c"} = $_POST["fecha_semana$c"];

Update

This problem would be easier if you'd use array syntax in your form fields:

<input name="fecha_semana[]" value="123" />
<input name="fecha_semana[]" value="456" />
<input name="fecha_semana[]" value="678" />

When that gets posted, you will have an array in PHP:

print_r($_POST['fecha_semana']);
// ["123", "456", "678"]

Upvotes: 2

Marshmellow1328
Marshmellow1328

Reputation: 1265

As an alternative option to Jack's solution, you can use concatention:

$_POST['fecha_semana'.$c];

I personally prefer concatenation as it is easier for me to see where variables are being used, but I would say it is largely a matter of preference.

Upvotes: 0

Related Questions