doudao8283 2012-07-18 11:56
浏览 29
已采纳

创建循环,在满足条件后重定向到另一个页面

I'm trying to create a loop, which redirects to another script when the condition is met. But at the moment, it meets the condition, ends the loop, but doesn't redirect to the script. Nothing I have done works...

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
define('INCLUDE_CHECK',true);
require '../config/connection.php';
session_name('ppRemember');
session_set_cookie_params(86400);
session_start();
//get value from database. if is pending then display loading else display summary. if is complete but no session set say session has expired
if (isset($_SESSION['txnId'])) {
if ($_SESSION['txnId']==true) {
    header('Location: ../confirmation');
    die();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body> 
<div id="basket-empty" title="Success" style="display:none;">
<p>Basket emptied</p>
</div>
<div id="confirm-empty" title="Confirm" style="display:none;">
<p>Are you sure you want to empty your basket?</p>
</div>
<div id="page-wrapper">
    <div id="page">
        <div id="header" class="without-secondary-menu">
                <div class="section clearfix">
                    <div id="logo-caption">
                        <a href="/" title="Home" rel="home" id="logo">
                        </a>
                    </div>
                    <div id="caption">
                    </div>
                    <div id="main-menu" class="navigation">
                        <ul id="main-menu-links" class="links clearfix">
                            <li class="menu-2 active-trail active"><a href="../index"> Order Online </a></li>
                            <li class="menu-9"><a href="../my-account"> My Account </a></li>
                            <?php if (!$_SESSION['id']) {?>
                            <li class="menu-10"><a href="../login"> Sign In </a></li>
                            <?php } ?>
                            <li class="menu-5"><a href="../getaquote"> Get a Quote </a></li>
                            <li class="menu-6"><a href="../contactus"> Contact Us </a></li>
                            <li class="menu-11"><a href="../news"> News </a></li>
                            <li class="menu-8"><a href="../uploads"> Uploads </a></li>
                            <li class="menu-7"><a href="../downloads"> Downloads </a></li>
                        </ul>           
                    </div>
                </div>
            </div>
            <?php if (isset($_SESSION['id'])) { ?>
                <div id="panel" class="clearfix">
                    <div id="account-links">
                        <ul class="username">
                            <li><span class="account-links"><a href="../my-account">You are logged in as <b><?php echo $_SESSION['username']; ?></b></a></span>
                            <span class="logout"><a href="../destroy">Logout</a></span></li>
                        </ul>   
                    </div>
                </div>
            <?php } ?>  
        <div id="main-wrapper" class="clearfix">
            <div id="main" class="clearfix">
                <div class="content">
                    <a id="main-content"></a>
                    <div class="tabs">
                    </div>
                    <div class="region region-content">
                        <div id="block-system-main" class="block block-system">
                            <div class="content">
                                <div id="node-1" class="clearfix">
                                    <div class="content clearfix">
                                        <?php if (!isset($_SESSION['txnId'])) {
                                        if (isset($_SESSION['cart'])) { ?>
                                        <div class="loading2" style="height:300px;">
                                            <span class="text2"><font size="6pt"><b>Loading....</b></font></span>
                                            <img src="../images/ajax-loader2.gif"/><br />
                                            <font size="3pt"><b>Please wait a moment whilst we process your payment</b></font><br />
                                            <font size="1pt"><b>Click <a href="/order/complete">here</a> to reload after 10 seconds</b></font>
                                        </div>
                                        <?php }
                                        } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div id="footer-wrapper">
                <div class="section">
                    <div id="footer" class="clearfix">
                        <div class="region region-footer">
                            <div class="block">
                                <div class="content">
                                    <div class="column1">
                                    </div>
                                    <div class="column2">
                                                                                </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html> 
<?php
$trans = $_SESSION['transaction_id'];
$clientId = $_SESSION['id'];
if (!isset($_SESSION['txnId'])){
    while ($_SESSION['txnId']!=true) {
        $mysqli = new mysqli("localhost", "#####", "#####", "######");
        $stmt = $mysqli->prepare("SELECT `txn_id` FROM `trans` AS t, `order_table` AS ot, `customer_order_details` AS cod WHERE t.transaction_id=?
         && t.transaction_id = ot.transaction_id && ot.customer_order_details_id = cod.customer_order_details_id && cod.client_id=?");
        $stmt->bind_param('ii',$trans,$clientId);
        if ($stmt->execute()) {
            $stmt->store_result(); 
            $num_rows = $stmt->num_rows;
            if ($num_rows>=1) {
                $stmt->bind_result($txnidrow);
                $stmt->fetch();
                $stmt->free_result();
                $stmt->close();
                $_SESSION['CARRYTXNID'] = $txnidrow;    
                $_SESSION['txnId'] = true;
            }
        }
    } 
}
header('Location: ../confirmation');
die();
}

?>

Any headway is greatly appreciated. I've tried a do while loop, header without die/exit. I've removed all code and redirected the page on it's own, ran the script and all is fine. Etc, etc. I echo'd something to make sure that the condition is finally met, as well.

  • 写回答

4条回答 默认 最新

  • dongpou1935 2012-07-18 12:38
    关注

    Following your edit, the solution here is actually quite simple - all you need to do is move the redirect code to the top of the page:

    <?php
    
      define('INCLUDE_CHECK',true);
      require '../config/connection.php';
    
      session_name('ppRemember');
      session_set_cookie_params(86400);
      session_start();
    
      $trans = $_SESSION['transaction_id'];
      $clientId = $_SESSION['id'];
    
      //get value from database. if is pending then display loading else display summary. if is complete but no session set say session has expired
      if (isset($_SESSION['txnId'])) {
        if ($_SESSION['txnId']==true) {
          header('Location: ../confirmation');
          die();
        }
      } else {
        while (!$_SESSION['txnId']) {
          $mysqli = new mysqli("localhost", "#####", "#####", "######");
          $stmt = $mysqli->prepare("
            SELECT `txn_id`
            FROM `trans` AS t, `order_table` AS ot, `customer_order_details` AS cod
            WHERE t.transaction_id = ?
               && t.transaction_id = ot.transaction_id
               && ot.customer_order_details_id = cod.customer_order_details_id
               && cod.client_id = ?
          ");
          $stmt->bind_param('ii', $trans, $clientId);
          if ($stmt->execute()) {
            $stmt->store_result(); 
            $num_rows = $stmt->num_rows;
            if ($num_rows>=1) {
              $stmt->bind_result($txnidrow);
              $stmt->fetch();
              $stmt->free_result();
              $stmt->close();
              $_SESSION['CARRYTXNID'] = $txnidrow;    
              $_SESSION['txnId'] = true;
            }
          }
        } 
      }
      header('Location: ../confirmation');
      die();
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <body> 
    <div id="basket-empty" title="Success" style="display:none;">
    <p>Basket emptied</p>
    </div>
    <div id="confirm-empty" title="Confirm" style="display:none;">
    <p>Are you sure you want to empty your basket?</p>
    </div>
    <div id="page-wrapper">
        <div id="page">
            <div id="header" class="without-secondary-menu">
                    <div class="section clearfix">
                        <div id="logo-caption">
                            <a href="/" title="Home" rel="home" id="logo">
                            </a>
                        </div>
                        <div id="caption">
                        </div>
                        <div id="main-menu" class="navigation">
                            <ul id="main-menu-links" class="links clearfix">
                                <li class="menu-2 active-trail active"><a href="../index"> Order Online </a></li>
                                <li class="menu-9"><a href="../my-account"> My Account </a></li>
                                <?php if (!$_SESSION['id']) {?>
                                <li class="menu-10"><a href="../login"> Sign In </a></li>
                                <?php } ?>
                                <li class="menu-5"><a href="../getaquote"> Get a Quote </a></li>
                                <li class="menu-6"><a href="../contactus"> Contact Us </a></li>
                                <li class="menu-11"><a href="../news"> News </a></li>
                                <li class="menu-8"><a href="../uploads"> Uploads </a></li>
                                <li class="menu-7"><a href="../downloads"> Downloads </a></li>
                            </ul>           
                        </div>
                    </div>
                </div>
                <?php if (isset($_SESSION['id'])) { ?>
                    <div id="panel" class="clearfix">
                        <div id="account-links">
                            <ul class="username">
                                <li><span class="account-links"><a href="../my-account">You are logged in as <b><?php echo $_SESSION['username']; ?></b></a></span>
                                <span class="logout"><a href="../destroy">Logout</a></span></li>
                            </ul>   
                        </div>
                    </div>
                <?php } ?>  
            <div id="main-wrapper" class="clearfix">
                <div id="main" class="clearfix">
                    <div class="content">
                        <a id="main-content"></a>
                        <div class="tabs">
                        </div>
                        <div class="region region-content">
                            <div id="block-system-main" class="block block-system">
                                <div class="content">
                                    <div id="node-1" class="clearfix">
                                        <div class="content clearfix">
                                            <?php if (!isset($_SESSION['txnId'])) {
                                            if (isset($_SESSION['cart'])) { ?>
                                            <div class="loading2" style="height:300px;">
                                                <span class="text2"><font size="6pt"><b>Loading....</b></font></span>
                                                <img src="../images/ajax-loader2.gif"/><br />
                                                <font size="3pt"><b>Please wait a moment whilst we process your payment</b></font><br />
                                                <font size="1pt"><b>Click <a href="/order/complete">here</a> to reload after 10 seconds</b></font>
                                            </div>
                                            <?php }
                                            } ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div id="footer-wrapper">
                    <div class="section">
                        <div id="footer" class="clearfix">
                            <div class="region region-footer">
                                <div class="block">
                                    <div class="content">
                                        <div class="column1">
                                        </div>
                                        <div class="column2">
                                                                                    </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度