How to multiply $variable that includes € with a value - php

since I'm new to PHP I have a quite simple question. After Googling and searching here on stackoverflow I still can't get it to work.
<?php
$payinfront = $product->get_price_html();
$totalprice = $payinfront * 2;
?>
<p class="price">Total price: <?php echo $totalprice ?></p>
<p class="price">Amount to pay in front: <?php echo $payinfront ?></p>
<p class="price">Amount to pay after: <?php echo $totalprice - $payinfront ?></p>
The $payinfront value does get it's value from another part of my template. Let's say it's €10,-. This is the amount people have to pay in front. When we have done the service they have to pay the other half wich is the last rule.
Thanks for helping me out!

You want to store prices as floats, not as strings, so that PHP can recognize them as numeric values, and do calculations on them. Only cast them to strings at the very last moment, when you are echoing them in your template.
<?php
$payInFront = $product->getPrice(); // should return a numeric value, instead of a string
$totalPrice = $payInFront * 2;
$payAfter = $totalPrice - $payInFront;
?>
Then, when you echo the prices, you might want to format them in a certain way, using number_format():
<p>Price: € <?php echo number_format($totalPrice, 2, ',', '.'); ?></p>

Remove the currency symbol before multiplication:
$totalprice = substr($payinfront, 1) * 2;

Like this?
<?php
$payinfront = $product->get_price_html();
$totalprice = $payinfront * 2;
?>
<p class="price">Total price: €<?php echo $totalprice ?></p>
<p class="price">Amount to pay in front: €<?php echo $payinfront ?></p>
<p class="price">Amount to pay after: €<?php echo $totalprice - $payinfront ?></p>
Note that € makes an euro sign

You first have to remove the euro sign so that the variable can be considered a number. Then you make the multiplication and concatenate the euro sign below at the html part.
<?php
$payinfront = $product->get_price_html();
$payinfront = str_replace("€","",$payinfront);
$totalprice = $payinfront * 2;
?>
<p class="price">Total price: <?php echo $totalprice ?>€</p>
<p class="price">Amount to pay in front: <?php echo $payinfront ?>€</p>
<p class="price">Amount to pay after: <?php echo $totalprice - $payinfront ?>€</p>

Related

create pagination to get database results

i want to create a pagination so here is what i tried
<section class="products">
<?php
$result_per_page=10;
$get = mysqli_query($conn," SELECT * FROM products");
$number_of_results=mysqli_num_rows($get);
if (!isset($_GET['page'])) {
$page=1;
} else{
$page=$_GET['page'];
}
$this_page_first_result=($page-1)*$result_per_page;
$get = mysqli_query($conn," SELECT * FROM products LIMIT ".$this_page_first_result.','.$result_per_page);
$number_of_results=mysqli_num_rows($get);
while ($row=mysqli_fetch_array($get)) {
$name = $row['product_name'];
$price = $row['product_price'];
$img = $row['img'];
}
$number_of_pages=ceil($number_of_results/$result_per_page);
?>
<article>
<a href="showproduct.php">
<img src="adminpanel/<?php echo $img?>" alt="" style="height:13rem;width:13rem;"></a>
<h3><?php echo $name;?></h3>
<h4>$<?php echo $price ?></h4>
Add to cart
</article>
</section>
</div>
<!-- / content -->
</div>
<?php for ($page=1; $page <=$number_of_pages ; $page++) {
echo ''.$page.'';
} ?>
</div>
<!-- / container -->
</div>
<!-- / body -->
</ul>
so here is my problem i only get 1 result from my data base no matter what i do i changed $result_per_page to random numbers some time i get another result in my database and some time give me error i tried to replace the codes around but still did not work
any one know where did i went wrong
In your while loop you're overwriting the values on each iteration. You're also only outputting one article in your HTML. Place the <article>..</article> code inside your while loop to have it printed for every iteration.
Change your while loop to:
while ($row=mysqli_fetch_array($get)) {
$name = $row['product_name'];
$price = $row['product_price'];
$img = $row['img'];
?>
<article>
<img src="adminpanel/<?php echo $img?>" alt="" style="height:13rem;width:13rem;">
<h3><?php echo $name;?></h3>
<h4>$<?php echo $price ?></h4>
Add to cart
</article>
<?php
}
and remove the single <article>...</article> block you have right now.

how to do multiplication in php and mysql

I need to multiply the price by the quantity to get the total cost but i am not sure how to do this.
This is what i have done so far:
<?php do { ?>
<p align="Center">
<?php echo $row3['unitprice'] ?>
<?php echo $row3['quantity']; ?>
</p>
<?php } while ($row3 = mysqli_fetch_assoc($resultCustomer3))?>
at the moment i am only printing out unit price and the quantity.
how would i multiply the two to get the total?
HELP please!!
Try this
<?php while ($row3 = mysqli_fetch_assoc($resultCustomer3)) {?>
<p align="Center">
<?php echo $row3['unitprice']; ?>
<?php echo $row3['quantity']; ?>
<?php echo $row3['quantity']*$row3['unitprice']; ?>
</p>
<?php } ?>
To multiply two fields in php, you need this code:
$total = $mnth_190 * $rate_190;
After having calculated the result, you can normally output the variable total!

Return a Key from the Next and Last Element in Array / Foreach Loop

I am hoping to modify the manner in which discounts are displayed for an eCommerce platform (OpenCart - PHP MVC based).
The default behaviour is that the discounts will be displayed as:
5 or more: $20.00
10 or more: $18.00
20 or more: $16.00
I would prefer:
5 - 9: $20.00
10 - 19: $18.00
20+: $16.00
Stripping out the "or more" text is simple enough through the template file (code provided below).
For all but the last element, this would require taking the quantity key ($discount['quantity']) from the next element and applying a basic maths function (- 1), then returning this new value besides the original.
For the last element, I would need to simply return the last quantity value and add "+" text.
Original code (controller):
$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);
$this->data['discounts'] = array();
foreach ($discounts as $discount) {
$this->data['discounts'][] = array(
'quantity' => $discount['quantity'],
'price' => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')))
);
}
Original code (template):
<?php if ($discounts) { ?>
<div class="discount">
<?php foreach ($discounts as $discount) { ?>
<span><?php echo sprintf($text_discount, $discount['quantity'], $discount['price']); ?></span>
<?php } ?>
</div>
<?php } ?>
Modified code to strip "or more" text from template (Note: Separate echo's used to allow table formatting - To maintain simplicity, these tags are not included):
<?php if ($discounts) { ?>
<div class="discount">
<?php foreach ($discounts as $discount) { ?>
<?php echo $discount['quantity']; ?><?php echo $discount['price']; ?>
<?php } ?>
</div>
<?php } ?>
How can I further modify this code to return the quantities in the preferred format?
Note: The arrays are quite small but I would still consider performance as a priority.
Edit:
Thank you tttony for providing the solution below. This is the code I have used in the template file for the custom table formatting (without the sprintf/formatted string function).
<?php for ($i=0; $i < count($discounts) -1; $i++) { ?>
<tr>
<td><?php echo $discounts[$i]['quantity']; ?> - <?php echo (int)$discounts[$i+1]['quantity'] - 1; ?></td>
<td><?php echo $discounts[$i]['price']; ?></td>
</tr>
<?php } ?>
<?php if (count($discounts)) { ?>
<tr>
<td><?php echo $discounts[$i]['quantity']; ?>+</td>
<td><?php echo $discounts[$i]['price']; ?></td>
</tr>
<?php } ?>
You can try this, very simple, in the product.php controller file
$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);
$discounts_formated = array();
for ($i=0; $i < count($discounts) -1; $i++) {
$discounts_formated[] = sprintf("%s - %s", $discounts[$i]['quantity'], (int)$discounts[$i+1]['quantity'] - 1);
}
// Last discount: 30+
$discounts_formated[] = sprintf("%s+", $discounts[$i]['quantity']);
var_dump($discounts_formated);
Output:
array (size=3)
0 => string '10 - 19' (length=7)
1 => string '20 - 29' (length=7)
2 => string '30+' (length=3)
EDIT:
Edit the file product.tpl, I tested with OC 1.5.6.4 and it's working
<?php if ($discounts) { ?>
<br />
<div class="discount">
<?php for ($i=0; $i < count($discounts) -1; $i++) { ?>
<?php echo sprintf("%s - %s: %s", $discounts[$i]['quantity'], (int)$discounts[$i+1]['quantity'] - 1, $discounts[$i]['price']); ?><br />
<?php } ?>
<?php if (count($discounts)) { // last discount ?>
<?php echo sprintf("%s+: %s", $discounts[$i]['quantity'], $discounts[$i]['price']); ?><br />
<?php } ?>
</div>
<?php } ?>
Will print something like this:
10 - 19: $88.00
20 - 29: $77.00
30+: $66.00

How do I display a WAS and NOW price on my product page?

I've been having this problem for a while now. I use Magento with Bundle Products on this website:
http://canjicabrasil.com/bikinis/wave-brazilian-white-bikini.html
But when I apply a discount, it only shows the final price not a WAS and NOW like other sites.
What code should I use to call Prices in a way that:
If price is full it will display only one price
If Price has discount (on any of the budle items) it will display a WAS and a NOW price in the frontend of the site.
Anyone has come up with this solution?
This is the code on bundle.phtml
<?php endif; ?>
<?php if ($_product->isAvailable()): ?>
<p class="availability in-stock"><?php echo $this->helper('catalog')->__('Availability:') ?> <span><?php echo $this->helper('catalog')->__('In stock') ?></span></p>
<?php else: ?>
<p class="availability out-of-stock"><?php echo $this->helper('catalog')->__('Availability:') ?> <span><?php echo $this->helper('catalog')->__('Out of stock') ?></span></p>
<?php endif; ?>
<div class="price-box-bundle">
<?php echo $this->getPriceHtml($_product) ?>
</div>
<?php echo $this->getChildHtml('bundle_prices') ?>
without looking at the code to see how you applying discounts and generating the new discounted values, i can't give you an exact answer.
but you can keep track of the original price, then when the discount is applied, display the original value then the new discounted value.
for example.
$originalPrice = 200;
function getDiscount($value,$originalValue){
return ($originalValue - $value);
}
echo 'Was: ';
echo '<s>'.$originalPrice.'</s>;
echo 'Now: '.$getDiscount(50,$originalPrice);
Obviously you code will be different but this is a general overview.

cannot render the weight

I'm working on a website. My client want me to show the weight of product. But the condition is if the weight is less than a Kg, it should show the grams. If it is more than a kg it should show the kg along with fraction point i,e if the weight is 1 kg 350 grams then it should show 1.35 Kg. My code for it goes like this.
<?php $_weight = $_product->getweight()?>
<?php if ($_weight >= 1) : ?>
<span><?php echo($_weight)?>Kg<span>
<?php else: ?>
<? $_weight1=($_weight * 1000) ?>
<span><?php echo(round($_weight1))?>grams<span>
<?php endif;?>
<br/>
But I don't know how to show the kg with only two fraction numbers. Please help me. I'm a budding developer.
You almost got there - when you use the round() function you can specify the decimal precision as the 2nd parameter.
<span><?php echo($_weight)?>Kg<span>
becomes
<span><?php echo(round($_weight,2))?>Kg<span>
http://php.net/round
<? $_weight1=($_weight * 1000) ?> line replace with <?php $_weight1=($_weight * 1000) ?> and try...
<?php $_weight = $_product->getweight(); ?>
<?php if ($_weight >= 1) { ?>
<span><?php echo round($_weight,2); ?>Kg<span>
<?php }else{ ?>
<?php $_weight1= round($_weight * 1000); ?>
<span><?php echo $_weight1; ?>grams<span>
<?php } ?>
<br/>

Categories