0

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

获取这些动态数组的编码 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'];
}

现在我将这些数组放在数据库中 n 获取它们我需要将这些获取数组传递到下一页

<?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++;

}

在这里我调用 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>";

}

当我访问这些页面时,page2.php 上的所有编码,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);

?>

请帮忙

4

2 回答 2

0

如果要使用会话,则必须session_start()在每个页面的开头输入:

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

将该方法放在每一页的开头,紧随其后<?php

于 2013-06-02T08:21:12.530 回答
0

您可以序列化您的数组并将其传递到 url。下面的链接解释了保存数组php 序列化,使用 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()在所有页面中使用。我在这个例子中使用了机器人会话和数组序列化。

希望这对您有所帮助。

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

kumar_av@usp.ac.fj

于 2013-06-02T08:30:29.087 回答