dshw124502 2015-04-03 06:21
浏览 51
已采纳

PHP重复输出

For some reason, my code is only outputting 'p3' and 'p4' twice. I was wondering if anyone could point out why it's repeating, and it'd be appreciated. Here's an example of what is wrong (this only is on p3 and p4 as stated above, p1 and p2 work fine)

http://i.imgur.com/glNXwr7.png

Here's my code:

index.php -

 <?php
 session_start();

 if (!isset($_SESSION['p']))
 {
     $_SESSION['p'] = 'p1';
 }

 include('core.php');
 include('core2.php');
 $story = new Story;
 $action = new Action;
 $part = $_SESSION['p'];
 ?>
 <!DOCTYPE HTML>
 <html>
<head>
    <title>MacBeth Console</title>

    <style>
        body{
        background:black;
        color:#2ecc71;
        font-family: "Courier New", Courier, monospace
        }

        .txt{
            border:0px;
            background-color:black;
            color:#2ecc71;
            font-family: "Courier New", Courier, monospace
        }

        input:focus {outline: none; }
    </style>
</head>

<body>
    <?php

    echo 'DEBUG: P_'.$part.' <br />';

        if ($_SESSION['p'] == 'p1')
            $story->p1();
        else
            $action->continueStory($part, '');

        if (isset($_POST['submit']))
        {
            $action->ContinueStory($part, $_POST['yourAction']);
        }
    ?>
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
        <input type="hidden" name="p" value="<?php echo $part; ?>" style="vertical-align:middle;">
        <img src="cmd.png"><input type="text" id="input" name="yourAction" class="txt">
        <input type="submit" name="submit" value="continue">    
    </form>
</body>
 </html>

core.php -

 <?php
class Story
{
    public function p1()
    {
        print '<b>Witch 1</b>: When shall we all meet again? <br />
                <b>Witch 2</b>: When the hurly-burly is done, and the battle\'s lost and won. <br />
                <b>Witch 3</b>: That will be ere the set of sun. <br />
                <b>Witch 1</b>: Where\'s the place? <br />
                <b>Witch 2</b>: Upon the heath, is this location to thou satisfaction? <br />';
    }

    public function p2($action)
    {
        $action = strtolower($action);
        if ($action == 'yes')
        {
            $_SESSION['p'] = 'p3';
            print '** The meeting shall be held upon the heath to meet with MacBeth. ** -- PRESS ENTER';
        }
        else if ($action == 'no')
        {
            $_SESSION['p'] = 'p3';
            print '** Despite your opinion, the other witches decide the best place to meet would be at the heath. **';
        }
        else
        {
            print 'Unknown action "'.$action.'".';
        }
    }

    public function p3($action)
    {
        echo 'test ';
            $_SESSION['p'] = 'p4';
            /*return '<b><i>Scene II</i></b> <br />
                    <b>Duncan</b>: What bloody man is that? He can report, as seemeth by his plight, of the revolt the newest state. <br /> <br />
                    <b>Sergent</b>: Doubtful it stood; As two spent swimmers, that do cling together, and choke their art. The merciless Macdonwald-- Worthy to be a rebel, for to that
                                    The multiplying villanies of nature. Do swarm upon him--from the western isles of kerns and gallowglasses is supplied; and fortune, on his damned quarrel smiling,
                                    Show\'d like a rebel\'s whore: but all\'s too weak: For brave Macbeth--well he deserves that name-- Disdaining fortune, with his brandish\'d steel,
                                    which smoked with bloody execution like valour\'s minion carved out his passage till he faced the slave; Which ne\'er shook hands, nor bade farewell to him 
                                    till he unseam\'d him from the nave to the chaps and fix\'d his head upon our battlements.  
                    <b>Duncan</b>: O valiant cousin! worthy gentleman! <br /> <br />
                    ** Please press enter ** <br />';*/

    }

    public function p4($action)
    {
        $_SESSION['p'] = 'p1';
        echo 'test 2';
    }
}
 ?>

core2.php -

 <?php
class Action
{
    public function ContinueStory($p, $action)
    {
        $story = new Story;
        if ($p == "p1")
        {
             $story->p2($action);
        }
        else if ($p == "p3")
        {
             $story->p3($action);
        }
        else if ($p == "p4")
        {
            $story->p4($action);
        }
    }
}
?>
  • 写回答

1条回答 默认 最新

  • dt20081409 2015-04-03 07:36
    关注

    Your problem is here :

    <?php
    
    echo 'DEBUG: P_'.$part.' <br />';
    
        if ($_SESSION['p'] == 'p1')
            $story->p1();
        else
            $action->continueStory($part, '');
    
        if (isset($_POST['submit']))
        {
            $action->ContinueStory($part, $_POST['yourAction']);
        }
    ?>
    

    You're doing

    $action->continueStory($part, '');
    

    twice. First in your first 'if else', and secondly in your "if isset $_POST".

    This should be the best way :

    <?php
    
    echo 'DEBUG: P_'.$part.' <br />';
    
    
        if (isset($_POST['submit']))
        {
            $action->ContinueStory($part, $_POST['yourAction']);
        } else {
            if ($_SESSION['p'] == 'p1')
                $story->p1();
            else
                $action->continueStory($part, '');
        }
        ?>
    

    N.B. : For security purpose, don't use $_SERVER['PHP_SELF'] in your form action, just #_ should be better I think!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?