Echo a different value depending on a variable value - php

I have 2 variables in phtml and want to make them a link.
<?php
$_reviewCount = $_ratingSummary->getReviewsCount();
$_reviewUrl=$_product->getRequestPath().'#reviews';
<!--Here if review count is 1 i want to show as "Review" else "Reviews" -->
<?php echo $_reviewCount ?>
<?php echo "<a href='".$_reviewUrl."'>Review</a>" ?>
<?php ($_reviewCount == 1 ) ? __('Review') : __('Reviews') ?>
But the above shows only keyword 'review' in lowercase .

Just rewrite your code to first define the singular or plural form and then echo it:
<?php
if($reviewCount==1){$text='Review';}else{$text='Reviews';}
echo "$_reviewCount $text";
?>
BTW i'd change the if condition to >1 because your code echoes Reviews if the count of reviews is 0.
<?php
if($_reviewCount>1){$text='Reviews';}else{$text='Review';}
echo "$_reviewCount $text";
?>

Related

creating a space before a variable including htmlencode str_repeat

I have a variable given to me by my cms
I want to add one space before that but have tried many things lol and all dont work, below is what I have tried so far.
<?php echo str_repeat('', 1) htmlencode('$postcode_coveringsRecord['postcode']) ?>
<?php echo htmlencode('&nbsp$postcode_coveringsRecord['postcode']) ?>
<?php echo htmlencode('. $postcode_coveringsRecord['postcode'].) ?>
<?php echo '&nbsp''htmlencode('$postcode_coveringsRecord['postcode'])' ?>
<?php echo htmlencode('&nbsp''$postcode_coveringsRecord['postcode']) ?>
How can I minipulate
where as the variable gives me one blank space prior to the variable content.
cheers for any input
emma
These ones should work
echo ' '.htmlencode($postcode_coveringsRecord['postcode']);
or
echo ' '.htmlencode($postcode_coveringsRecord['postcode']);

How to put a link passing variable inside php echo

I have a link that passes variable to another page but I want to display that link only on some condition so how to place it inside php code.
Product
<?php
echo 'Product';
?>
This doesn't work.
Simply, put your anchor link in if condition:
<?php
if (YOUR_CONDITION_HERE) {
echo 'Product';
}
?>
OR
<?php
if (YOUR_CONDITION_HERE) {
?>
Product
<?php
}
?>
Above condition will display your link only when your condition is TRUE.
Otherwise, it will hide.
Your second line is incorrect why are you opening your php tags again while you're in a echo ?
Please try the following :
<?php
echo 'Product';
?>
Your echo field should be.
Product
<?php
echo 'Product';
?>
You dont have to print echo inside3 echo so u hv to do this way!
<?php
echo 'Product';
?>

If - else php query

I need a code that pulls out the listings count for a product in order to have them in the navigation. I am trying to get the product and listing number in the navigation (if there is any), but I don't want to display something if the listings count is equals to 0.
Here follows my code:
<?php if($listingsCount = getListingsCount(0,0,2,2,1,86) > 0): ?>
Bakery
<?php echo $listingsCount['totalRecords'] ?>.
<?php else: ?>
It should be
if ( ($listingsCount = getListingsCount(0,0,2,2,1,86)) > 0)
In this case you the assignment is done first and is then compared to 0. So $listingCount contains the real count.
The way you did it, the comparison is done first and the return value (true/false) is assigned to $listingCount.
Nevertheless. If you never reach the else-part, maybe something in your method getListingsCount() is broken.
Try this:
<?php if (($listingsCount = getListingsCount(0,0,2,2,1,86)) > 0) { ?>
Bakery
<?php echo $listingsCount['totalRecords']; ?>
<?php } else { ?>
Other text
<?php }?>
Thanks guys for your answers. managed to get it working with this...
<?php
$listingsCount = getListingsCount(0,0,2,2,1,86);
if($listingsCount['totalRecords'] > 0):
?>
<dd>
Bakery
[<?php echo $listingsCount['totalRecords'] ?>]
</dd>
<?php endif; ?>

How do you stop echo of multiple values with 'foreach' in PHP?

How do you when using custom fields in Wordpress echo just the first value using foreach?
Currently the code is:
<?php for(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php endforeach; ?>
This takes the field from the wordpress page (the field is a link to another page), creates a link to that page using get_permalink but when I want to echo the page title it does it, but then it also echos all other values that are not needed.
If you just want the execute the first iteration of the loop, try this:
<?php foreach(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php break; ?>
<?php endforeach; ?>
Wouldn't it then be easier just to use the first element of the returned array? Maybe Wordpress offers other filters that return the the page's title only.
you can just add
$counter = 0;
<?php for(get_field('venue_event') as $post_object): ?>
$counter++;
if($counter == 1)
{
<?php echo get_the_title($post_object) ?>
}
<?php endforeach; ?>

Magento - How to get cart items total in header.phtml

I am using Magento eCommerce and I have modified my header.phtml via the Blank template. Code, this is my code but it shows blank.
<?php $cartQty = $this->getSummaryCount() ?>
<?php if ($cartQty>0): ?>
<?php if ($cartQty==1): ?>
<?php echo $this->__('<a class="cartgo" href="%s">(1 ITEM)</a>', $this->getUrl('checkout/cart')) ?>
<?php else: ?>
<?php echo $this->__('<a class="cartgo" href="%s">(%s ITEMS)</a>', $this->getUrl('checkout/cart')) ?>
<?php endif ?>
<?php endif ?>
There was an answer to a link before by someone called SUHUR I think, I was going to reward him with the answer but it seems he deleted his own post?
He linked to this: http://nothingtopost.wordpress.com/tag/how-to-get-total-cart-item-in-magento/
I modified my code and this works now on .phtml files.
<?php
$count = $this->helper('checkout/cart')->getSummaryCount(); //get total items in cart
$total = $this->helper('checkout/cart')->getQuote()->getGrandTotal(); //get total price
if($count==0)
{
echo $this->__('(0 ITEMS)',$count);
}
if($count==1)
{
echo $this->__('(1 ITEM)',$count);
}
if($count>1)
{
echo $this->__('(%s ITMES)',$count);
}
echo $this->__('', $this->helper('core')->formatPrice($total, false));
?>
<?php
$cartTotal = $this->helper('checkout/cart')->getQuote()->getGrandTotal();
$cartItemsCount = Mage::helper('checkout/cart')->getCart()->getItemsCount();
$cartSuffix = ($cartItemsCount != 1) ? 's' : '';
echo '<a class="cartgo" href="'.$this->getUrl('checkout/cart').'">
<strong>'.$this->__('Your basket').'</strong><br />'.
$this->__('(%s) Item%s', $cartItemsCount, $cartSuffix).
'<span>[$'.$this->helper('core')->formatPrice($cartTotal, false).']</span>
</a>';
?>
Output:
Your basket
3 Items [$32.5]
<?php
$_cartQty = Mage::getSingleton('checkout/cart')->getItemsCount();
echo $_cartQty;
?>
thats all you need for 1.7 if your already running the mage:app which you can't do anything without really.
furthermore, this only shows "item" count, not quantity.
Use the helper object to get the current cart object, and then count the number of items in the cart object.
echo Mage::helper('checkout/cart')->getCart()->getItemsCount();
More from http://www.douglasradburn.co.uk/how-to-get-number-of-cart-items-in-magento/
You can find your cart template here:
YOURSITE/app/design/frontend/YOURTHEME/default/template/checkout/cart/minicart.phtml
Within a span with the class of .count you'll see this snippet:
<span class="count"><?php echo $_cartQty; ?></span>
Replace it with this snippet and you'll get the grand total displayed instead:
<?php echo $this->helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal()); ?>
When linking to a cart, you should really use Mage::helper('checkout/cart')->getCartUrl(). The example given would not work if your site is hosted in a sub-domain.
<?php
$count = $this->helper('checkout/cart')->getSummaryCount(); //get total items in cart
$total = $this->helper('checkout/cart')->getQuote()->getGrandTotal(); //get total price
if($count==0)
{
echo $this->__('(0 ITEMS)',$count);
}
if($count==1)
{
echo $this->__('(1 ITEM)',$count);
}
if($count>1)
{
echo $this->__('(%s ITMES)',$count);
}
echo $this->__('', $this->helper('core')->formatPrice($total, false));
?>
this works for me thanx...
Use the below code in phtml file to get the no of items in cart.
<?php $helper = $this->helper('\Magento\Checkout\Helper\Cart');
$noCartItems= $helper->getSummaryCount();
echo $noCartItems;?>

Categories