如何在php中的不同页面上传递动态数组?

时间:2013-06-02 08:09:56

标签: php

我需要通过按钮点击将数组从一个页面传递到其他页面。实际上我在数组中获取数据库的每一列我希望将该数组传递到不同的页面。我使用了会话可变,但它不是工作。

编码og page2.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,) VALUES ('',:c1, :c2, :c3, :c4,)
$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());
    while($row = mysql_fetch_array($result)) 
    { 
         $row['c1'];
       $row['c2'];
         $row['c3'];
         $row['c4'];
    $m[]=round(($row['c1']/$row['c4']);
    $n[]=round(($row['c2']/$row['c4']),2);
    $o[]=round(($row['c3']/$row['c4']),2);
    $row_count++;

}

这里我称之为showng的fecth数组

<?php 
    session_start();
$_SESSION['name'] =$r;
$_SESSION['name1'] = $r1;
$_SESSION['name2'] = $r2;
    for($i=0;$i<$row_count;$i++) 
    { // do the exploding, the imploding, the row echoing for each row// 
?>

<?php
    echo "<table border='1' align='center'>
    <tr>
    <th>Inputs</th>
    <th>A</th>
    <th>B</th>
    <th>C</th>
    </tr>";
    echo "Meas1".($i+1);
      echo "<tr>";

         $f=implode($m,',');
         $r=explode(',',$f);

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

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

    echo "<td>".$r[$i]. "</td>";
    echo "<td>".$r1[$i]. "</td>";
    echo "<td>".$r2[$i]. "</td>";

}

当我在r[],r1[],r2[]访问next page page3.php page3.php <?php session_start(); $r= $_SESSION['name']; echo $r; var_dump($r); print_r($r); $r1= $_SESSION['name1']; echo $r1; print_r($r1); $r2= $_SESSION['name2']; echo $r2; print_r($r2); ?> 时,它显示在page2.php上面的所有密码

{{1}}

为这些人提供帮助

2 个答案:

答案 0 :(得分:0)

如果您想使用会话,则必须在每个页面的开头放置session_start()

<?php
session_start();
    for ($i = 1; $i <= $de; $i++) {
    ?>
    <tr>
        <td>T</td>
        <td>
        //rest of enormous amount of code...

<?php

之后立即将该方法放在每个页面的开头

答案 1 :(得分:0)

您可以序列化数组并将其传递到url中。以下链接说明Saving arraysphp serialze,使用get方法获取数组并反序列化。

带表单/按钮的页面

<?php
    session_start();
    //this is a sample array
    $samplearray = array(
                        't1' => 'this is t1',
                        't2' => 'this is t2',
                        'a1' => 'this is a1',
                        'username' =>'this is username'
                    );
    $str = serialize($samplearray);
    $strenc = urlencode($str);  
    $_SESSION['array'] = $samplearray;
?>
<form action="page2.php" method="POST">
<input type="hidden" name="array" value="<?php echo $strenc; ?>"/>
<input type="submit" name="btnsubmit" />
</form>

<强>使page2.php

<?php
session_start();
if (isset($_POST['btnsubmit'])) {
$array = $_POST['array'];
$arr = unserialize(urldecode($array));


echo "Unserialized array";
echo "<pre>";
print_r($arr);
echo "</pre>";

echo "<br/><br/>SESSION array";
echo "<pre>";
print_r($_SESSION['array']);
echo "</pre>";
}
?>

请记住,您必须在所有页面中使用session_start()。我在这个例子中使用了bot会话和数组序列化。

希望这会对你有所帮助。

如果您需要其他帮助,请发送电子邮件给我

kumar_av@usp.ac.fj