Problems creating "select" to choose and show the chosen country - php

I was watching how to create a php code to show in a the countries chosen in the column enum type in the database, and in turn show the chosen country by default.
But it does not show the list of countries to select, it only shows the one I have selected and the rest is blank.
public static function getAllowFlags(){
$flaglist = [];
$flag_q = Db::query('SELECT flag FROM players');
while($l = $flag_q->fetch_assoc()) {
$flaglist[] = $l;
}
return $flaglist;
}
$AllowFlags = Account::getAllowFlags();
<?php foreach($AllowFlags as $key => $flag) { ?>
<option value="<?=$flag['flag'];?>" <?=$flag['flag']?'selected':'';?>><?=$flag['flag'];?></option>
<?php } ?>

Related

Gravityforms populate selected value on dropdown within theme functions.php

I am working in gravityforms to pre-populate a form with database values dynamically, which is working. I need to be able to specity which of these options is selected by default when the form is built, but I can't find the option to do so. I have seen the placeholder, but I presume this doesnt actualy select anything on the dropdown. I have been unable to find any docs or references that allow me to set the "selected" options once I have built all the text/value pairs in the array and set the choices.
The function (which I have redacted here) works fine and populates the field, I just need to populate a placeholder and/or default selected value.
add_filter('gform_pre_render_1', 'getFieldValues');
add_filter('gform_pre_validation_1', 'getFieldValues');
add_filter('gform_pre_submission_filter_1', 'getFieldValues');
function getFieldValues($form) {
global $wpdb;
foreach ($form['fields'] as $field) {
if ($field->id == '40') {
// get the list of values from the Database
$list1Q = "SELECT <data> FROM <table> WHERE <params> = <value>";
$list1R = $wpdb->get_results($list1Q);
// Generate a nice array that Gravity Forms can understand
if (!empty($list1R)) {
$list1A[] = array();
foreach ($list1A as $listItem) {
// Add current value to the array for dropdown choices
$list1C[] = array('text' => $listItem->variable, 'value' => $listItem->variable);
}
}
// Set choices to field
$field->choices = $List1C;
***** THIS IS WHERE I WOULD LIKE TO SET THE SELECTED VALUE *****
}
}
return $form;
}
If there is a better way to go about populating this field, I am open to suggestions at it seems that the form loads a bit slow using this method. Otherwise, I would love to know how to set a selected choice value after populating the choices.
the quick answer is, there is an option that i found for "isSelected" that can be included in the array when defining choices. I added a ternary operation to match on what I wanted selected and it set the "selected value for me.
$isSelected = ($listItem->variable == "value I want to select") ? true : false;
$list1C[] = array('text' => $listItem->variable, 'value' => $listItem->variable, 'isSelected' => $isSelected);
Adding the ternary choice and changing the array push allowed me to set a value as selected.

Getting data from table on input_post codeigniter

So I'm creating an application for clients to fill in an order to calculate the price. The client has to fill in a form, the form will be posted to a table in MySQL. When the order is complete the client gets an overview of his order with the calculated price. I'm having trouble with storing a variable (from tblPanel_Prices) which is related with a variable in tblPaneltypes. So after filling in the form data from column tblPaneltypes and tblPanel_Prices should be stored together in another table. Both tables (types and prices) are related.
The client has to select a type of panel. These types are stored in the table (tblPaneltypes). Every panel has a certain price-categorie (field price_id) which is needed to calculate the price. The prices of the paneltypes are stored in another table (tblPanel_Prices). There is a relation between the tblPaneltypes and tblPanel_Prices (both integer)
So the client fills in the form and selects a certain type of panel, this gets posted (controller) to the table with the function: this->input->post('paneltype').
After this I want to calculate the surface of the panel and make select the right price-category on the field price-id.
So in the form there is a type of panel choosen by the client. This already goes to my table, but I also want to pass the price_id tag in the form.
Then, I would like to get the price_id tag from the table tblPaneltypes to link this with the price_id tag in tblPanel_Prices. The value of the price_id isn't important for the client, so no need to select or show this value. I just need it to calculate the price of the panel (which depends on the type of panel filled in by the client)
Thanks in advance!
Store function in controller
public function store()
{
$this->load->model("orders/panels/Panels");
$this->form_validation->set_rules('amount', 'aantal', 'numeric|required|max_length[2]');
$this->form_validation->set_rules('dwidth', 'paneelbreedte', 'numeric|callback_check_width');
$this->form_validation->set_rules('dheight', 'paneelhoogte', 'numeric|callback_check_height');
$this->form_validation->set_rules('paneltype', 'paneeltype', 'required');
$this->form_validation->set_rules('ral_code', 'ral kleur', 'callback_check_double');
if ($this->auth()) {
if ($this->form_validation->run() == FALSE) {
$this->read_panel();
} else {
$panel = $this->Panels->create__entry(
$this->input->post('order_id'),
$this->input->post('amount'),
$this->input->post('dwidth'),
$this->input->post('dheight'),
$this->input->post('paneltype'),
$this->input->post('panelcategory'),
$this->input->post('color'),
$this->input->post('othercolor'),
$this->input->post('tubes'),
$this->input->post('colorswatch'),
$this->input->post('structurepaint'),
$this->input->post('uv'),
$this->input->post('window'),
$this->input->post('paintwindow'),
$this->input->post('windowsetup'),
$this->input->post('glastype'),
$this->input->post('door')
);
redirect("rail/read_rail?order=" . $this->input->post('order_id'));
}
} else {
login();
}
create__entry in model: at this moment I'm filling in the panelcategory manually in the model in. In the future it should filled in automaticly.
function create__entry($order_id, $amount, $dwidth,
$dheight, $paneltype, $panelcategory, $color,
$othercolor, $tubes,
$colorswatch, $structurepaint,
$uv, $windowtype, $paintwindow,
$windowsetup, $glastype, $door)
{
$this->order_id = $order_id;
$this->amount = $amount;
$this->dwidth = $dwidth;
$this->dheight = $dheight;
$this->paneltype = $paneltype;
$this->panelcategory = $panelcategory;
$this->color = $color;
$this->othercolor = $othercolor;
$this->tubes = $tubes;
$this->colorswatch = $colorswatch;
$this->structurepaint = $structurepaint;
$this->uv = $uv;
$this->windowtype = $windowtype;
$this->paintwindow = $paintwindow;
$this->windowsetup = $windowsetup;
$this->glastype = $glastype;
$this->door = $door;
$this->db->insert(self::TABLE_NAME, $this);
return $this->read__entry($this->db->insert_id());
}

Allow users to update their gravity form single unique entry

I am trying to make a form which will allow them to update their entered unique entry. Suppose there is form which is used to collect, the User Specializations
And this Specializations field is gravity forms multiselect field which is dynamically set. And as they update their specializations also updated in entries tab.
So, i just want to allow they to have this multiselect field updation.
I came across a solution to add multiselect choice dynamically but that only populate the field with all the choices not the user selected also.
So, is there is way to set the user entered choices dynamically?
Possible Solution:
Like we can get the entries and then set search criterion to current user and will get a unique entry and then after we can set these selected values in the choices.
Issue Exists with the above possible solution:
but still there is issue like it will be creating new entry as form dont know it is update operation. So can anyone help?
//Auto Populate Service Field in Form ID=
add_filter('gform_pre_render_2', 'populate_services');
add_filter('gform_pre_validation_2', 'populate_services');
add_filter('gform_pre_submission_filter_2', 'populate_services');
add_filter('gform_admin_pre_render_2', 'populate_services');
function populate_services($form)
{
foreach ($form['fields'] as &$field) {
if ($field->type != 'multiselect' || strpos($field->cssClass, 'populate_services') === false) {
continue;
}
$services = get_specializations();
$choices = array();
foreach ($services as $service) {
$title = stripslashes($service->name);
$choices[] = array(
'value' => $service->ID,
'text' => esc_html($title)
);
}
// update 'Select Duration' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Services';
$field->choices = $choices;
}
return $form;
}

How to display custom error messages instead of the default ones?

I'm creating a recommendation controller using PHP Yii framework, using a heuristic concept. My recommendation will only work if the user or other user with the same course have made a booking. But right now if my bookings is equal to null it is showing an error message like below:
Error 500 Invalid argument supplied for foreach()
Therefore I have made a condition where I created my own message:
We are sorry currently there are no recommendation of books at this moment
and it is working. Unfortunately, it also shows together with the error message. I just want to show only my own message. Below is my code:
//recommendation - use heuristic concept, more accurate result as more people same course recommendation
public function actionRecommendation() {
$user = User::model()->findByPk(Yii::app()->user->id);
//get course mates
$courseMates = User::model()->findAllByAttributes(array('course_id' => $user->course_id));
$popularity = array();
if($bookings==null){Yii::app()->user->setFlash('success', 'We are sorry currently there are no recommendation of books at this moment'); }
foreach ($courseMates as $courseMate) {
//identify all bookings made by user
$bookings = Booking::model()->findAllByAttributes(array('user_id' => $courseMate->id));
foreach ($bookings as $booking) {
//add hit into array as per book
$popularity[$booking->book_id] = (isset($popularity[$booking->book_id])) ? $popularity[$booking->book_id] + 1 : 1;
}
}
//sort according to popular count
arsort($popularity, SORT_NUMERIC);
$display = 10;
foreach ($popularity as $bookId => $popular) {
//if display is 0, break the foreach, only display up 10
if ($display == 0) {
break;
}
//create book object array
$books[] = Book::model()->findByPk($bookId);
//reduce book count
$display--;
}
$this->render('recommendation',array(
'books'=>$books,
));
}()
How can I modify it so it displays just the custom error message?
For Input Fields
*In your Model*
array('gender','required','message'=>'Please select gender.'),
Or can call Function too.
array('gender', 'ownFunction'),
public function ownFunction() {
{
if (empty($this->gender))
$this->addError("gender", 'Please select gender.');
}
}
Using this you can display custom error message on input fields
For Error Page
for Custom message in Error Page .
In your Controller
if(Your Condition)
{
//Show Error form.
throw new CHttpException(500,'We are sorry currently there are no recommendation of books at this moment. ');
}
else
{
/* rest of code goes Here */
}
Hope this Helps You

Add language to relationship dropdown in ExpressionEngine

I'm using ExpressionEngine for a multi-language website with products. I used Transcribe for controlling the multi-language. Products have the same title in different languages, and this gives me some problems selecting the correct product in a relationship field.
The builder of this site did not use the title for a unique name in the back-end only, but also displayed it everywhere in the front-end.
Dropdown example:
Product A
Product A
Product A
Product B
Product B
Product B
Product c
Product c
Product c
I found that the dropdown information is filled in /system/expressionengine/fieldtypes/rel/ft.rel.php from line 59
/**
* Display Relationship Field
*
* #access public
* #param string
* #return string
*/
function display_field($data)
{
if ($this->settings['field_related_orderby'] == 'date')
{
$this->settings['field_related_orderby'] = 'entry_date';
}
$this->EE->db->select('entry_id, title');
$this->EE->db->where('channel_id', $this->settings['field_related_id']);
$this->EE->db->order_by($this->settings['field_related_orderby'], $this->settings['field_related_sort']);
if ($this->settings['field_related_max'] > 0)
{
$this->EE->db->limit($this->settings['field_related_max']);
}
$relquery = $this->EE->db->get('channel_titles');
if ($relquery->num_rows() == 0)
{
return $this->EE->lang->line('no_related_entries');
}
else
{
if ( ! isset($_POST[$this->field_name]))
{
$this->EE->db->select('rel_child_id');
$relentry = $this->EE->db->get_where('relationships', array('rel_id' => $data));
if ($relentry->num_rows() == 1)
{
$data = $relentry->row('rel_child_id') ;
}
}
$field_options[''] = '--';
foreach ($relquery->result_array() as $relrow)
{
$field_options[$relrow['entry_id']] = $relrow['title'];
}
return form_dropdown($this->field_name, $field_options, $data, 'id="field_id_'.$this->field_id.'"');
}
}
How can I add the language name to the dropdown at line 98 ($field_options[$relrow['entry_id']] = $relrow['title'];)?
Transcribe did not found any possibilities in Expression Engine 2.5.
I created a workaround and added the url_title in the dropdown menu. It is not the best solution, but in this case it is workable.
$this->EE->db->select('entry_id, title');
changed into
$this->EE->db->select('entry_id, title, url_title');
And changed
$field_options[$relrow['entry_id']] = $relrow['title'];
into
$field_options[$relrow['entry_id']] = $relrow['title'] . " - " . $relrow['url_title'];

Categories