0

I have this on the first page: returned results enter image description here

it shows 2 groups with different ID and with different number of members. IT09 has 3 and IT10 has 4. when the "Evaluate" label is clicked, the page then proceeds here.

enter image description here

Those textboxes are declared in such a way like this:

<table>
$i=1;
while ($data = mysql_fetch_array($querys))
{
echo '<tr>
<td width="120">'.$data['thenames'].'</td>
<td width="50">
<input type="text" name="newSG'.$i.'" id="newSG'.$i.'" class="evaluation" value="'.$data[$toshow].'" onkeypress="return isNumber(event)" />
</td>
<td>
<input type="hidden" name="stud'.$i.'" id="stud'.$i.'" value="'.$data['memberID'].'" />
</td>
</tr>';
$i++;
}
</table>

As you can see, it creates a textbox and a hidden input with name and id with a number at the end. That's what i also want to happen on my php part.

I have this code

$ii = 1;
while ($data = mysql_fetch_array($qry))
{
$stud(here) = $_POST['stud'(and here)]; <<<<

$ii++
}

I want to declare variables using this loop . as you can see (here , andhere) I want that to have numbers 1,2,3 or depending on the number of records returned by my query. the problem here is that I want to declare something like $stud1, $stud2 and so on. I dont know how to add the value of the loop after the variable $stud. can someone help?

8
  • yah yah yah that's my problem. can you give me an example of a syntax where the variable is concatenated with the number ? Commented Dec 30, 2015 at 10:40
  • Don't use variable variables. Just use an array. Commented Dec 30, 2015 at 10:41
  • @Rizier123 Don't help him do the wrong thing. Commented Dec 30, 2015 at 10:42
  • @Barmar You're right. If this would be the only wrong thing(mysql_*). Commented Dec 30, 2015 at 10:44
  • @Rizier123 that's what i did earlier, its wrong. Commented Dec 30, 2015 at 10:44

2 Answers 2

1

make an associative array instead.

$studs = array(); while(...){ $studs['stud'.index] = $row; }

access it :

echo $studs['stud1'][field];

if you really want it as a variable then:

eval('return $stud'.$ii.'=json_decode(\''.json_encode($row).'\');');

then you can:

echo $stud1->field;

Edit:

it seems your assigning your stud variable with the post. then you can:

eval('return $stud'.$ii.'=$_POST[\''.$ii.'\']');

access it:

echo $stud1;

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

2 Comments

im finding it a bit hard to understand, shoud I write anything inside array(here)? and the word index, should it be just that word or any index number i would need? and lastly $row, (the value) what if it's from a post method. $_POST['stud'] how should I include the number on that?
dont put anything in array() it's just declaring an array. index should be in your case the $ii. $row should be the $data in your mysql_fetch_array
0

Don't use separate variables, use an array.

$studs = array();
$i = 1;
while ($data = mysql_fetch_array($querys)) {
    $studs[$i] = $_POST['studs' . $i];
}

I'm not sure why you're filling in a variable from POST data in a loop over database results, but that's not really relevant.

I also suggest that you take $i out of the form inputs. Give them names like studs[] and newSG[], and PHP will automatically put them into an array in $_POST. You can then do:

$studs = $_POST['studs'];
$newSG = $_POST['newSG'];

1 Comment

There. it's done! :) thanks @barmar. but the part that you suggest, sorry but i have my reasons why I include $i on their name values. hehe.. Thank you so much anyway :) Happy new year

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.