arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

arrays

Post by gurjit »

i do some validation on a page and if a field is empty the form is passed through.

in sending the form i use <form name="f1" method="post">

on this page i have a field 4 times

<input type="text" name="t1[]">
<input type="text" name="t1[]">
<input type="text" name="t1[]">
<input type="text" name="t1[]">

<input type="text" name="t2[]">
<input type="text" name="t2[]">
<input type="text" name="t2[]">
<input type="text" name="t2[]">

when the validation fails i want to pull the correct value in the correct box. i have tried this:

<?php
var1 = "t$myvar"."[$myvar2]"; echo $var1; $myvar2++;
?>

it prints out t1[0],t2[0],t1[1],t2[1]

which appears in the correct box, HOW DO I READ THE VALUE THROUGH????????????
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

How do you know what the correct box is? For both t1 and t2, you're going to have 4 values in each array. Could you give an example of a situation that would show up?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

for example

1 2 3 4 5
main1 10.00 11.00 14.00 15.00 20.00
main2 60.00 90.00 50.00 etc
main3 22.00 88.00 etc


so the box names in main 1 are t1[], in main 2 are t2[] and main 3 are t3[].

i can see on the screen t1[0], t1[1] etc in the correct boxes but i cannot display the value when the screen is refreshed. however if i put <?php echo $t1[0]; ?> direct the value comes out.

ive created the variables like this so far, which is in a loop where myvar and myvar2 increment for each box.

<?php $var = "t$myvar"."[$myvar2]"; ?>

i want to display the value for this variable?

if i put the array position <?php echo t1[0]; ?> direct the value appears.


how can i create the variable t1[0] and display the value?



heres my code:

<tr>
<td align="right" bgcolor="#FDCC65" class="boldbodytext">&nbsp;</td>
<td bgcolor="#FDCC65" class="bodytext">Tier 1</td>
<td bgcolor="#FDCC65" class="bodytext">Tier 2</td>
<td bgcolor="#FDCC65" class="bodytext">Tier 3</td>
<td bgcolor="#FDCC65" class="bodytext">Tier 4</td>
<td bgcolor="#FDCC65" class="bodytext">Tier 5 </td>
</tr>
<?php
$myvar = 0;

while ($row = mysql_fetch_array($mysql_result_stageP))
{

$stid = $row["stid"];
$st_name = $row["st_name"];
$myvar2 = 0;
$myvar++;
?>
<tr>
<td class="boldbodytext" bgcolor="#E2E2E2"><?php echo $st_name; ?></td>
<td bgcolor="#E2E2E2"><input name="t<?php echo $myvar; ?>[]" type="text" class="feeforms" value="<?php $var = "t$myvar"."[$myvar2]"; $val = $var; echo $val; ?>"></td>
<td bgcolor="#E2E2E2"><input name="t<?php echo $myvar; ?>[]" type="text" class="feeforms" value="<?php echo $myvar2; $myvar2++; ?>"></td>
<td bgcolor="#E2E2E2"><input name="t<?php echo $myvar; ?>[]" type="text" class="feeforms" value="<?php echo $myvar2; $myvar2++; ?>"></td>
<td bgcolor="#E2E2E2"><input name="t<?php echo $myvar; ?>[]" type="text" class="feeforms" value="<?php echo $myvar2; $myvar2++; ?>"></td>
<td bgcolor="#E2E2E2"><input name="t<?php echo $myvar; ?>[]" type="text" class="feeforms" value="<?php echo $myvar2; $myvar2++; ?>"></td>
</tr>
<?php

} //close loop
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Ok, I think what you want is the ability to dynamically create variable names. I'm not 100% sure how to do this, but I think it's something like this

Code: Select all

${"t".$myvar};
so if $myvar = 1, then the variable you've just made is $t1. From there you can call $t1[$myvar2]. However, I'm wondering why you want to dynamically make a variable when it's already in an array, which is a construct ideally suited for a collection of values not assigned to a particular variable?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply