Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php if语句跳过foreach,但仍在其中运行代码_Php - Fatal编程技术网

Php if语句跳过foreach,但仍在其中运行代码

Php if语句跳过foreach,但仍在其中运行代码,php,Php,当我只有一个银行帐户时,如何在foreach循环中运行代码,同时避免重复代码 <?php if($_GET['bank_id']>0){ $id = ($_GET['bank_id']); $bank_account = BankAccounts::find_by_id($id); }else{ $bank_accounts = BankAccounts::find_all(); } ?>

当我只有一个银行帐户时,如何在
foreach
循环中运行代码,同时避免重复代码

<?php 
    if($_GET['bank_id']>0){
        $id = ($_GET['bank_id']);
        $bank_account = BankAccounts::find_by_id($id); 
    }else{
        $bank_accounts = BankAccounts::find_all(); 
    }
?>

    <table class="bordered">
      <tr>
        <th>accounts id</th>    
        <th>accounts name</th>  
        <th>accounts number</th>    
        <th>account</th>    
      </tr>

    <?php 
    if(isset($bank_accounts)){
    foreach($bank_accounts as $bank_account){
        ?>
      <tr>
        <th><?php echo $bank_account -> bank_accounts_id; ?> </th>  
        <th><?php echo $bank_account -> bank_accounts_name; ?> </th>    
        <th><?php echo $bank_account -> bank_accounts_number; ?> </th>  
        <th><?php echo $bank_account -> bank_account; ?> </th>  
      </tr>
    <?php
        } 
        }else{
    ?>
        <tr>
        <th><?php echo $bank_account -> bank_accounts_id; ?> </th>  
        <th><?php echo $bank_account -> bank_accounts_name; ?> </th>    
        <th><?php echo $bank_account -> bank_accounts_number; ?> </th>  
        <th><?php echo $bank_account -> bank_account; ?> </th>  
      </tr>
 <?php  } ?>
    </table>

帐户id
帐户名
帐号
账户

如果您只有一个银行帐户,请将其添加到一个包含一个元素的数组中。这允许您避免重复代码,并意味着0、1或多个银行账户将以统一的方式处理:

<?php 
  $bank_accounts = array();
  if($_GET['bank_id']>0){
    $id = ($_GET['bank_id']);
    $bank_account = BankAccounts::find_by_id($id); 
    $bank_accounts[] = $bank_account;
  } else {
    $bank_accounts = BankAccounts::find_all(); 
  }
?>

<table class="bordered">
  <tr>
    <th>accounts id</th>    
    <th>accounts name</th>  
    <th>accounts number</th>    
    <th>account</th>    
  </tr>

  <?php foreach($bank_accounts as $bank_account): ?>
    <tr>
      <th><?php echo $bank_account -> bank_accounts_id; ?> </th>  
      <th><?php echo $bank_account -> bank_accounts_name; ?> </th>    
      <th><?php echo $bank_account -> bank_accounts_number; ?> </th>  
      <th><?php echo $bank_account -> bank_account; ?> </th>  
    </tr>
  <?php endforeach; ?>
</table>

帐户id
帐户名
帐号
账户

也考虑在HTML代码中使用<代码> Frace/EndoFrace<代码>,因为它更可读。只有

$bank\u accounts
,一个银行账户就是一个银行账户列表,其中只有一个项目。单例==1项列表,不是完全独立的逻辑分支。现在结束并在几分钟内删除这个问题,而不留下任何时间来改进它,这不是有点激进吗?@this.lau u u这个问题不会被删除,因为有一个经过投票的公认答案为什么不简单地
$back\u accounts=[bankacounts::find\u by\u id($id)]
?@deceze,一般来说,我更喜欢在需要的范围内定义变量(例如$bank_accounts),以避免代码变得更复杂时出现未定义的变量问题。在这种情况下,它确实相当于使用您的方法。@this.lau uu感谢它的有效性。正如您所提到的,我最初有foreach