I am trying to print some information within a foreach loop:
<?php foreach($output['data']['rooms'] as $info): ?>
<?php if($info['room_number'] == $id): ?>
<h1 style="font-size: 20pt; color: white;">Room: <?php echo $info['room_number']; ?></h1>
<?php echo '<a style="float: right;" class="button1" href="'.$curIndex.'">Home</a>'; ?>
<hr style="margin-top: 20px;">
<?php $services = reset($info['services']); ?>
<h2 style="font-size: 14pt; color: #924c9e;"> Room Charges: <?php echo ($services['room_charges']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?></h2>
<h2 style="font-size: 14pt; color: #924c9e;"> Adult: <?php echo ($services['adult']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?> </h2>
<h2 style="font-size: 14pt; color: #924c9e;"> Room Status: <?php echo $info['status']; ?></h2>
<?php else : echo "No data found for the room number you entered."; ?>
<?php endif; ?>
<?php endforeach; ?>
The part that is NOT working correctly is the <?php else: "no data found for the room number you entered."; ?>
The else statement echo's that information for every entry within the foreach loop, rather than just one time. Easiest way of explaining what i'm trying to do:
if room_number matches the id the user enters, then print everything that corresponds to that room number, else print that room's data was not found. If room number not found, I only want "No data found for the room number you entered" to display one time on the page.
I can give you more code if you need it, but I think this should cover it. I just haven't been able to correctly include the else within the foreach loop.
Thank you for your help! I'll be sure to mark your answer correct if you can help me figure it out.
Generally, the way I'd go about doing this is a little bit different from what you have.
// TODO: declare a variable called matchFound and set to false
foreach($output['data']['rooms'] as $info):
if($info['room_number'] == $id):
// TODO: set matchFound = true
// TODO: print out room data
// TODO: break the foreach loop since looping through the remaining records is pointless
endif;
endforeach;
// TODO: if the matchFound variable is false, then print the "No data found for the room number you entered" message
Let me know if you need clarification. It's been a while since I used PHP, so I'm a bit rusty on the syntax, but I could probably figure out how to write the "TODO:" lines if you can't figure it out yourself.
Edit, I think this is correct syntax and it should work:
<?php $matchFound = false; ?>
<?php foreach($output['data']['rooms'] as $info): ?>
<?php if($info['room_number'] == $id): ?>
<?php $matchFound = true; ?>
<h1 style="font-size: 20pt; color: white;">Room: <?php echo $info['room_number']; ?></h1>
<?php echo '<a style="float: right;" class="button1" href="'.$curIndex.'">Home</a>'; ?>
<hr style="margin-top: 20px;">
<?php $services = reset($info['services']); ?>
<h2 style="font-size: 14pt; color: #924c9e;"> Room Charges: <?php echo ($services['room_charges']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?></h2>
<h2 style="font-size: 14pt; color: #924c9e;"> Adult: <?php echo ($services['adult']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?> </h2>
<h2 style="font-size: 14pt; color: #924c9e;"> Room Status: <?php echo $info['status']; ?></h2>
<?php break; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php if (!$matchFound): ?>
<?php echo "No data found for the room number you entered."; ?>
<?php endif; ?>
If I understand the situation correctly, you'd like to stop looping once you identify that the room is not found.
If so, do this (I added a break statement in the else condition). You may need to encapsulate what do do when the else condition is met with { }. I prefer the if (condition) {do this first; do this second;} syntax.
Whatever the case, adding the break statement as part of the code to run when it hits the else condition should fix the issue.
<?php foreach($output['data']['rooms'] as $info): ?>
<?php if($info['room_number'] == $id): ?>
<h1 style="font-size: 20pt; color: white;">Room: <?php echo $info['room_number']; ?></h1>
<?php echo '<a style="float: right;" class="button1" href="'.$curIndex.'">Home</a>'; ?>
<hr style="margin-top: 20px;">
<?php $services = reset($info['services']); ?>
<h2 style="font-size: 14pt; color: #924c9e;"> Room Charges: <?php echo ($services['room_charges']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?></h2>
<h2 style="font-size: 14pt; color: #924c9e;"> Adult: <?php echo ($services['adult']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?> </h2>
<h2 style="font-size: 14pt; color: #924c9e;"> Room Status: <?php echo $info['status']; ?></h2>
<?php else : echo "No data found for the room number you entered."; break;?>
<?php endif; ?>
<?php endforeach; ?>
Related
I have a question. Heres my code
<p><strong style="color: blue;">IF</strong></p>
<?php foreach ($data as $i => $key) : ?>
<p><?php echo $key['symptoms'] ?></p>
<p><strong style="color: green;">AND</strong></p>
<?php endforeach ?>
<p style="color: red;"> <strong>THEN</strong></p>
<p><?php echo $key['disease_name'] ?></p>
But, The output is like this
IF
symptoms A
AND
symptoms B
AND
symptoms C
AND
THEN
disease A
I want the last AND to be disappear. Can anyone help me pls? :( I'm sorry because I'm a newbie in here. Thanks!
You need to check if $i is less than the last element in the array: $i < count($data) - 1. Here is an example:
<p><strong style="color: blue;">IF</strong></p>
<?php foreach ($data as $i => $key) : ?>
<p><?php echo $key['symptoms'] ?></p>
<!-- only print AND if it is not the last element -->
<?php if ($i < count ($data) - 1) { ?>
<p><strong style="color: green;">AND</strong></p>
<?php } ?>
<?php endforeach ?>
<p style="color: red;"> <strong>THEN</strong></p>
<p><?php echo $key['disease_name'] ?></p>
<?php if($notification['user_status']==0){?>
<li style="background-color: #edf2fa;">
<?php } else { ?>
<li style="background-color: white;">
<?php } ?>
<a id="notification_list" href="index.php?page=<?php echo $page?>&act=notification">
<?php echo $notefor; ?><small style="color:#AAA; padding-left:10px;">
<?php echo $time_frame;?></small>
</a></li>
The first if statement will run fine but the else will not run, is there a way to do something similar.
Sure,
But try to put the color in a separate variable, and do the PHP logic first, and then use the variable once, instead of rewriting the whole li:
example:
<?php
$color = 'white';
if($notification['user_status']==0){
$color = '#edf2fa';
}
?>
<li style="background-color: <?php echo $color; ?>;">
Assuming you are running php 5.4 >
First of all this code could use some cleanup.
<?php
if ( $notification['user_status'] == 0 ) {
$backgroundColor = '#edf2fa';
}else {
$backgroundColor = '#FFFFFF';
}
?>
<li style="background-color: <?= $backgroundColor ?>;">
<a id="notification_list" href="index.php?page=<?= $page ?>&act=notification">
<?= $notefor; ?>
<small style="color:#AAA; padding-left:10px;"><?= $time_frame;?></small>
</a>
</li>
The else codeblock will only run if the result of the expresssion inside the if statement equates to false. so if you would kindly check if $notification['user_status'] actually is unequal to 0 in the case you expect the else code to run.
You can use the if statement inside something like the following:
<li style="background-color: <?php ($notification['user_status'] == 0) ? print '#edf2fa' : print 'white' ?>">
<a id="notification_list" href="index.php?page=<?php echo $page ?>&act=notification">
<?php echo $notefor; ?>
<small style="color:#AAA; padding-left:10px;">
<?php echo $time_frame; ?></small>
</a>
</li>
Nobody that can help me? (update 17-02)
Basicly what I need it the following.
Get the qty of stock of a product
Show qty number X
When negative number show 0
When it's > 0 show image X
When it's < 0 show image Y
Old info
In magento I am showing the stock qty with the actual numbers.
When the number is lower then 0 he always shows 0.
What I now want to do is add an image to both conditions.
For example when the qty is > 0 show a green image and when the qty = 0 then show a red image. Anybody knows how to do this?
<div class="qty-amount2">
<?php //echo (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); ?>
<?php $_op_voorraad = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
if($_op_voorraad < 0){
$_op_voorraad = 0;
}
echo $_op_voorraad; ?><span><?php echo $this->__(' op voorraad') ?></span>
Ok got this fixed by hiring a developer but will share it
?>
<?php /* #var $this Mage_Catalog_Block_Product_View_Abstract */?>
<?php $_product = $this->getProduct() ?>
<?php if($_product->isSaleable()): ?>
<?php $_op_voorraad = (int) Mage::getModel('cataloginventory/stock_item')- >loadByProduct($_product)->getQty();?>
<?php if($_op_voorraad > 0): ?>
<div class="qty-amount2"><img style="float: left;" src="image_path" alt="">
<?php echo $_op_voorraad; ?><span><?php echo $this->__(' op voorraad') ?></span>
</div>
<?php else: ?>
<?php $_op_voorraad = 0;?>
<div class=" qty-amount2 geen"><img src="image_path" alt="">
<?php echo $_op_voorraad; ?><span><?php echo $this->__(' op voorraad') ?></span>
</div>
<?php endif;?>
<?php else: ?>
<div class="geen"><img src="image_path" alt="">
<p><?php echo $this->__('Availability: Out of stock.') ?></p></div>
<?php endif; ?><br>
<?php echo $this->getPriceHtml($_product) ?>
<?php
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
if ($_product->isAvailable()):
if(intval($stock)>0){
//green image code
}else{
//red image code
}
endif;
?>
You don't need to know the stock quantity.
If the item is in Out of Stock the isSaleable() method return 'false'. You can use this method.
For Example:
<?php if($_product->isSaleable()): ?>
<img src="[linkimage1]" width="135" height="135" alt="" />
<?php else: ?>
<img src="[linkimage2]" width="135" height="135" alt="" />
<?php endif; ?>
Remember to enable the Out of Stock visibility on frontend:
from admin:System->Catalog\Inventory->Stock Options->Display Out of Stock Products ('Yes')
Reindex all.
I have this code printing the output of a list of best selling products. The problem is it spans horizontally without breaking and goes off the side of the screen after 7 products.
What I'd like to do, and am completely stymied as my attempts have been fruitless, is to have the 8th to 14th entries start a new row. How do I need to modify my code in order to accomplish this?
<?php $products = $this->getCollection(); ?>
<?php if ($products && $products->count() > 0) { ?>
<div class="block block-list block-viewed">
<div class="block-title">
<strong><span><?php echo $this->__($this->getHeader()) ?></span></strong>
</div>
<div class="block-content">
<table class="amsorting-table">
<tr>
<?php foreach ($products as $p) { ?>
<td style="padding: 15px 15px 0px 15px;">
<a href="<?php echo $p->getProductUrl() ?>" title="<?php echo $this->htmlEscape($p->getName()) ?>" class="product-image"><img
src="<?php echo $this->helper('catalog/image')->init($p, 'small_image')->resize(125) ?>" width="125" height="125" alt="<?php echo
$this->htmlEscape($p->getName()) ?>" /></a>
<h3 class="product-name"><a href="<?php echo $p->getProductUrl() ?>" title="<?php echo $this->htmlEscape($p->getName())
?>"><?php echo $this->htmlEscape($p->getName()) ?></a></h3>
<?php echo $this->getPriceHtml($p, true) ?>
</td>
<?php } ?>
</tr>
<tr>
<?php foreach ($products as $p) { ?>
<td style="padding: 0px 15px 15px;">
<?php if($p->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart"
onclick="setLocation('<?php echo $this->getAddToCartUrl($p) ?>')"><span><span><?php echo $this->__('Add to Cart')
?></span></span></button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
<?php } ?>
</td>
</tr>
</table>
</div>
</div>
<?php } ?>
Do your <tr> tag conditionally, based on whether the current product index is divisible by 7
<table class="amsorting-table">
<?php
foreach ($products as $i => $p) {
if ($i % 7 == 0) { // start a new row on product 0, 7, 14, etc
if ($i > 0) { // if this is not the 1st product, close the previous row
?></tr><?php
}
?><tr><?php
}
... your code the display the table cell for the current product
}
// after the loop, we need to close the last row
?></tr><?php
...
I'm creating a feedback. I show the success message when it's good and error when there is something wrong, but my css (red and green block) is there from the beginning. How can I hide this before there is feedback?
<div id="feedback_success">
<?php if(empty($feedback_success)) { ?>
<h1 ></h1>
<?php } else { ?>
<h1><?php echo $feedback_success ?></h1>
<?php } ?>
</div>
<div id="feedback_error">
<?php if(empty($feedback_error)) { ?>
<h1 ></h1>
<?php } else { ?>
<h1><?php echo $feedback_error ?></h1>
<?php } ?>
</div>
css
#feedback_success
{
background-color: #45e589;
color: white;
padding:10px;
font-size:1.0em;
}
#feedback_error
{
background-color: #ff5555;
color: white;
padding:10px;
font-size:1.2em;
}
I think what you are asking is how to do this
<?php if(!empty($feedback_success)): ?>
<div id="feedback_success">
<h1><?php echo $feedback_success ?></h1>
</div>
<?php endif; ?>
<?php if(!empty($feedback_error)): ?>
<div id="feedback_error">
<h1><?php echo $feedback_error ?></h1>
</div>
<?php endif; ?>