简体   繁体   English

如何将变量传递到php中的会话数组

[英]how to pass variables into session array in php

after executing query search.php ,multiple results appears with own info(title,description,url). 执行search.php查询后,多个结果会显示自己的info(title,description,url)。

result_1,result_2,result_3......

when i click on url,next page final.php opens. 当我单击url时,下一页final.php打开。 how should i store that result info in variable and display it on final.php . 我该如何将结果信息存储在变量中并显示在final.php上
I tried session variables,but it only passes last result info ie result_3 to next page. 我尝试了会话变量,但它仅将最后的结果信息(即result_3到下一页。

here's my code 这是我的代码

search.php search.php中

  <?php
  session_start();
  $_SESSION['title'] = $title;
  $_SESSION['description'] = $description;
  $_SESSION['content_url'] = $url;
  $_SESSION['icon'] = $icon;
  ?>   

final.php final.php

 <?php 
    session_start();
    $session_title  = $_SESSION['title'];
    $session_description = $_SESSION['description'];
    $session_url = $_SESSION['content_url'];
    $session_icon = $_SESSION['icon'];
  ?>

Make sure you put session_start(); 确保您放置session_start(); on top of each file you were using the session variables. 在您使用会话变量的每个文件之上。

To make a array session variable you can do something like this: 要创建一个数组会话变量,您可以执行以下操作:

<?php
session_start();

$_SESSION["array"][] = array(
    "title"         => $title,
    "description"   => $description,
    "content_url"   => $url,
    "icon"          => $icon
);

echo "<pre>";
var_dump($_SESSION);
echo "</pre>";

# Optional clear session, remove line if you don't want to
session_unset($_SESSION);
?>

Above code will output something like this: 上面的代码将输出如下内容:

array(1) {
  ["array"]=>
  array(1) {
    [0]=>
    array(4) {
      ["title"]=>
      NULL
      ["description"]=>
      NULL
      ["content_url"]=>
      NULL
      ["icon"]=>
      NULL
    }
  }
}

In final.php you can check this by doing: 在final.php中,您可以通过以下方法进行检查:

if(isset($_SESSION["array"])) {
    foreach($_SESSION["array"] as $result => $key) {
        echo $key["title"];
    }
}

I hope this will help you. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM