How to add math in PHP echo? - php

I have a website that everything is working well except lowest price. I mean all prices will be changed based on selected currency but not lowest price.
Lowest Price is showing correctly just based on US dollar not other currencies, I mean If we will change currency to Euro, still lowest price is showing on US dollar which is default currency.
In my Sql Database I have a table pt_currencie and Column rate
and on my room page, lowest price is showing with following PHP code:
<?php echo $lowestPrice; ?>
and in the controller, the code is:
$this->data['lowestPrice'] = $this->hotels_lib->bestPrice($this->data['hotel']->id);
and here is setting for userdata and change currency
function changeCurrency($id){
$this->db->where('id',$id);
$rs = $this->db->get('pt_currencies')->result();
$this->session->set_userdata('currencycode', $rs[0]->code);
$this->session->set_userdata('currencysymbol', $rs[0]->symbol);
$this->session->set_userdata('currencyname', $rs[0]->name);
$this->session->set_userdata('currencyrate', $rs[0]->rate);
}
}
How can I show $lowestPrice based on selected currency (rate) ?
what formula should I add into above code that lowest price show based on selected currency?

Ok if I'm understanding your question correctly, you will have to take the currency rates out of the db into a php array and then determine the lowest price using php with something like this:
$query = "SELECT rate FROM pt_currencie";
$result = mysqli_query($db_conn, $query);
$rate_arr = mysqli_fetch_fields($result);
$currLowestPrice = $lowestPrice;
foreach ($rate_arr as $rate) {
$tmpLowestPrice = $lowestPrice * $rate;
if($currlowestPrice > $tmpLowestPrice)
$currLowestPrice = $tmpLowestPrice;
}
So this should atleast give you the lowest price in $currLowestPrice for adjusted to currency rates by the end of the for loop (the code above will probably have to be adjusted a bit for your program)

Related

laravel slow query issue optimization

I want to optimize my query as it is taking long to run with current eloquents. I have two table, toys and product.
From each product one is reserved as sample of toy if not than it has to be updated as sample by the query so what i'm doing right now is below.
$toywithsample=product::select('toyid')->groupBy('toyid')->where('sample','yes')->get();
Above code is to get id of all the product with which have their one sample from in its product
$toywithoutsamples=toy::select('id')->whereNotIn('id',$toywithsample)->get();
Above code is to get id of all product which have no sample toy in
foreach($toywithoutsamples as $toywithoutsample){
$product=product::where('toyid',$toywithoutsample->id)
->where('sample','sale')->limit(1)
->update(['sample'=>'yes']);
}
Below is table structure
toy table
id,name,
product
id, toyid,sample
$toys_ids_with_sample = Product::where('sample', 'yes')->get()->pluck('toyId');
// get the products grouped by toyId.
$products = Product::whereNotIn('toyId', $toys_ids_with_sample)->where('sample', 'sale')->get()->groupBy('toyId');
// get the product ids whose sample field you want to change to
// yes.
$update_product_ids = [];
foreach($products as $toyId => $products){
// We will only pick the first one, as we have to change just 1.
array_push($update_product_ids, $products->first()->id);
}
Product::whereIn('id', $update_product_ids)->update(['sample' => 'yes']);
This reduces the total number of queries.

how to calculate to get data using servqual formula in codeigniter

I have the data to be calculated using the servqual formula.
Servqual quality = perceptions - expectations / total users
First, I calculate all the active criteria
Second, I count all the users who answered the questionnaire
Third, I calculate all perceptions and expectations to get the sum of each.
Fourth I do multiplication for expectations. Because expectations are inputted by the admin. So expectations multiplied by the number of users.
Then I do the calculation with total perception minus the expectations divided by total user
Then servqual results, I update on the answer colomn in the result_jawab table.
Then I will display it according to the criteria and the results.
this my code. i dont know this code not work. please any body help me.
this image i attach for 2 table
function hasil_servqual(){
$jml_kriteria_aktif=$this->db->where('aktif','Y')->count_all_results("kriteria");
$kuisioner_aktif=$this->db->query("SELECT * FROM kuisioner WHERE aktif='Y'");
$aktif_kuisioner=$kuisioner_aktif->row();
$tenant=$this->db->query("SELECT id_tenant FROM hasil_kuisioner WHERE NOT id_tenant='0' AND id_kuisioner=$aktif_kuisioner->id_kuisioner");
$jumlah_tenant=$tenant->num_rows();
$persepsi= $this->db->query("SELECT SUM(jawaban) AS j_p FROM detail_jawaban WHERE id_kuisioner=$aktif_kuisioner->id_kuisioner");
$jumlah_persepsi= $persepsi->result_array('j_p');
$ekspektasi= $this->db->query("SELECT SUM(jawaban) AS j_e FROM detail_jawaban WHERE id_tenant='0' AND id_kuisioner=$aktif_kuisioner->id_kuisioner");
$jmlh_ekpektasi= $ekspektasi->result_array('j_e');
$jumlah_ekspektasi=("");
print_r($jumlah_ekspektasi);
$query=$this->db->query("SELECT * FROM kriteria WHERE aktif='Y' ORDER BY id_kriteria");
if ($query->num_rows()>0) {
$data=$query->result_array();
foreach ($data AS $value) {
$this->hasil.="<tr>";
$this->hasil.="<td>$value[id_kriteria]</td>";
$this->hasil.="<td>$value[kriteria]</td>";
$this->hasil.="</tr>";
}
}
}

Magento currency Converter

I am implementing custom filter by price range functionality on product list page in magento
I have multiple currency store, and base currency is INR , with other 6 to 7 currencies
I take input from price range and using following filter on product collection
$this->_productCollection = $layer->getProductCollection();
if($this->getRequest()->getParam('filterPrice')){
$baseCurrency = Mage::app()->getStore()->getBaseCurrencyCode();
$currentCurrency = Mage::app()->getStore()->getCurrentCurrencyCode();
$price = explode('-',$this->getRequest()->getParam('filterPrice'));
$min = str_replace(Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol(),'',$price[0]);
$min = $this->currencyConverter($min, $currentCurrency, $baseCurrency);
$max = str_replace(Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol(),'',$price[1]);
$max = $this->currencyConverter($max, $currentCurrency, $baseCurrency);
$this->_productCollection->addAttributeToFilter('price',array('from'=>$min,'to'=>$max));
}
Where currencyConverter function is like
public function currencyConverter($amount,$from,$to)
{
if($from!=$to){
$targetCurrency = Mage::getModel('directory/currency')->load($to);
$price = Mage::helper('directory')->currencyConvert($amount, $from, $targetCurrency);
$converted_final_price = Mage::app()->getStore()->roundPrice($price);
return $converted_final_price;
}else{
return $amount;
}
}
but I am getting following error
Undefined rate from "CAD-INR".
From other threads , I get to know that I need to setup currencies and rates from magento backend ,and I implement the same, but still error remains same.
Magento have rates only for pairs "Base currency => Display Currency".
You have Base currency "INR" and you likely have rate for pair "INR => CAD".
Your error say that your code try to get rate for "CAD" currency and in your system you have not rate for "CAD => INR".
Please, make sure, that you try to convert price in base currency to any other currency, and not between two display currencies.
But if you need this, you should use your own convert function, that calculates neccessary rate.

Day rate discount

I am creating an intranet for a vehicle hire company
I set the default price as such:
$dayRate = '90.00';
I have created a field on my table to store a discount day rate if needed, called disc_day_rate which defaults to 0.
I pull the discount price as such
$discDayRate = $hire['disc_day_rate'];
I wish to find the lowest of these two numbers, but I think the default disc_day_rate of 0 is causing issues
I have tried using min(); and if ($discDayrate == "0") methods but after finding many answers on stackoverflow without having to post my own It's time to ask for help with an elegant solution
This ensures the 0.00 will never cause you a problem.
if ($discDayRate == 0.00) { ## don't use quotes here; it should be saved as a DECIMAL or INT in the database
$the_rate = $dayRate; ## back to the default
}
else {
if ($discDayRate < $dayRate) {
$the_rate = $diskDayRate;
}
else {
$the_rate = $dayRate;
}
}
$the_rate has your desired rate.

cakephp price check

I have a matrix of inputs boxes which contain prices for dates. If there is no price in the database for a particular date the input box displays 0. I have the following code which saves into the database the prices typed into the input boxes. It does not save all the 0 values only the new prices.
Which is fine. However I have now discovered an issue. If one of the inputs dislays a value from the database, say $10 and I want to set it now to 0, the code will not do it.
It will only save if the values and above 0. I have not been able to do this final check.
The conditions for saving are
1. If the value is numeric
2. If it is 0 and already has an entry in the database then save
3. If it has no value in the database and is greater than 0
4. If it is 0 and has no value in the database then do not save
if (isset($this->data['Rate'])){
// for each rate
foreach($this->data['Rate'] as $rate_id => $room){
// for each room type
foreach($room as $room_id => $room){
$price_for_today = isset($room['Price'][$key]) ? $room['Price'][$key] : 0;
// get existing availabilities is num (get this from previous date loop)
$today = ''.$date.' 00:00:00';
$conditions = array('Availability.date' => $today,'Availability.room_id'=>$room_id);
$this->Availability->contain();
$result = $this->Availability->find('all',array('order'=>'Availability.room_id ASC', 'conditions'=>$conditions));
$avail_id = $result[0]['Availability']['id'];
// check prices
$check_prices = "SELECT * FROM prices
WHERE rate_id = '".$rate_id."' AND availability_id = '".$avail_id."'";
$prices_result = $this->Availability->query($check_prices);
// if new prices > 0.00
if($price_for_today>0 && is_numeric($price_for_today)){
// better checking needed!
if($prices_result){
$setprices = "UPDATE prices SET price = '".$price_for_today."'
WHERE rate_id = '".$rate_id."' AND availability_id = '".$avail_id."'";
$update = $this->Availability->query($setprices);
} else {
$setprices = "INSERT INTO prices (price, availability_id, rate_id)
VALUES ('".$price_for_today."', '".$avail_id."', '".$rate_id."')";
$insert = $this->Availability->query($setprices);
}
}
//$errors[] = $setprices;
} // end rooms loop
} // end Rates loop
Your problem is in
> // if new prices > 0.00
> if($price_for_today>0 &&
> is_numeric($price_for_today)){
here you specify that $prices_for_today have to be >0, so if you had a price and want to put it 0 today then you will not do anything... You should use
if(($price_for_today>0 && is_numeric($price_for_today)) || (!empty($prices_result) && $price_for_today==0 && is_numeric($price_for_today))){
if you change it it will now enter in the if and do the change.
I sugest that you do NOT use the query function unless is extremely necesary. you should create a model for price (if you haven't done that already) and then use the associations (hasMany, HasOne, HABTM) or load the model directly in the controller with $this->loadModel('Price'). Then use a find 'all' as always with conditions and fields. This recomendation is to use cake as it was intended, not indispensable. Also the save, updatefield, read can be done if you do this... leaving the checks and everything to cake.

Categories