-1

I face a problem about the following code.

<?php 
$sql="SELECT * FROM `question` WHERE sub_id=$subject_id";
$query=mysql_query($sql);
$question_count=mysql_num_rows($query);
for($a=1;$a<=$question_count;$a++){
    $ans{$a}=$_POST["ans$a"];
    $subject_id{$a}=$_POST["sub_id$a"];
    $group_id{$a}=$_POST["grup_id$a"];  
}

?>

I want to use $ans{$a}, $subject_id{$a}, $subject_id{$a} etc variable outside for loop. If I use those like following code,

<?php 
$sql="SELECT * FROM `question` WHERE sub_id=$subject_id";
$query=mysql_query($sql);
$question_count=mysql_num_rows($query);
for($a=1;$a<=$question_count;$a++){
    $ans{$a}=$_POST["ans$a"];
    $subject_id{$a}=$_POST["sub_id$a"];
    $group_id{$a}=$_POST["grup_id$a"];
}

    echo $ans1;
    echo $subject_id1;
?>  

php says that:

Notice: Undefined variable: ans1 in F:\xampp\htdocs\oes\Student\result_process.php on line 25.

please help me give some advice to use these variable outside for loop scope.

t.niese
  • 32,069
  • 7
  • 56
  • 86
Sifat080
  • 15
  • 6
  • I haven't used such constructs as I think it is bad practice to use them. But shouldn't it be `${"ans".$a}` instead of `$ans{$a}` – t.niese Jun 15 '14 at 14:39
  • In your loop, do you mean to write: `${'ans' . $a}`? In other words dynamically creating a variable, such as `$ans1`? – bloodyKnuckles Jun 15 '14 at 14:40
  • yes i want it. that's variable dynamically create $ans1. i will try your suggestion. – Sifat080 Jun 15 '14 at 14:50

2 Answers2

0

Possible variable $question_count can be 0. Otherwise try to use arrays:

<?php 
$sql="SELECT * FROM `question` WHERE sub_id=$subject_id";
$query=mysql_query($sql);
$question_count=mysql_num_rows($query);
$items = array();

for($a=1;$a<=$question_count;$a++){
    $items[$a] = array(
       'ans' => $_POST["ans$a"],
       'subject_id' => $_POST["sub_id$a"],
       'group_id' => $_POST["grup_id$a"]
    );
}

echo $items[1]['ans'];
echo $items[1]['subject_id'];
?> 
  • Even if `$question_count` would never be `0`, `$ans1` would never be set with the code given by the OP as `$ans{$a}` is equal to `$ans[$a]`. – t.niese Jun 15 '14 at 14:59
0

The problem with your code is that $ans{$a} is equal to $ans[$a] (PHP curly braces in array notation)

So your loop does this:

 for($a=1;$a<=$question_count;$a++){
     $ans[$a]=$_POST["ans$a"];
     $subject_id[$a]=$_POST["sub_id$a"];
     $group_id[$a]=$_POST["grup_id$a"];  
 }

That's why $ans1 is not defined. If you would write echo $ans[1] you would get the result.

If you really want a variable called $ans1 you would need to write it that way:

 for( $a = 1 ; $a <= $question_count ; $a++ ){
     ${"ans" . $a} = $_POST["ans$a"];
     ${"subject_id" . $a} = $_POST["sub_id$a"];
     ${"group_id" . $a} = $_POST["grup_id$a"];  
 }

But as I wrote in my comment I think this way of writing code should be avoided.

Community
  • 1
  • 1
t.niese
  • 32,069
  • 7
  • 56
  • 86
  • Sorry this code also show same problem. still now it say. Notice: Undefined variable: ans3 in F:\xampp\htdocs\oes\Student\result_process.php on line 65 i want to use ans3 variable. it work if i use such like this variable ${"ans" . $a} instead of $ans[$a] – Sifat080 Jun 15 '14 at 15:15
  • @Sifat080 If you can access `$ans1` but `$ans3` would result in an `Notice: Undefined variable: ans3` then `$question_count` is smaller then `3`. What do you want to say with `it work if i use such like this variable ${"ans" . $a} instead of $ans[$a]` ? – t.niese Jun 15 '14 at 15:29