Sorry about the strange title, but I am having a small issue if the user type is 2 or 3 then I need the user not to be able to insert or update a few fields, but instead of creating a number of insert queries listed below are a few fields I need not updated.
AMStatus,HQStatus,approvedforsite
public function databaseinsert($data)
{
$this->data = $data;
switch($this->data['action']){
case "newlead":
$this->insert("customer_detail",
"TradingName,Street,City,State,PostCode,Industry,SubCategories,Membership,LeadStatus,AMStatus,HQStatus,approvedforsite,Salutation,FirstName,LastName,Website,Company,ABN_ACNNumber,Phone,Mobile,Notes,Description",
"'{$this->data['tradingname']}',
'{$this->data['street']}',
'{$this->data['suburb']}',
'{$this->data['state']}',
'{$this->data['postcode']}',
'{$this->data['category']}',
'{$this->data['subcategory']}',
'{$this->data['membership']}',
'{$this->data['salestatus']}',
'{$this->data['managerstatus']}',
'{$this->data['hqstatus']}',
'{$this->data['publishtoweb']}',
'{$this->data['title']}',
'{$this->data['firstname']}',
'{$this->data['lastname']}',
'{$this->data['webaddress']}',
'{$this->data['companyname']}',
'{$this->data['abnacn']}',
'{$this->data['phonenumber']}',
'{$this->data['mobile']}',
'{$this->data['notes']}',
'{$this->data['businessdescription']}'
");
break;
}
}
You need to do it in your script handle database fields and values by applying conditions. and make strings of both according to different user types before applying them in $this->insert function.
Something like below:
if($usertype=='yourtype')
{
$fieldsstr="TradingName,Street,City.................";
$valuestr=$this->data['tradingname'].",".$this->data['field2'].",".......
}
else
$fieldsstr="TradingName,Street.................";
$valuestr=$this->data['tradingname'].",".$this->data['field2'].",".......
}
$this->insert("customer_detail",$fieldsstr,$valuestr);
Related
Currently, I have controller & model for form edit. If user submit different value, this will modify the value in database and if there is an affected rows, will set flash message as "success" and if same data is submitted by user without modify the value,then will set flash message as "nochange".
if($this->db->affected_rows() > 0 ) {
$this->session->set_flashdata('success','Database is updated');
redirect('registrar');
} else {
$this->session->set_flashdata('nochange','There is no changes in database');
redirect('registrar');
}
In Codeigniter, how can I check if only specific columns are affected?
For example, if my table in database have 12 columns, if user only change the value let say in column 10, then set flash message as "nochange".
If user changes values in other columns except column 10, set flash message as "success"
There is no in-built functionality in CodeIgniter or php to do this.
You would have to essentially get the entire entry before updating it. And then column by column compare the submitted value with the current value of the entry as per your required logic.
Example pseudo-code:
$q = $this->db->get_where('table', array('id' => $id))->row();
$change = false;
if ($this->input->post('col1') !== $q->col1) {
$change = true;
}
...
if ($this->input->post('col10') == $q->col10) {
$change = false;
}
$this->db->update('table', $data);
Here is the project installation:
1. Nodejs server side environment
2. MySQL database (with Knexjs query builder)
3. 'locations' table in a database with fields like 'country', 'city', 'category', 'working_hours'.
4. Form input where user can select all these values and submit the search. When the user selects nothing, it defaults to 'ALL'. I need to filter the database and to select rows corresponding to the user search input.
I tried to do something like this:
knex('locations').where({
country: countryInput,
city: cityInput,
category: categoryInput,
working_hours: hoursInput
})
.then(function(rows) {.....})
.catch(function(err) {.....});
This works well if user selects all the input fields, what if the user selects only few of them and other are set to 'all'? How to make this work?
Thank you in advance!
This is pretty common solution:
let query = knex('locations');
if (countryInput) {
query = query.where('country', countryInput);
}
if (cityInput) {
query = query.where('city', cityInput);
}
if (categoryInput) {
query = query.where('category', categoryInput);
}
if (hoursInput) {
query = query.where('working_hours', hoursInput);
}
query
.then(function(rows) {.....})
.catch(function(err) {.....});
you can modify your query using Modify
expecting req.query object matching your table columns-
knex('locations')
.modify(function(queryBuilder) {
if (req.query) {
queryBuilder.where(req.query);
}
})
.then(function(rows) {.....})
.catch(function(err) {.....});
well, there are some potential risk. You need filter your query first.
This is one of my first applications out of tutorials so I don't know how to express my issue well.
Well I have these 2 tables:
User ( id, code )
Hours ( id, user_id, created)
I want to know how I can add an entry to the Hours table using the user_code.
I tried to grab the data of the User table with the code value and then findBy and pass for the patchEntity but it did not work.
I don't have a whole lot of information to work with, but I'll give it a go.
I want to know how I can add an entry to the Hours table using the
user_code
You mention using patchEntity, so that's updating information that's already there. Assuming user_code is the 'code' column you're talking about there, first find the user by his code:
$users_tbl = TableRegistry::get('Users');
// find the user
$user = $users_tbl->findByCode($user_code)->first();
if ($user) {
// replace '$this->request->data() with whatever patch data you wanted
$users_tbl->patchEntity($user, $this->request->data(), [
'associated' => ['Hours']
]
if ($users_tbl->save($user)) {
// success!
} else {
// error!
}
} else {
// error!
}
It will also depend on how you have the data you passed in (where my '$this->request->data() is, or whatever your array might be) - it needs to match the right column names and be in the correct format listed here.
However, this is updating the data. Just adding the data, you can load the hours table and add a new entry with the user_id acquired from the user search:
$hours_tbl = TableRegistry::get('Hours');
$hours = $hours_tbl->newEntity([
'user_id' => $user->id // $user populated from same method earlier
]);
/* assumed 'id' was autoincrementing and 'created' was populated
through Timestamp behavior */
if ($hours_tbl->save($hours)) {
// yay!
} else {
// boo
}
Is there a way to create/update form poll fields based on a content type? For example, I've got a post type called Candidate and I'd like like to dynamically update a poll list when a new Candidate is added, or when one is removed.
The reason I'm looking for this is because I'm creating a voting mechanism for this client and they have requested that users see an image, the name, and brief Bio of who they are voting for. My idea is to tie in the names so that I can target a hidden Gravity Form poll on page so when the voter clicks, it updates the corresponding named checkbox.
I can of course add each Candidate one by one, and then add each candidate one by one in the form, but was hoping there was a way to do that in code. So far I haven't found much other than filters in the Gravity Forms Documentation.
To reiterate, my question here is not the frontend connection, rather how to dynamically update/add an option in a poll field in a form when content is created for a Candidate post type.
Any help would be appreciated.
I believe the solution you're after is documented here:
http://www.gravityhelp.com/documentation/gravity-forms/extending-gravity-forms/hooks/filters/gform_pre_render/
Probably a combination of solution examples #1 and #2 on that page will fit your needs? So -
Create a category of standard Wordpress pages (category name: "Candidates"), and the pages would contain the candidate information (bio, photo, etc).
In your functions.php file for your current theme, you'd add something like the following to pull out that category's worth of posts:
// modify form output for the public page
add_filter("gform_pre_render", "populate_checkbox");
// modify form in admin
add_filter("gform_admin_pre_render", "populate_checkbox");
function populate_dropdown($form) {
// some kind of logic / code to limit what form you're editing
if ($form["id"] != 1) { return $form; }
//Reading posts for "Business" category;
$posts = get_posts("category=" . get_cat_ID("Candidates"));
foreach ($form['fields'] as &$field) {
// assuming field #1, if this is a voting form that uses checkboxes
$target_field = 1;
if ($field['id'] != $target_field) { break; }
// init the counting var for how many checkboxes we'll be outputting
// there's some kind of error with checkboxes and multiples of 10?
$input_id = 1;
foreach ($posts as $post) {
//skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
if($input_id % 10 == 0) { $input_id++; }
// here's where you format your field inputs as you need
$choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
$inputs[] = array("label" => $post->post_title, "id" => "{$field_id}.{$input_id}");
// increment the number of checkboxes for ID handling
$input_id++;
}
$field['choices'] = $choices;
$field['inputs'] = $inputs;
}
// return the updated form
return $form;
}
I have asked this question before but i did not get the right answer. I have a table 'cv', i want to search for items in the database using two fields i.e. course and location, the two items are on the same table('cv'). I dont have an idea on how to implement the two searches.
The code below is for displaying search for course only and its working fine. Help me to use the two search criterions i.e.course and location or in other words how do i add the search for location input in my code.
Controller
function search_worker()
{
$data['query']=$this->kint_model->search_workers($this->input->post('search'));
$this->load->view('hire_display',$data);
}
Model
function search_workers($search)
{
return $query = $this->db->get_where('cv', array('course '=> $search))->result();
}
View
$data = array('name'=>'search', 'id'=>'search','value'=>'bcom');
echo form_input($data);
$data = array('name'=>'submit', 'id'=>'submit', 'value'=>'Search Item(s)');
echo form_submit($data);
Maybe you need a like clause in your query,
function search_workers($search){
//return $query = $this->db->get_where('cv', array('course '=> $search))->result();
return $this->db->like('course', $search)->like('location', $location)->get('cv')->result();
}
You should also get some basic SQL training.