0

大家好,我有以下 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 :-?

4

2 回答 2

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
        }


    ?>
于 2013-02-22T09:14:37.807 回答
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();否则它会出错。

于 2013-02-22T09:24:05.113 回答