I have an int field in my cakephp project which translates into some values like here:
On my add
echo $form->input('bid_type', array('options' => array(
'1'=>'CPC',
'2'=>'CPM', )));
But when viewing this field i dont want the integers shown but the values.
How can i translate these values into strings when loaded?
if you don't want integers just change the keys
echo $form->input('bid_type', array('options' => array(
'CPC'=>'CPC',
'CPM'=>'CPM', )));
this will give you CPC when selecting CPC etc... also the "values" are the keys of the array so technically it was giving you the values. The value of the array is just the display of the option, you may change it to whatever you need
Related
We are working on a website based on SilverStripe and this site is connected with a SugarCRM database.
We have created a Form with a CheckboxSet with multiple values and store it a variable called $data['Interessen']
$set_entry_parameters = array(
"session" => $session_id,
"module_name" => "Contacts",
"name_value_list" => array(
array(
"name" => "interessen_c",
"value" => $data['Interessen']['fotografie']
),
array(
"name" => "interessen_c",
"value" => $data['Interessen']['dance']
)
)
);
Now the last array with "interessen_c" overwrites the previous values. We want to add more than one value at one time.
How is this possible?
If the contents of $data['Interessen'] can only have values from a fixed list of possibilities, I'd recommend making the field interessen_c into type multienum ("Multi-Selection Dropdown" field).
For that field create a list of all available items in Sugar (e.g. in Studio or creating the app_list_strings entry manually via code).
Sugar will then support multiple values in this field and display them nicely.
If your program writes the data by communicating with the Sugar REST API you can then just pass the $data['Interessen'] array as the value for interessen_c and Sugar will know what to do with it.
If your program writes the data directly to the interessen_c field in the database, then the field contents must adhere to the following format:
^value1^,^value2^,^value3^
So with ^ around each value and all items being separated by ,
Here an example of how to convert the array values to such a string in PHP:
$interessen = array();
foreach ($data['Interessen'] as $value) {
// add value surrounded by ^ to array
$interessen[] = "^$value^";
}
// transform values in array to string with items being separated by ,
$interessen = implode(',', $interessen);
Side-Note:
From within Sugar one can use encodeMultienumValue($arr) and unencodeMultienum($string) to convert from array to db-string format and back.
Both functions are defined in include/utils.php
Here I want to enter the value of the test result, where the data is integer based.
Plus, I would like to add up with other test results value data.
But why when I enter this integer-based data, its value is always rounded, or I get unsuitable results
in Controller Exam.php
public function Exam()
{
$data_exam = array(
'subjects' => 'Natural Sciences',
'name_of_pupil' => 'Rizky Remangsa',
'the_pure_value_of_the_exam' => 5.6,
'addition_with_another_value' => 11.2,
'student_address' => 'Indonesia/Jakarta Selatan',
'graduation_status' => 'Not graduated yet'
);
$this->exam_model->insertDataExam($data_exam);
}
The results of the data entered above are:
table the_pure_value_of_the_exam = 6
table addition_with_another_value = 11
You're storing your data in the wrong format so it's being converted for you.
5.6 is a float, not an integer. Integers are whole numbers. If you enter that value into an integer field it will round it to the nearest whole number.
You need to change the format you're storing the value in to something more appropriate:
https://dev.mysql.com/doc/refman/5.7/en/fixed-point-types.html
https://dev.mysql.com/doc/refman/5.7/en/floating-point-types.html
I'm making the assumption here that you're using MySQL but the same approach is going to apply regardless of the database type. Check the documentation to determine how best to store the data.
Ive come upon the following code and having trouble deciphering its use.
(changed up the variable names a bit for simplicity)
$fooo = array(
'dog' => array('pages', 'home'),
'cat' => array('users', 'login'),
'bird' => array('users', 'reset', 1),
);
I am familiar with associative arrays but have not seen this "nested array" implementation before.
Is this code creating an array of arrays?
For example, $fooo['dog'] returns an array where $dog[0]='pages' and $dog[1]='home'
That seems wrong.
Yes, this is an array of arrays. But it perhaps may be more accurate to describe it as an associative array with an indexed array for every value.
The following can be done with it:
$fooo['dog'] // gets array("pages", "home")
$fooo['bird'][0] // gets "users"
$fooo['cat'][1] // gets "login"
$fooo['cow'] = array('x', 'y'); // adds another value to the outer array
$fooo['bird'][] = 2; // $fooo['bird'] now equals array('users', 'reset', 1, 2)
There is nothing wrong with this code, but your example is lacking practicality. There is plenty of code that uses such structures though. For example, a logical representation of a menu with sub-menus on a website (which seems like the source of your sample), this data structure can then be looped to generate an HTML/CSS menu.
I have a function using array_search not working ... here is my code
function LangFull($name){
$languageCodes = array(
"abkhazian"=>"ab",
"afar"=>"aa",
"afrikaans"=>"af",
"afrikaans"=>"af-za",
"zulu"=>"zu",
"zulu"=>"zu-za"
);
return ucwords(array_search(strtolower($name),$languageCodes));
}
echo LangFull("zu"); /// Gives no output
echo LangFull("zu-za"); /// Gives output
same with af is no output ... please help
If its possible to interchange, (values to keys and keys to values) and won't have those key collisions, then you can do it that way also:
function LangFull($name){
$languageCodes = array(
"ab" => "abkhazian",
"aa" => "afar",
"af" => "afrikaans",
"af-za" => "afrikaans",
"zu" => "zulu",
"zu-za" => "zulu",
);
return isset($languageCodes[$name]) ? ucwords(strtolower($languageCodes[$name])) : 'Not found';
}
echo LangFull("zu"); /// Gives output
echo LangFull("zu-za"); /// Gives output
echo LangFull("yahoo!");
You have two identical array keys:
"zulu"=>"zu",
"zulu"=>"zu-za"
You need to name one of them something else.
As they are the same, trying to access one of them specifically is futile as PHP does not know which of the two you are requesting.
Alternatively, if you are trying to store more than 1 data value for a given key, you can make the value of a key an array, so can then store more data as required.
e.g.
array (
"afrikaans"=> array(
"af",
"af-za",
),
"zulu"=> array(
"zu",
"zu-za",
)
);
EDIT.
In response to you asking about swapping the keys and values:
You can, and Ghost has shown you how.
However retaining your keys as they are (as my above array example) allows you to collate all relevant data into one index, and can access it easily.
Swapping values and keys will likely make it harder to obtain data you need, as your key is now the "data". So to grab data from an array you'd need to know the data (as it's now the key) and you'd actually be grabbing the reference (what was your key).
Which is a bit odd. It can work, but it's not really ideal.
HI GEEKS
I am working in PHP and Mysql.
I have an array from database, in which I get the values if field name of a form i.e., Name, Company Name, etc. There are lots of field in the array.
I want these field to be displayed in my desired sequence, likewise:
Name:
Company Name:
Date of Birth:
Address:
These values are available in array in unordered format likewise:
array(
Company Name,
Address,
Date Of Birth,
Name
)
Now my question is how can manipulate this array to get the desired sequence in display of fields as shown above in PHP?
I think the better way is you should print them as you need instead of doing sorting on array based on custom desire something like:-
echo $array['Name'];
echo $array['Company Name'];
and so on
OR
if the fields are dynamic and edited by admin then you have to add one more field in your mysql database and store the order number of all the fields by which value to be printed on page.
Thanks
You are using associative array. You can output each key,value in any order you like.
If you want each ordering to be configurable, then simply store the key names in a list in order of appearance. Then display values according to this list of keys.
For eg : array name is user_info in which you have
array(
Company Name,
Address,
Date Of Birth,
Name
)
$user_info[0] = $user_info[3];
$user_info[1] = $user_info[0];
$user_info[2] = $user_info[2];
$user_info[3] = $user_info[1];
Unordered arrays do not exist in PHP. If you don't give them an explicit index, the elements will be indexed numerically starting from 0.
Here is an array:
$pizzas = array(
'boston',
'pepperoni',
'cheese'
);
To get the third value, use
$pizzas[2]
Let's say I want to echo the first value, then the third, then the second:
echo $pizzas[0].$pizzas[2].$pizzas[1];
EDIT: to reorder your array in the way you want, here is what you can do (let's take you would like it ordered as in the previous example:
$pizza_nicely_ordered = array (
$pizzas[0],
$pizzas[2],
$pizzas[1]
);