0

我得到了这段代码:

if(isset($_GET['viewevent'])) {
    if(count($_SESSION['e_lastviewed']) == 0) {
      $_SESSION['e_lastviewed'][0] = $_GET['viewevent'];
    } else if(!in_array($_GET['viewevent'], $_SESSION['e_lastviewed'])) {
      $_SESSION['e_lastviewed'][2] = $_SESSION['e_lastviewed'][1];
      $_SESSION['e_lastviewed'][1] = $_SESSION['e_lastviewed'][0];
      $_SESSION['e_lastviewed'][0] = $_GET['viewevent'];
    }
  }
  if($_GET['show']) {
    $_SESSION['show'] = $_GET['show'];
  } else if($_SESSION['show']=='') {
    $_SESSION['show'] = "all";
  }

它显然保存了最近查看的项目的 ID,所以我需要将这些 ID 放入一个数组中。这行得通吗?

$my_array = array($_SESSION['e_lastviewed'][2],$_SESSION['e_lastviewed'][1],$_SESSION['e_lastviewed'][0]);

我已经运行了它,但它显示空白结果(不确定那是因为我没有做正确还是代码不完整......我错过了什么?我不确定我是否完全理解给我的脚本......

4

1 回答 1

0

尝试这个:

  if ( !isset($_SESSION['e_lastviewed']) )
    $_SESSION['e_lastviewed'] = array();

  // alt: while(count($_SESSION['e_lastviewed']) > 2 ) {
  if(count($_SESSION['e_lastviewed']) > 2 ) {
    array_shift($_SESSION['e_lastviewed']); // drop off from 3

  array_unshift($_SESSION['e_lastviewed'],$_GET['viewevent']); // insert in the beginning

  if($_GET['show']) {
    $_SESSION['show'] = $_GET['show'];
  } else if($_SESSION['show']=='') {
    $_SESSION['show'] = "all";
  }
于 2013-05-28T11:07:53.740 回答