Bruce Gust
asked on
How can I convert this switch / case dynamic into a regular if / else
I need to reconfigure this code and I want to replace the switch / case to a if / else. If all goes well, there will probably be some more questions forthcoming and you'll see why.
Thing is, I'm missing something and I need some other eyes on it. Here's the code and the corresponding page:
Here's my attempt to replace the switch / case with a regular if /else and the epic fail that results:
Here's the page:
You'll notice with my page, there are no icons. It's just a running list and I need to duplicate the look of the original page.
What am I missing?
Thing is, I'm missing something and I need some other eyes on it. Here's the code and the corresponding page:
<li class='event <?=$left?'left':'right'?> <?=$eventtype?>' data-type='<?=$eventtype=='transactions'?'transaction':$eventtype?>'>
<div class='ev_container'>
<div class='event_header'><span class='event_icon'><span class='appicons <?=strtolower($eventtype)?>'></span></span><?=ucfirst($eventtype)?></div>
<div class='event_body'>
<div class='content'>
<span class='arrow'><span class="point"><span class="timeline_stopper top"><span></span></span><span class="circle"></span><span class="timeline_stopper bottom"><span></span></span></span></span></span>
<?
switch($eventtype){
case "comment":
case "text":
case "note":
?>
<?=$content?>
<?
break;
case "statement":
$dir_exists = $demo ? true : is_dir($dir);
$file_exists = $demo ? true : file_exists($dir . $file);
if($file_exists && $dir_exists) {
?>
<a href='<?=$demo?'/admin/statement/?accountid='.$demo:'/admin/statement/load/'.$content?>' target='_statement'><img src='/assets/images/pdf_hover.png'/>View Statement</a>
<?
} else if($dir_exists) {
?>
<span class="error">Failed to send.</span>
<?
} else {
?>
<span class="pending">Attempted to send... Pending feedback.</span>
<?
}
break;
case "transaction":
?>
<div class='amount'><?=GlobalFunctions::formatMoney($amount)?><span class='float'>
<span class='payer <?=$payer?>'><?=$payer?></span><span class='transtype <?=$transactiontype?>'><?=$transactiontype?></span>
</div>
<?if($dos !== null && $dos !=='1900-01-01'){?><div class="dos"><?='DOS : ' . date('M d, Y',strtotime($dos))?></div><?}?>
<div class='encounter'><?if($encountercode != null){?>Encounter Code <?=$encountercode?><?}else{?>No encounter<?}?></div>
<div class='description'>
<?=$content?>
</div>
<?
break;
case "transactions":
?>
<ul>
<?foreach($transactions as $txn){
?>
<li>
<div class='amount'><?=GlobalFunctions::formatMoney($txn['amount'])?><span class='float'>
<span class='payer <?=$txn['payer']?>'><?=$txn['payer']?></span><span class='transtype <?=$txn['transactiontype']?>'><?=$txn['transactiontype']?></span>
</div>
<?if($txn['dos'] !== null && $txn['dos'] !=='1900-01-01'){?><div class="dos"><?='DOS : ' . date('M d, Y',strtotime($txn['dos']))?></div><?}?>
<div class='encounter'><?if($txn['encountercode'] != null){?>Encounter Code <?=$txn['encountercode']?><?}else{?>No encounter<?}?></div>
<div class='description'>
<?=$txn['content']?>
</div>
<div class='txntime'><?=Date('h:i A',strtotime($txn['eventdate']))?></div>
<?/*if(isset($txn['runningbal'])){?>
<div class='runningbal'>Running balance : <span><?=$txn['runningbal']?></span></div>
<?}*/?>
</li>
<?}?>
</ul><?
}
?></div>
<?if(isset($runningbal) && ($new_eventtype == 'transaction' || $eventtype == 'transactions')){?>
<div class='runningbal'>Running balance : <span><?=$runningbal?></span></div>
<?}?>
<div class='datefoot'>
<span class='appicons clock'></span><?=date('M d, Y'.($eventtype!='transactions'?' / h:i A':''),strtotime($eventdate))?>
</div>
</div>
</div></li>
Here's my attempt to replace the switch / case with a regular if /else and the epic fail that results:
<li class='event <?=$left?'left':'right'?> <?=$new_eventtype?>' data-type='<?=$new_eventtype=='transactions'?'transaction':$new_eventtype?>'>
<div class='ev_container'>
<div class='event_header'><span class='event_icon'><span class='appicons <?=strtolower($eventtype)?>'></span></span><?=ucfirst($eventtype)?></div>
<div class='event_body'>
<div class='content'>
<span class='arrow'><span class="point"><span class="timeline_stopper top"><span></span></span><span class="circle"></span><span class="timeline_stopper bottom"><span></span></span></span></span></span>
<?
if($eventtype=="comment" OR $eventtype=="text" OR $eventtype=="note")
{
echo $content;
}
if($eventtype=="statement")
{
$dir_exists = $demo ? true : is_dir($dir);
$file_exists = $demo ? true : file_exists($dir . $file);
if($file_exists && $dir_exists) {
?>
<a href='<?=$demo?'/admin/statement/?accountid='.$demo:'/admin/statement/load/'.$content?>' target='_statement'><img src='/assets/images/pdf_hover.png'/>View Statement</a>
<?
} else if($dir_exists) {
?>
<span class="error">Failed to send.</span>
<?
} else {
?>
<span class="pending">Attempted to send... Pending feedback.</span>
<?
}
}
if($eventtype=="transaction")
{
?>
<div class='amount'><?=GlobalFunctions::formatMoney($amount)?><span class='float'>
<span class='payer <?=$payer?>'><?=$payer?></span><span class='transtype <?=$transactiontype?>'><?=$transactiontype?></span>
</div>
<?if($dos !== null && $dos !=='1900-01-01'){?><div class="dos"><?='DOS : ' . date('M d, Y',strtotime($dos))?></div><?}?>
<div class='encounter'><?if($encountercode != null){?>Encounter Code <?=$encountercode?><?}else{?>No encounter<?}?></div>
<div class='description'>
<?=$content?>
</div>
<?php
}
?>
<?
if($eventtype=="transactions")
{
?>
<ul>
<?foreach($transactions as $txn){
?>
<li>
<div class='amount'><?=GlobalFunctions::formatMoney($txn['amount'])?><span class='float'>
<span class='payer <?=$txn['payer']?>'><?=$txn['payer']?></span><span class='transtype <?=$txn['transactiontype']?>'><?=$txn['transactiontype']?></span>
</div>
<?if($txn['dos'] !== null && $txn['dos'] !=='1900-01-01'){?><div class="dos"><?='DOS : ' . date('M d, Y',strtotime($txn['dos']))?></div><?}?>
<div class='encounter'><?if($txn['encountercode'] != null){?>Encounter Code <?=$txn['encountercode']?><?}else{?>No encounter<?}?></div>
<div class='description'>
<?=$txn['content']?>
</div>
<div class='txntime'><?=Date('h:i A',strtotime($txn['eventdate']))?></div>
<?/*if(isset($txn['runningbal'])){?>
<div class='runningbal'>Running balance : <span><?=$txn['runningbal']?></span></div>
<?}*/?>
</li>
<?}?>
</ul><?
}
?></div>
<?if(isset($runningbal) && ($eventtype == 'transaction' || $eventtype == 'transactions')){?>
<div class='runningbal'>Running balance : <span><?=$runningbal?></span></div>
<?}?>
<div class='datefoot'>
<span class='appicons clock'></span><?=date('M d, Y'.($eventtype!='transactions'?' / h:i A':''),strtotime($eventdate))?>
</div>
</div>
</div></li>
Here's the page:
You'll notice with my page, there are no icons. It's just a running list and I need to duplicate the look of the original page.
What am I missing?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Brian
PHP supports both the double pipe (||) and the OR operator - however they operate at different precedence. The Word version (OR) has a lower operator precedence than ||
In this case it makes no difference as there are no other operators in the expression.
PHP supports both the double pipe (||) and the OR operator - however they operate at different precedence. The Word version (OR) has a lower operator precedence than ||
In this case it makes no difference as there are no other operators in the expression.
ASKER
Gentlemen! Forgive me for not being more timely in closing / resolving this question. The good news is that today I turned the completed project, as far as adding an additional feature to the code that you see. Rather than trying to revamp the "switch" dynamic, I just chose to fix something that wasn't broke and kept the format the same. Initially I hesitated only because I'm not as familiar with the swtich / break approach and I was looking for ways to break things down into what was most familiar. Once I got my head around the real heavy lifting, however, I was able to move forward even with being a little guarded about the switch syntax. As always, thanks for your time and your help!
Welcome as always Bruce,
Open in new window
Open in new window
$new_eventtype vs $eventtype - can't find the definition of $new_eventtype.