SugarCRM Field Calculation example of ifElse - php

I am new to SugarCRM. I have a requirement to calculate a field value through studio. But, in one of the fields which comes in denominator can be 0. So, i want to modify the formula through ifElse, how to do that.
Example - If (field1>0){calcField/field1} else(calcField=0).

you can use calculated field , if else like this:
ifElse(equal($status,"Held"),1,0)
For more information check this link :
Calculated field in sugarcrm

Related

How to generate unique auto increment value for each user in laravel and add it to database

I am developing a event organization website. Here when the user registers for an event he will be given a unique random number(10 digit), which we use to generate a barcode and mail it to him. Now,
I want to make the number unique for each registered event.
And also auto increment
One solution is to grab all the auto increment numbers in an array and generate a auto increment number using laravel takes the form (0000000001 to 9999999999) and loop through and check all the values. Grab the first value that doesn't equal to any of the values in the array and add it to the database.
But I am thinking that there might be a better solution to this. Any suggestion?
Select Maximum number stored in your DB and add 1 in it like:
SELECT (MAX(Column_Name)+1) AS Max_val FROM Table_Name;
I suggest simple timestamp-based solution using the Carbon class to produce a unique number using timestamp. It's fairly simple to have a basic unique and random stamp generation using timestamp.
You can use as given below,
use Carbon\Carbon;
$current_timestamp = Carbon::now()->timestamp; // Produces something like this 1552296328
You can use it as a unique identifier. If you want the next numbers, just +1. But keep in mind, you have to manage another number batch in a timely manner. (i.e if you have generated 500 numbers for now by increment, You should not generate another number for the next 500 seconds. Otherwise, it will repeat the number). If you want to know more, you can read here.
The solution with rand() function may not work here because it can re-produce the existing number in the database and you will be errored for Unique Constraint Violation(i.e. If you have column unique in DB).
No matter what approach you use, it would never be truly random. It will be PRNG. For your case, I think auto increment with zero fill should be enough.
But if you are set on using random number then using rand() function of PHP should be enough. 10 digit means 10000000000 unique number.Unless your project has millions of events it should realistically be no problem. So, approach 1 should be no problem. Also, you can check after generating any random number that whether that number is already present(There is 0.000001% or something like that chance.). If it is present then try to generate a random number again.
But if your project gets very successful (I.E. millions of events) then problems similar to Y2K might creep up.
MySQL UUID would give you truly unique is: Store UUID v4 in MySQL
You don’t need to worry about auto incrementing.

Chronoforms field to insert dynamic data

I am needing to have a custom element that will look at a drop-down called month and conditionally insert the numeric month into the database. For example if the user selects October it would put the number 10 in the numeric_month field in the DB. The month drop-down is set to another field in the same database and unfortunately there is no way to combine them for our purposes. Any help would be appreciated. I am new to php coding.
Create a hidden field in your form for numeric_month
add a "Custom Code" action to the "On Submit" event before your "DB Save Action"
In the Custom Code action insert PHP code to set the numeric_month field to the appropriate value depending on the selected drop down value.
In ChronoForms v4 or v5, this will be something similar to this:
<?php
$form->data['numeric_month'] = date("n",strtotime($form->data['month']));
?>
This would set numeric_month to '7' for a month value of 'July'.
Use date("m",strtotime($form->data['month']) to set numeric_month to '07' instead.
For more information about the PHP code to convert a month name to a numberic string, see the answers to these questions:
convert month from name to number
PHP convert short month-name to month-number

Get highest custom field value from last x months

in Wordpress, I've got a category called "Reviews" where I assign a vote (in a custom field called 'rate') to every single post I create in this category.
Now I'm trying to get the review with the highest rate in the last X months.
The rate ranges from 0 to 10, with one decimal number (ex. 5.3, 7.4, 9.1).
How can I do this? Use WP_Query or direct DB query? Thanks in advance.
You can use something like this to get the highest rating:
SELECT max(cast(meta_value as unsigned)) FROM wp_postmeta WHERE meta_key='your_meta_name'
But post meta data does not hold the date/time value for you. You need to think a way to insert date value as well.

jQuery currency spinner - "normalization"

I want to use the jQuery spinner for a currency value (i.e. money), the value of which might be in dollars, euros, or anything supported by the spinner.
The data should then be saved into a mysql database, in one col I would save the currency, in another the value.
The problem is that I am not sure on how to do this. Is it better to save the data in the format provided by the spinner (therefore saving everything as varchar), or should I take the extra step of normalizing all the values in and reconverting according to the currency when out from the db?
Note that I would like to perform some calculations (simple algebra) on that data, but only with values of the same currency.
If you want to do some calculations with the inserted data, it's better to store it without , . / $.
And wherever you want to show the data, change it into currency format.

Basic PHP/MySQL math example

I have a PHP form that grabs user-entered data and then posts it to a MySQL database. I'd like to know, how I can take the mathematical difference between two fields and post it to a third field in the database?
For example, I'd like to subtract "travel_costs" from "show_1_price" and write the difference to the "total_cost" variable. What's the best way to do this? Thanks so much.
You can lately process a select query: SELECT show_1_price - travel_costs AS pricediff FROM my_table; and then grab value in php and again do an insert query...
Should be simple to do on the PHP side of things how about
query=sprintf("INSERT INTO table VALUES(%d, %d, %d)", travel_costs,
show_1_price, show_1_price - travel_cost);
Generally though it is bad form to store a value in a database that can be calculated from other values. The reason being that you may never ever access this value again yet you are using storage for it. CPU cycles are much more abundant today so calculate the value when need. This is not a golden rule though - there are times when it could be more efficient to store the calculated value - although this is not usually the case.

Categories