如何在php中将数组从一个页面传递到另一个页面?

时间:2013-06-01 10:50:58

标签: php

嗨我得到动态arry来获取一个表单我需要将该数组传递给其他形式。在那我执行了一些计算,我在下面显示我需要传递$data_t1,因为它包含我可以将动态数组显示传递给其他形式。

page1.php中

    <?php

        for ($i = 1; $i <= $de; $i++) {
        ?>
        <tr>

            <td>
                <select id="in4-<?php echo $i; ?>" name="t1[<?php echo $i; ?>]" onclick="getText3(<?php echo $i; ?>)" onchange="getText39(<?php echo $i; ?>)" onmouseout="getText89(<?php echo $i; ?>)">
                    <option value="0">0</option>
                    <option value="12">12</option>
                    <option value="18">18</option>
                    <option value="24">24</option>
                    <option value="75">75</option>
                </select>
            </td>
            <td>
                <input type="text" name="t2[<?php echo $i; ?>]" id="in1-<?php echo $i; ?>" onblur="getText3(<?php echo $i; ?>)" onchange="getText39(<?php echo $i; ?>)" onmouseout="getText89(<?php echo $i; ?>)"/>
            </td>
            <td>
                <select name="a1[<?php echo $i; ?>]" id="in2-<?php echo $i; ?>" onclick="getText3(<?php echo $i; ?>)"onchange="getText39(<?php echo $i; ?>)" onmouseout="getText89(<?php echo $i; ?>)" >
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                </select>
            </td>
            <td>
                <input type="text" id="in3-<?php echo $i; ?>" name="username[<?php echo $i; ?>]" readonly="readonly" />
            </td>
<?php }?>
        </table>

    <input type="submit" name="submit" />



coding of acessing array from page1.php

<?php

if (isset($_POST['submit'])) {

    $data_t1 = $_POST['t1'];


    foreach ($data_t1 as $key => $value) {

        $value ;
       $_POST['t2'][$key];
    $_POST['a1'][$key];
      $_POST['username'][$key]; 



    }
      $data_t2 = $_POST['t2'];
      $data_t3=$_POST['a1'];
      $data_t4=$_POST['username'];

}

?>
  <?php
  //database connection
$db = new PDO("mysql:host=localhost;dbname=ems",'root','');
//query
$sql = "INSERT INTO table1 (c0,c1, c2, c3, c4 ,c13) VALUES ('',:c1, :c2, :c3, :c4, '$d')";
$stmt = $db->prepare($sql);
foreach ($data_t1 as $i => $value) {
    $stmt->execute(array(
       ':c1'=>$data_t1[$i],
       ':c2'=>$data_t2[$i],
       ':c3'=>$data_t3[$i],
       ':c4'=>$data_t4[$i],
    ));
} ?>
<?php
include('config.php');
$sa="select * from table1 where timestamp=now()";
$result=mysql_query($sa) or die(mysql_error());
$th=-2;
$xa=1;
$xb=2;
$xc=2;
echo "<table border='1'>
<tr>

</tr>";
$row_count=0;

while($row = mysql_fetch_array($result)) 
{ 
     $row['c1'];
   $row['c2'];
     $row['c3'];
     $row['c4'];

    $a[]=$row['c1'];
        $b[]=$row['c2'];
            $c[]=$row['c3'];
            $d[]=$row['c4'];

            //tube lights//
                $m[]=round(($row['c1']/$row['c4']),2);
                $n[]=round(($row['c2']/$row['c4']),2);
                $o[]=round(($row['c3']/$row['c4']),2);

                //ccr model technique//
                //tube lights//
                $m1[]=abs(round((((($row['c1'])*($th)+($row['c1']*$xa)+($row['c5']*$xb)+($row['c9']*$xc))+1)/2),2));
                $n1[]=abs(round((((($row['c2'])*($th)+($row['c2']*$xa)+($row['c6']*$xb)+($row['c10']*$xc))+1)/2),2));
                $o1[]=abs(round((((($row['c3'])*($th)+($row['c3']*$xa)+($row['c7']*$xb)+($row['c11']*$xc))+1)/2),2));



                $row_count++;

}

 //optimial values//
$fa=implode($a,',');
$fa1=explode(',',$fa);
$fb=implode($b,',');
$fb1=explode(',',$fb);
$fcc=implode($c,',');
$fcc1=explode(',',$fcc);
$fe=implode($e,',');
$fe1=explode(',',$fe);
$ff=implode($f,',');


    //for efficeiency frointer//

for($i=0;$i<$row_count;$i++) 
{ // do the exploding, the imploding, the row echoing for each row// 
echo "<table border='1' align='center'>
<tr>
<th>Inputs</th>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>";
echo "Measuring1".($i+1);
  echo "<tr>";
  //tube lights//
     $f=implode($m,',');
     $r=explode(',',$f);

     $f1=implode($n,',');
     $r1=explode(',',$f1);

     $f2=implode($o,',');
     $r2=explode(',',$f2);


echo "<td>" ."W"</td>";
echo "<td>".$r[$i]. "</td>";
echo "</tr>"; 
echo  "<td>" ."No. "</td>";
echo "<td>".$r1[$i]. "</td>";
;
 echo "</tr>"; 
echo  "<td>" ."Dur "."</td>";
echo "<td>".$r2[$i]. "</td>";
echo "</tr>"; 

echo  "</table>";
}
for($i=0;$i<$row_count;$i++) 
{
echo "<table border='1' align='center'>
<tr>
<th>Inputs</th>
<th>ccr</th>
<th>ccr</th>
<th>Ccr</th>
</tr>";
  echo "<tr>";
  echo "Measuring 2".($i+1);  
      //tube lights//
     $fc=implode($m1,',');
     $rc=explode(',',$fc);

     $fc1=implode($n1,',');
     $rc1=explode(',',$fc1);

     $fc2=implode($o1,',');
     $rc2=explode(',',$fc2);

     //ccr model technique// 
 echo "<td>" ."Wa ". "</td>";
echo "<td>".$rc[$i]. "</td>";
echo "</tr>"; 
echo  "<td>" ."n ". "</td>";
echo "<td>".$rc1[$i]. "</td>";
echo "</tr>"; 
echo  "<td>" ."Dur "."</td>";
echo "<td>".$rc2[$i]. "</td>";
echo  "</table>";
}
?>

<?php
for($i=0;$i<$row_count;$i++) 
{
echo "<table border='1' align='center'>
<tr>
<th>Inputs</th>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>";
 echo "<tr>";
  echo " Optimial values Of Room ".($i+1);
  //voltage of appliances//
   echo "<td>" ."W". "</td>";
if($fa1<=$rc)
{

echo "<td>". $fa1[$i]."</td>";
}
else
{

echo "<td>". $rc[$i]."</td>";
}
  echo "<tr>";
      echo "<td>" ."Du". "</td>";
if($fcc1<=$rc2)
{
echo "<td>". $fcc1[$i]. "</td>";
}
else
{
echo "<td>". $rc2[$i]. "</td>";
}
echo "</tr>";
 echo "<tr>";
    echo "<td>" ."Co". "</td>";
if($fa1<=$rc && $fb1<=$rc1 && $fcc1<=$rc2 )
{

 $gk[]=round(((($fa1[$i]*$fb1[$i]*$fcc1[$i])*30)/1000),2);
 echo  "<td>" . $gk[$i]. "</td>";
 }
else
{
$gk1[]=round(((($rc[$i]*$rc1[$i]*$rc2[$i])*30)/1000),2);
 echo  "<td>" . $gk1[$i]. "</td>";
}

?>

请帮助我。

2 个答案:

答案 0 :(得分:2)

看看SESSIONs,您可以在那里存储临时会话数据。

答案 1 :(得分:2)

我想评论@mightyuhu之前的回答,但我还没有代表,所以我会这样试试:

只是阅读你的问题,而不是完整的一些令人困惑的代码,我会说他的方法是完全正确的:会话。
但是因为你显然不知道如何处理它们 - 这里有一个非常简短的操作方法:

在您要访问共享数据的任何站点上调用session_start(); - 但在进行任何输出之前,在任何回显或打印或空行或php-tags之外的html代码之前调用它。 之后,您可以通过$data = $_SESSION['my_variable_name'];$_SESSION['my_variable_name'] = $new_data;访问/设置/修改会话数据 如果您想要删除数据,请致电unset($_SESSION['my_variable_name']);

希望有所帮助:)