我有这个演示页面,我希望按专辑显示一些图像。 我正在用一个名为ActionControl.php的php文件的ajax请求。每张专辑都在演示页面上用'a'标签表示。我通过单击链接并从服务器获取正确的html来执行jquery ajax请求。
在ActionControl.php中,我声明了一个名为PhotoGallery.php的对象的新实例,其中包含我需要调用的所有函数,以获取相册的正确html。我还使用此对象存储当前选定的页面。
问题: 我似乎无法在服务器端存储信息(如当前活动页面)。 如果我错了,请纠正我,但问题是每次我对ActionControl.php做一个ajax请求时,我得到一个我的PhotoGallery.php的新实例,忘记了之前存储的数据。
ActionControl.php类:
<?php
require 'config.php';
require( DIR_PHOTOGALLERY . 'PhotoGallery.php' );
$photoGallery = new PhotoGallery();
if(isset($_POST['action']) && !empty($_POST['action']))
{
$action = $_POST['action'];
switch ($action)
{
case "not set": getAllImages($photoGallery); break;
case "navLinks": getLinks($photoGallery); break;
case intval($action) != 0: getAlbum($photoGallery, $action - 1); break;
default: getAllImages($photoGallery); break;
}
}
function getAllImages(PhotoGallery $gallery)
{
echo "</br></br></br></br>";
echo $gallery->getAllImages();
}
function getAlbum(PhotoGallery $gallery, $index)
{
setActiveLink($gallery, $index);
echo "</br></br></br></br>";
echo $gallery->getAlbum((int)$index);
}
function getLinks(PhotoGallery $gallery)
{
echo $gallery->getLinkHtml();
}
function setActiveLink(PhotoGallery $gallery, $index)
{
$gallery->setActiveLink($index);
}
?>
解决这个问题的最佳方法是什么?
答案 0 :(得分:0)
您可以将数据存储在$_SESSION
变量中。然后你会得到这些方面的东西;
<?php
require 'config.php';
require( DIR_PHOTOGALLERY . 'PhotoGallery.php' );
session_start();
$_SESSION["photoGallery"]=(isset($_SESSION["photoGallery"]))?$_SESSION["photoGallery"]:new PhotoGallery();
if(isset($_POST['action']) && !empty($_POST['action'])){
$action = $_POST['action'];
switch ($action){
case "not set": getAllImages($_SESSION["photoGallery"]); break;
case "navLinks": getLinks($_SESSION["photoGallery"]); break;
case intval($action) != 0: getAlbum($_SESSION["photoGallery"], $action - 1); break;
default: getAllImages($_SESSION["photoGallery"]); break;
}
}
function getAllImages(PhotoGallery $gallery){
echo "</br></br></br></br>";
echo $gallery->getAllImages();
}
function getAlbum(PhotoGallery $gallery, $index){
setActiveLink($gallery, $index);
echo "</br></br></br></br>";
echo $gallery->getAlbum((int)$index);
}
function getLinks(PhotoGallery $gallery){
echo $gallery->getLinkHtml();
}
function setActiveLink(PhotoGallery $gallery, $index){
$gallery->setActiveLink($index);
}
?>