Question

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!!

Was it helpful?

Solution

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>';
?>

OTHER TIPS

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.

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!

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

The eval function will do what you want.

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

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

Variable Variables will do what you want..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top