I would like your suggestions

Hello,
Sorry I do not speak English
My local currency TL Turkish lira

Products registered in the database
Sample.
id - product1 - 10 - $ (USD)
id - product2 - 12 - € (EURO)
id - product3 - 15 - TL (Turkish lira)

The products are registered with different currencies
AND
Also registered in the database of exchange rates

My question is:
Bring me products in USD
OR
Bring me products in EURO
OR
Bring me products in TL

How should an algorithm to convert money(s)?

Thank you from now

Please use ONE currency in your database to keep your data consistence. With PHP you can easily calculate a value to another currency.

$price = 1.25;
$dollarRate = 1.10999;
$priceInDollars = $price * $dollarRate ;
echo 'USD ' . number_format($priceInDollars, 2);
1 Like

I created a sample database
exchange_rates
products

currencies will not be used in php codes
Bring products:
echo USD
echo EURO
echo TL

you could loop thru all of the items in the schema, and then check what currencies the item have and from there convert to the currency you want

if(currency == USD OR currency == EURO) {
   convert currency to turkish lira
} else if(currency == lira){
   do nothing
}

I’ve done this, but lots of codes.
I wonder is there a shorter code for this job?

are you using anfor loop to loop thru them all?

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if ($result = $mysqli->query("SELECT Code, Name FROM Country ORDER BY Name")) {

    /* determine number of rows result set */
    $row_cnt = $result->num_rows;

    printf("Result set has %d rows.\n", $row_cnt);

    /* close result set */
    $result->close();
}

for($i=0; $i<$row_cnt); $i++){
   if(currency[$i] == USD OR currency[$i] == EURO) {
      convert currency[$i] to turkish lira
   }
}

EDIT: sorry, mysql_num_rows should easily give you amount of db-rows. i edited my post

PS thats just an example code you obv have to change the query etc

For the sample product information, it is not clear what result you want to display. I think you want to display the price for each product in all three currencies? Could you post an example of the output you want?

Since I don’t speak English, I will try to explain short word by word
There are multiple products in the Database
some currency USD, some currency EURO, some currency TL
So, currencies mixed
So far okay no problem

We are preparing price quotation of products according to customer request
select currency
choose%20currency
Sample Price Offers
EURO

USD

TL
I for each product group separately
if(){
}else{
}
have very long codes like, Could it be a shorter code?
Sample,
Currency for the quote
$selected_currency = “USD”;
or
$selected_currency = “EURO”;
or
$selected_currency = “TL”;

The code I use for a group of products

    // istenen para cinsi ile ürün para cinsi aynı ise USD, EUR, TL
    if($teklifkayitlari['secilen_paracinsi_id']=='USD' AND $teklifdvr['dvr_para_birimi']=='USD' OR $teklifkayitlari['secilen_paracinsi_id']=='EUR' AND $teklifdvr['dvr_para_birimi']=='EUR' OR $teklifkayitlari['secilen_paracinsi_id']=='TL' AND $teklifdvr['dvr_para_birimi']=='TL'){
    $toplamdvrfiyat=$teklifdvr['dvr_fiyati']*$row['dvr_adet'];
    $toplamdvrfiyati=number_format($toplamdvrfiyat,2,",",".");
    $dvrbirimfiyat=$teklifdvr['dvr_fiyati'];
    $dvrbirimfiyati=number_format($dvrbirimfiyat,2,",",".");
    }
    // istenen EUR, ürün TL ise
    if($teklifkayitlari['secilen_paracinsi_id']=='EUR' AND $teklifdvr['dvr_para_birimi']=='TL'){
    $dvrfiyati=$teklifdvr['dvr_fiyati']*$row['dvr_adet'];
    $toplamdvrfiyat=($dvrfiyati/$EUR_KUR);
    $toplamdvrfiyati=number_format($toplamdvrfiyat,2,",",".");
    $dvrbirimfiyat=($teklifdvr['dvr_fiyati']/$EUR_KUR);
    $dvrbirimfiyati=number_format($dvrbirimfiyat,2,",",".");
    }
    // istenen TL, ürün EUR ise
    if($teklifkayitlari['secilen_paracinsi_id']=='TL' AND $teklifdvr['dvr_para_birimi']=='EUR'){
    $dvrfiyati=$teklifdvr['dvr_fiyati']*$row['dvr_adet'];
    $toplamdvrfiyat=$dvrfiyati*$EUR_KUR;
    $toplamdvrfiyati=number_format($toplamdvrfiyat,2,",",".");
    $dvrbirimfiyat=$teklifdvr['dvr_fiyati']*$EUR_KUR;
    $dvrbirimfiyati=number_format($dvrbirimfiyat,2,",",".");
    }
    // istenen USD, ürün TL ise
    if($teklifkayitlari['secilen_paracinsi_id']=='USD' AND $teklifdvr['dvr_para_birimi']=='TL'){
    $dvrfiyati=$teklifdvr['dvr_fiyati']*$row['dvr_adet'];
    $toplamdvrfiyat=($dvrfiyati/$USD_KUR);
    $toplamdvrfiyati=number_format($toplamdvrfiyat,2,",",".");
    $dvrbirimfiyat=($teklifdvr['dvr_fiyati']/$USD_KUR);
    $dvrbirimfiyati=number_format($dvrbirimfiyat,2,",",".");
    }
    // istenen TL, ürün USD ise
    if($teklifkayitlari['secilen_paracinsi_id']=='TL' AND $teklifdvr['dvr_para_birimi']=='USD'){
    $dvrfiyati=$teklifdvr['dvr_fiyati']*$row['dvr_adet'];
    $toplamdvrfiyat=$dvrfiyati*$USD_KUR;
    $toplamdvrfiyati=number_format($toplamdvrfiyat,2,",",".");
    $dvrbirimfiyat=$teklifdvr['dvr_fiyati']*$USD_KUR;
    $dvrbirimfiyati=number_format($dvrbirimfiyat,2,",",".");
    }
    // istenen USD, ürün EUR ise
    if($teklifkayitlari['secilen_paracinsi_id']=='USD' AND $teklifdvr['dvr_para_birimi']=='EUR'){
    $dvrfiyati=$teklifdvr['dvr_fiyati']*$row['dvr_adet'];
    $toplamdvrfiyat=($dvrfiyati/$BIR_USD_EUR);
    $toplamdvrfiyati=number_format($toplamdvrfiyat,2,",",".");
    $dvrbirimfiyat=($teklifdvr['dvr_fiyati']/$BIR_USD_EUR);
    $dvrbirimfiyati=number_format($dvrbirimfiyat,2,",",".");
    }
    // istenen EUR, ürün USD ise
    if($teklifkayitlari['secilen_paracinsi_id']=='EUR' AND $teklifdvr['dvr_para_birimi']=='USD'){
    $dvrfiyati=$teklifdvr['dvr_fiyati']*$row['dvr_adet'];
    $toplamdvrfiyat=($dvrfiyati/$BIR_EUR_USD);
    $toplamdvrfiyati=number_format($toplamdvrfiyat,2,",",".");
    $dvrbirimfiyat=($teklifdvr['dvr_fiyati']/$BIR_EUR_USD);
    $dvrbirimfiyati=number_format($dvrbirimfiyat,2,",",".");
    }

could you please post your code inside of [CODE][/CODE] ? itll make it easier for us to read it.

Hm i am not really sure what you want to accomplish and what the problem is. it works as you want it to but you just want to make the code shorter?

i assume you have every seperate currency in a sperate table?

To get the correct currency based on what the user want, you could do something like this:

if(user_currency_setting == EUR) {
  $currency = 'EUR';
} else if(user_currency_setting == USD){
  $currency = 'USD';
} else {
  $currency = 'TL';
}

$sql = "SELECT * FROM $currency";

this code works if your db-table is named after the currency

To perform the conversion for display, you would write a function that converts the product price from whatever currency_unit it is in, to the selected currency unit. See the following example code -

<?php

/*

products that are selected for a quote
id	name	price	currency_unit
1	prod 1	10		USD (58.31 TL)
2	prod 2	9		EURO (57.68 TL)
3 	prod 3	20		TL

conversion table - note: there is no need to repeat every combination of from/to values. just enter one conversion factor. the convert function takes care of multiplying or dividing by the conversion factor.
id	from	to		cf (conversion factor)
1	USD		TL		5.8313
2	EURO	TL		6.4087
3	USD		EURO	.9030

*/

// example product information to be displayed
$products = [];
$products[] = ['id'=>1, 'name'=>'prod 1', 'price'=>10, 'currency_unit'=>'USD'];
$products[] = ['id'=>2, 'name'=>'prod 2', 'price'=>9, 'currency_unit'=>'EURO'];
$products[] = ['id'=>3, 'name'=>'prod 3', 'price'=>20, 'currency_unit'=>'TL'];


$selected_currency = 'TL'; // the selected currency value from the select/option menu

// $cf is an array containing the conversion factors - the from|to values are used as an index and the cf (conversion factor) is the value
// you would query the conversion database table to build the following array of data
$cf = [];
$cf['USD|TL'] =	5.8313;
$cf['EURO|TL'] = 6.4087;
$cf['USD|EURO'] = .9030;


// convert function -
// $cf is an array containing the conversion factors as described above
// $product is an array with the price and currency_unit for one product
// $selected_currency is the selected currency_unit from the select/option menu
// the function returns the converted price
function convert($cf,$product,$selected_currency)
{
	// if the product currency is the same as the selected currency, return the product price
	if($product['currency_unit'] == $selected_currency)
	{
		return $product['price'];
	}
	// the currency_unit is different, convert the price
	// if the product currency|selected currency matches a from|to entry in $cf, multiple by the factor
	$index = $product['currency_unit'] .'|'. $selected_currency;
	if(isset($cf[$index]))
	{
		return $product['price'] * $cf[$index];
	}
	// if the selected currency|product currency matches a from|to entry in $cf, divide by the factor
	$index = $selected_currency .'|'. $product['currency_unit'];
	if(isset($cf[$index]))
	{
		return $product['price'] / $cf[$index];
	}
}


// loop over the example data and display the result
foreach($products as $product)
{
	$price = convert($cf,$product,$selected_currency);
	$price = number_format($price,2);
	
	echo "id: {$product['id']}, name: {$product['name']}, price: $price $selected_currency<br>";
}

The above produces this output -

id: 1, name: prod 1, price: 58.31 TL
id: 2, name: prod 2, price: 57.68 TL
id: 3, name: prod 3, price: 20.00 TL

But still it should be better to hold one currency in your database. Only then you will be able to use aggregate functions like MAX, MIN, AVG and COUNT with one query.

1 Like

There are currencies in the database.
Sample picture in the message above

I mean this
For each product, I think the loop for each currency is wrong
I’m a beginner, I’ll try to test your sample code review
I’ll work on the sample code
Thank you for now

The sample code works perfectly.
I will try to apply it to my code
I have a question
There is a loop for each product
Sample

while ($row = $dish->fetch_assoc()) {
// Here your sample code
}
while ($row = $receiver->fetch_assoc()) {
// Here your sample code
}
while ($row = $lnb->fetch_assoc()) {
// Here your sample code
}
while ($row = $cable->fetch_assoc()) {
// Here your sample code
}
while ($row = $mdulnb->fetch_assoc()) {
// Here your sample code
}
while ($row = $hdd->fetch_assoc()) {
// Here your sample code
}
while ($row = $multiswitch->fetch_assoc()) {
// Here your sample code
}
......

Do you need to enter your sample code into each loop?
OR ?

for what is this looping? Looks very redundant.

For creating table rows AND money conversion factor
This loop has the following
Product brand | Name of the product | number of products | unit price | discount% | aggregate amount
Sample
Merkezi%20Uydu%20Sistemi%20Fiyat%20%C3%87%C4%B1karma%20Web%20Sitesi

Repeating code for every possible choice is a bad design. You would use one sql query that gets all the data you want, sorted/ordered the way you want it.

You would call the convert() function at any point where you want to get the price in the selected currency.

Thanks for your help

amateurish
I’ll try to fix it as I learn

Creating a conversion factor

$doviz_kurlari = $mysqli->query("SELECT * FROM dovkur ORDER BY ID ASC");
             $cf = [];
             while($sonuc = $doviz_kurlari->fetch_assoc()) {
                  if($genelayarlar['dovizkuru_mb_dan']=='1'){
             $cf[''.$sonuc['doviz_cinsi'].'|'.$sonuc['birime'].''] =	$sonuc['tcmb_kur'];
                  }elseif($genelayarlar['dovizkuru_mb_dan']=='0'){
             $cf[''.$sonuc['doviz_cinsi'].'|'.$sonuc['birime'].''] =	$sonuc['elle_kur'];
                  }
              }

I create a series of products in each cycle. OK, no problem,

$urun = [];
while ($row = $dish->fetch_assoc()) {

      $urunler[] = ['marka'=>$tekliffkonnektor['urun_markasi'], 
                    'urun_adi'=>$tekliffkonnektor['fkablo_tipi'].' '.$tekliffkonnektor['fkonnektor_adi'], 
                    'adet'=>$toplam_fkonnektorsayisi,
                    'b_fiyati'=>$tekliffkonnektor['fkonnektor_fiyati'],
                    'para_birimi'=>$tekliffkonnektor['fkonnektor_para_birimi'],
                    'iskonto'=>$teklifkayitlari['bayi_iskonto']];
                    
}
while ($row = $receiver->fetch_assoc()) {

      $urunler[] = ['marka'=>$tekliffkonnektor['urun_markasi'], 
                    'urun_adi'=>$tekliffkonnektor['fkablo_tipi'].' '.$tekliffkonnektor['fkonnektor_adi'], 
                    'adet'=>$toplam_fkonnektorsayisi,
                    'b_fiyati'=>$tekliffkonnektor['fkonnektor_fiyati'],
                    'para_birimi'=>$tekliffkonnektor['fkonnektor_para_birimi'],
                    'iskonto'=>$teklifkayitlari['bayi_iskonto']];
                    
}
while ($row = $lnb->fetch_assoc()) {

      $urunler[] = ['marka'=>$tekliffkonnektor['urun_markasi'], 
                    'urun_adi'=>$tekliffkonnektor['fkablo_tipi'].' '.$tekliffkonnektor['fkonnektor_adi'], 
                    'adet'=>$toplam_fkonnektorsayisi,
                    'b_fiyati'=>$tekliffkonnektor['fkonnektor_fiyati'],
                    'para_birimi'=>$tekliffkonnektor['fkonnektor_para_birimi'],
                    'iskonto'=>$teklifkayitlari['bayi_iskonto']];
                    
}
function convert($cf,$urun,$secilen_paracinsi_id)
{
	// if the product currency is the same as the selected currency, return the product price
	if($product['para_birimi'] == $secilen_paracinsi_id)
	{
		return $urun['b_fiyati'];
	}
	// the currency_unit is different, convert the price
	// if the product currency|selected currency matches a from|to entry in $cf, multiple by the factor
	$index = $urun['para_birimi'] .'|'. $secilen_paracinsi_id;
	if(isset($cf[$index]))
	{
		return $urun['b_fiyati'] * $cf[$index];
	}
	// if the selected currency|product currency matches a from|to entry in $cf, divide by the factor
	$index = $secilen_paracinsi_id .'|'. $urun['para_birimi'];
	if(isset($cf[$index]))
	{
		return $urun['b_fiyati'] / $cf[$index];
	}
}
<div align="center">
	<table border="1" style="width: 793px;">
	<tr>
		<th>MARKA</th>
		<th>ÜRÜN ADI & AÇIKLAMA</th>
		<th>MİKTAR</th>
		<th>B.FİYATI</th>
		<th>ISK &</th>
		<th>TOPLAM FİYAT</th>
	</tr>

foreach($urunler as $urun)
{
	$fiyat = convert($cf,$urun,$secilen_paracinsi_id);
	$fiyati = number_format($fiyat,2,",",".");
	$toplamfiyat =  $fiyat * $urun['adet'];
  $toplamfiyatt = $toplamfiyat - ($toplamfiyat * ($urun['iskonto'] / 100)); // Is this discount code correct?
  $toplam_fiyat =  number_format($toplamfiyatt,2,",",".");

  echo "<tr><td>{$urun['marka']}</td><td>{$urun['urun_adi']}</td><td>{$urun['adet']}</td><td>{$fiyati} $secilen_paracinsi_id</td><td>{$urun['iskonto']} %</td><td>{$toplam_fiyat} $secilen_paracinsi_id</tr>";
}

Multiply unit price and number of products
price $fiyat
Number of product $urun[‘adet’]
$toplamfiyat = $fiyat * $urun[‘adet’];

discount $urun[‘iskonto’] = 30
$toplamfiyatt = $toplamfiyat - ($toplamfiyat * ($urun[‘iskonto’] / 100)); // Is this discount code correct?

Are they working but the code is correct?

Also, I would like to collect all discounted prices, How is it done?

Test result
Merkezi%20Uydu%20Sistemi%20Fiyat%20%C3%87%C4%B1karma%20Web%20Sitesi2

Discount and total transaction complete
They all work perfectly
Thank you very much for your help

Sponsor our Newsletter | Privacy Policy | Terms of Service