简体   繁体   English

if else 语句 js 嵌套在 php while 循环语句中 - 结果出现在循环之外/未出现

[英]if else statement js nested inside php while loop statement - result appearing outside of loop/ not showing up

This is my code - I am trying to add a class upon a boolean result that is coming from the database.这是我的代码 - 我试图在来自数据库的布尔结果上添加一个类。 But when I run my code the echo'ing is happening outside of my loop and where it is supposed to be echo'ed is blank.但是当我运行我的代码时, echo'ing 发生在我的循环之外,并且它应该被 echo'ed 的地方是空白的。 I believe this to be syntax but can't seem to find the answer while combing through other answers.我相信这是语法,但在梳理其他答案时似乎无法找到答案。

while($record = mysqli_fetch_assoc($results))
{

    $invoice .= '<tr>
                    <th scope="row" class="displayField" ><a href="orderView.php?invoiceID='.$record['id'].'">'.$record['nOrderNum'].'</a></th>
                    <td class="displayField"><a href="#">'.$record['strOrderDate'].'</a></td>
                    <td class="displayField"><a href="#">$'.$record['nPrice'].'</a></td>
                    <td class="displayField"> 
               <div class="toggle-btn ';
                if($record['bOrderToggle'] != 1){
                   echo('');
                } else {
                       echo('active');
                   }
               $invoice .= '"><input id="switch" type="checkbox" checked class="checking" name="nInvoiceID" />
               <span class="round-btn"></span>
               </div>
            </tr>';
}

Try this:尝试这个:

<?php while($record = mysqli_fetch_assoc($results)): ?>
    <tr>
        <th scope="row" class="displayField" ><a href="orderView.php?invoiceID=<?= $record['id'] ?>"><?=$record['nOrderNum']?></a></th>
        <td class="displayField"><a href="#"><?= $record['strOrderDate'] ?></a></td>
        <td class="displayField"><a href="#">$<?= $record['nPrice'] ?></a></td>
        <td class="displayField"> 
            <div class="toggle-btn <?= ($record['bOrderToggle'] != 1)? '': 'active' ?>">
                <input id="switch" type="checkbox" checked class="checking" name="nInvoiceID" />
                <span class="round-btn"></span>
            </div>
        </td>
 </tr>
<?php endwhile; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM