1

I have this code that generates an HTML table with php:

<?php 
include("numbers2.php");

echo '<table border="1">';
 echo '<tr>';

for ($i = 1; $i <= 9; $i++) {
if($a1_pos_txt !== TRUE) {

echo "<td>" . $numbers["a" . $i . "_pos"] . "</td>";

} else {?> 
<?php 
echo '<td><input type="text" name="a' . $i . '_post" size="1" maxlength="1" /></td>';
?>
<?php } }?>

What I need to do is modify the $a1_post_txt variable so that it when the foor loops I will get instead of $a1_pos_txt every time:

$a1_pos_txt
.
.
$a9_pos_txt

I it basically what I did with $numbers["a" . $i . "_pos"] and with name="a' . $i . '_post" but now that the variable is inside another variable I don´t know how to do this.

I hope it is clear enough, if no please ask for any clarifications needed.

Thanks in advance!!

Trufa
  • 39,971
  • 43
  • 126
  • 190

6 Answers6

2

Instead of variable use array. In your array will contain values like true or flase, which were earlier in $a1_pos_txt......$a9_pos_txt

$arrOfValues[1] = TRUE;
$arrOfValues[2] = FALSE;
 ......
 .....
 ...
$arrOfValues[9] = TRUE;

So code will look like this

<?php 
include("numbers2.php");

 echo '<table border="1">';
  echo '<tr>';

for ($i = 1; $i <= 9; $i++) {
if($arrOfValues[$i] !== TRUE) {

 echo "<td>" . $numbers["a" . $i . "_pos"] . "</td>";

} else {?> 
<?php  
echo '<td><input type="text" name="a' . $i . '_post" size="1" maxlength="1" /></td>';
?>

Poonam Bhatt
  • 10,154
  • 16
  • 53
  • 72
1

You can use dynamic variables:

<?php
    $test = 'a';
    echo ${'test'}; 
?>

This will display "a".

So you can build a variable name as string and get its value!

MatTheCat
  • 18,071
  • 6
  • 54
  • 69
  • Yes I gues I could but I cant figure out how to do this for "part of a variable" $a1_post_txt (only changing the "1") – Trufa Nov 19 '10 at 13:34
1

Someone suggested variable variable -- they're awful! Don't use them! (they can make your code very hard to read and maintain, and have the potential to introduce security issues).

Someone else suggested using eval() -- definitely don't use that!! (using eval is considered very poor practice in virtually every possible situation; it is highly likely to introduce security issues)

Several people have suggested using an array instead of named variables -- this is the correct solution.

You already did it with $numbers, so could you do something similar with the post variables?

If they're related to the HTML code you've got name="a' . $i . '_post" then you could change this code to post variables instead -- something like this:

name="a_post['.$i.']"

Then instead of having post variables named a1_post and a2_post, etc, you will have ones named a_post[1] and a_post[2] etc. It then becomes very easy to loop through them because they're an array.

Spudley
  • 166,037
  • 39
  • 233
  • 307
0

http://php.net/manual/en/function.eval.php

The eval function will do what you want.

Ian P
  • 12,840
  • 6
  • 48
  • 70
0

I guess you should replace your single variable with an array. I mean, instead of creating

$a1_pos_txt ... $a9_pos_txt

you should have an array where you can use something like:

if($myVar[$i] !== true) {...}

Where and how are you initializing your flag variables?

Charlie

Chemed
  • 51
  • 3
0

http://us2.php.net/manual/en/language.variables.variable.php

Variable Variables will do what you want..

Doon
  • 19,719
  • 3
  • 40
  • 44