重複したコードを避けながら、銀行口座が1つしかない場合、foreach
ループ内でコードを実行するにはどうすればよいですか?if 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>
'$ bank_account'と' $ bank_accounts'を区別しないでください。 '$ bank_accounts'しか持っていないので、1つの銀行口座は、単純に1つの項目を持つ銀行口座の一覧です。単一のケース== 1つのアイテムのリスト、完全に別個の論理ブランチではない。 – deceze
これをやり直すにはちょっと急進的ではなく、すぐにすぐにこの質問を数分で削除してください。 –
@ this.lau_質問は削除されません。なぜなら、upvoted受け入れられた回答があるからです。 –