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 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒