Jump to content

keeping the values of two dimensional array on a form


sad_sam19

Recommended Posts

Gud morning to all! :)

I'm new to this forum and I hope somebody could help me about arrays.

I 'am not gud on explaining things so please try to understand.

my problem is, i want to keep the value of my two dimensional array on a form.

for example..

 

PAGE1.PHP

$nodep = 2;
// ' this part should also be array, right?but how?
$name = $_POST['name'];
$age = $_POST['age'];
$yr = $_POST['yr'];
$school = $_POST['school'];

<form method='post' action='page2.php'>
for($i=1;$i<=$nodep;$i++)
{
   for($j=1;$j<=4;$j++)
   {
     echo"<input type='text' name='name[$i][$j]' value='i don't know what to put here to restore my value from page2.php'>"; $j++;
     echo"<input type='text' name='age[$i][$j]' value='i don't know what to put here to restore my value from page2.php'>"; $j++;
     echo"<input type='text' name='yr[$i][$j]' value='i don't know what to put here to restore my value from page2.php'>"; $j++;
     echo"<input type='text' name='school[$i][$j]' value='i don't know what to put here to restore my value from page2.php'>"; $j++;
   }
}
<input type='submit' value='go to page2'>
</form>

 

PAGE2.PHP

$nodep = 2;
// ' this part should also be array, right?but how?
$name = $_POST['name'];
$age = $_POST['age'];
$yr = $_POST['yr'];
$school = $_POST['school'];

<form method='post' action='page1.php'>
for($i=1;$i<=$nodep;$i++)
{
   for($j=1;$j<=4;$j++)
   {
      echo" $name[$i][$j]? "; $j++;
      echo" $age[$i][$j]? "; $j++;
      echo" $yr[$i][$j]? "; $j++;
      echo" $school[$i][$j]? "; $j++;
   }
}
<input type='submit' value='go to page1'>
</form>

 

please help me out..thank you so much!! :-*

Link to comment
Share on other sites

A couple of points...

 

1. What is your data when you say this ... 'i don't know what to put here to restore my value from page2.php'?

 

2. Are you intentionally incrementing this?

echo"<input type='text' name='name[$i][$j]' value='i don't know what to put here to restore my value from page2.php'>"; $j++;

 

3. If you are trying to retrieve an array with what you have try this...haven't tested it though

 

<?php
for($i=1;$i<=$nodep;$i++)
  for($j=1;$j<=4;$j++)
  {
    $name[$i][$j] = $_POST['name[' . $i . '][' . $j .'];
    $age[$i][$j] = $_POST['age[' . $i . '][' . $j .'];
    $yr[$i][$j] = $_POST['yr[' . $i . '][' . $j .'];
    $school[$i][$j] = $_POST['school[' . $i . '][' . $j .'];
  }
?>

 

4. You don't really need a $_POST statement on your form (The one sending) unless you recieve it from prior pages too.

 

5. Use php tag when you are on your php section .... use a separate file or whatever ... just dont forget the tag


<form method='post' action='page2.php'>
<?php
for($i=1;$i<=$nodep;$i++)
{
   for($j=1;$j<=4;$j++)
   {
     echo"<input type='text' name='name[$i][$j]' value='i don't know what to put here to restore my value from page2.php'>";
     echo"<input type='text' name='age[$i][$j]' value='i don't know what to put here to restore my value from page2.php'>";       
     echo"<input type='text' name='yr[$i][$j]' value='i don't know what to put here to restore my value from page2.php'>";
     echo"<input type='text' name='school[$i][$j]' value='i don't know what to put here to restore my value from page2.php'>";
   }
}
?>

 

6. Do you really want to display your result on a form when you recieve it?

7. You don't use double qoutes  < " > when trying to display the value of a variable

 

<?php
for($i=1;$i<=$nodep;$i++)
   for($j=1;$j<=4;$j++)
   {
      echo $name[$i][$j];
      echo $age[$i][$j];
      echo $yr[$i][$j];
      echo $school[$i][$j];
   }
}
?>

 

Work this out section first dude.

Link to comment
Share on other sites

to me your best bet is to use sessions

 

Page1.php

<?php
session_start();

//init vars
for($i=1;$i<=$nodep;$i++)
{
   for($j=1;$j<=4;$j++)
   {
     $name[$i][$j] = "cell ".$i."-".$j;
     $age[$i][$j] = "cell ".$i."-".$j;
     $year[$i][$j] = "cell ".$i."-".$j;
     $school[$i][$j] = "cell ".$i."-".$j;
   }
}

//set session values
$_SESSION['name'] = $name;
$_SESSION['age'] = $age;
$_SESSION['year'] = $year;
$_SESSION['school'] = $school;

<form method='post' action='page2.php'>
<input type='submit' value='go to page2'>
</form>
?>

 

 

page2.php

<?php
session_start();

$name = $_SESSION['name'];
$age = $_SESSION['age'];
$year = $_SESSION['year'];
$school = $_SESSION['school'];

for($i=1;$i<count($name);$i++)
{
   for($j=1;$j<=4;$j++)
   {
     echo $name[$i][$j]." ".$age[$i][$j]." ".$year[$i][$j]." ".$school[$i][$j]."<br>";
   }
}

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.