php - 不需要的会话结果

时间:2016-06-23 08:50:40

标签: php session multidimensional-array

我正在开发一个有3页的网络应用程序 首先是 index.php
其中包含用户搜索的搜索栏 第二个是 search.php
当用户点击将用户发送到最终页面的任何结果时,显示搜索结果,如 result_1,result_2,result_3 ,其中包含信息(标题,说明,网址),即show.php
第三页是 show.php

显示用户点击的结果的信息 例如(相应的网址内容将使用iframe显示)

我尝试使用二维会话数组,但工作不正确 当用户点击任何结果时,show.php上会显示其他一些结果信息

我按print_r检查会话数组内容,它有不必要的内容 有人帮助我,我正在分享我的代码片段 search.php

<?php
session_start();
$id = 1;
while($row = mysqli_fetch_array($sql))
                        {   

                          $title = $row['title'];
                          $description = $row['description'];
                          $url = $row['content_url'];
                          $icon = $row['thumb_icon_url'];


   $_SESSION['result'][]  = Array('title' => $title,'description'=> $description,'content_url' => $url,'icon' => $icon,'id'=> $id);
  ?>
  <li name="id" ><a href="show.php?id=<?php echo $id;?>&name=<?php echo $title;?>">View doc</a></li>
  <?php
   $id++;
   ?>

show.php

<?php
 if(isset($_GET['id']))
{
 $id = $_GET['id'];
?>  
<div> 
 <iframe  class="embed-responsive-item item" src="<?php echo   $_SESSION['result'][$id]['content_url'];?>"></iframe>
</div>

当我试图检查$_SESSION['result']时,我得到了这个 enter image description here

此数组应该只包含查询结果。帮我修复它

2 个答案:

答案 0 :(得分:1)

您没有设置数组的密钥:

new ProcessBuilder().command('/bin/echo', 'FooBar').inheritIO.start

在show.php中添加$_SESSION['result'] = Array(); $_SESSION['result'][$id] = Array('title' => $title,'description'=> $description,'content_url' => $url,'icon' => $icon,'id'=> $id);

答案 1 :(得分:0)

如果我理解正确,你的问题就是你有不想要的结果。

这主要是因为您总是在所有查询中向$ _SESSION [&#39;结果&#39;]添加结果。

每次运行search.php时,您都会继续向$ _SESSION [&#39;结果&#39;]添加项目,从而打破$ id与数组位置的对应关系。

我会初始化$ _SESSION [&#39;结果&#39;]

<?php 
    session_start();
    $id = 1;
    $_SESSION['results'] = [];
    while($row = mysqli_fetch_array($sql)){
    //...
    }
相关问题