将动态div保存到SESSION中

时间:2013-02-22 08:59:58

标签: php

大家好,我有以下PHP脚本:

if(isset($_POST['add_slider']))
    {
        echo '<div class="sliderz">';
        $images = mysql_query("SELECT * FROM store");               
        while($row = mysql_fetch_assoc($images)) 
            {
                echo '<ul class="connectedSortable">';
                echo '<li>';
                echo "<img class='ui-state-default' src=".$row['image'].">";
                echo '</li>';
                echo '</ul>';
            }
        echo '</div>';
    }

并且html看起来像这样:

<form action="admin.php" method="POST">
                    <input type="submit" value="Add new slider!" name='add_slider'>
                </form>

我怎么能同意这件事但将其保存到$ _SESSION: - ?

2 个答案:

答案 0 :(得分:1)

试试这个,

<?php
    if(isset($_POST['add_slider']))
        {
            $arr = array(); //the array will hold the html element 
            $arr[] = '<div class="sliderz">';
            $images = mysql_query("SELECT * FROM store");               
            while($row = mysql_fetch_assoc($images)) 
                {

                $arr[] = '<ul class="connectedSortable">';
            $arr[] = '<li>';
                    $arr[] = "<img class='ui-state-default' src=".$row['image'].">";
                    $arr[] = '</li>';
                    $arr[] = '</ul>';
                }
            $arr[] = '</div>';

            $_SESSION['your_session'] = implode("",$arr); // the array content will be gluedd together to form a your div with contents inside it
        }


    ?>

答案 1 :(得分:1)

if(isset($_POST['add_slider']))
{
    $output = '<div class="sliderz">';
    $images = mysql_query("SELECT * FROM store");               
    while($row = mysql_fetch_assoc($images)) 
        {
            $output .= '<ul class="connectedSortable">';
            $output .= '<li>';
            $output .= "<img class='ui-state-default' src=".$row['image'].">";
            $output .= '</li>';
            $output .= '</ul>';
        }
    $output .= '</div>';

   $_SESSION['yourKey'] = $output;
}

这会将div字符串存储在$_SESSION数组中,并以yourKey作为关键字

当你需要那样做时

echo $_SEESION['yourKey'];

也不要忘记与session_start();开始会话,否则会出错。