1

i have following while

while($rs=mysqli_fetch_array($row))
{
 }

in each while execution there is one column which will have some ID lik 5 , 6 7, 8

ID  Name    Intake  division_no      
1   5       1       2    
2   6       4       5

i have some varible name like txtin5, txtin6

while looping in while i want to automatic assign intake value to that variable for ex in this case

        $txtin5 = 1
        $txtin6 = 4

i used this

      $spvar = "txtin" ;
 $i=5;
    while {
    $$spvar.$i = rs['Intake']; 
 $i++;
    }  

i know this is wrong , but anyone could tell me the right way

2 Answers 2

2

Here's how you do variable variables in PHP:

${$spvar.$i} = $rs['Intake'];

However, I recommend that you avoid the need for this. Instead of variables $txtin5 and $txtin6, why not make $txtin be an array, so you can use $txtin[$i]?

Sign up to request clarification or add additional context in comments.

1 Comment

it worked!!! thanks !!! actually that variable will be fixed 5 to 12, and there some other task that was based on var not arr
0

You can use arrays instead of what you did. Handling array is quite simple and easy. SO your code will look like:

    while {
    $spvar[] = rs['Intake']; 
     }  

ANd if you want to have a associative array you can use:

   $i=5;
    while {
    $spvar[$i] = rs['Intake']; 
 $i++;
    } 

1 Comment

yes i know i can use array , but the fact is my next code was depend on variables. bye the way thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.