Inserting multiple selected dropdown values into a single column - mysql

Iam having a form which contaings of name,id etc..in this form iam getting dropdown from the same table only some ids here iam selecting multiple values and that should be inserted into db but only one value is inserting into the table remaining is not inserting can any one help me regarding this.
Model:
$data=array(
'first_name'=>$this->input->post('first_name'),
'exam_id'=>$this->input->post('exam_id'),
);
$this->db->insert('table',$data);
This is my dropdown
$this->table = 'table';
$exam = $this->dropdown('exam_id','examr_id');
return $exam;
This is my view:
$exam['']='--Select --';
$exam_id="id='exam_id' ";
if($this->input->post('exam_id')) $selected=$this->input->post('exam_id');else $selected='';
echo form_multiselect('exam_id',$exam,$selected,$exam_id);?>
any one help me it will be more helpfull for me

When you select multiple values and check that selected value pass in your url using GET or POST method. And than split your requested data using implode function. and save into database.

Related

How to edit and save multiple records in database from spring boot

My problem is simple and straight forward. I want to edit multiple records and save them in database. For editing a single record, I have used following statement in JpaRepository
DatabaseEntity findByAbcId(Integer abcId);
Here, I am trying to fetch a record from Mysql database table DatabaseEntity with respect to its column named abcId which is a foreign key of another table named ABC.
In my service class, I get this record, set its attributes and simply save it back in database like:
//Getting an existing record from database:
//Giving hardcoded value just for understanding
DatabaseEntity databaseEntity = databaseEntityRepository.findByAbcId(106);
//Setting edited fields into Model object. Except for its own id and abcId(foreign key)
databaseEntity.setCol1(value);
databaseEntity.setCol2(value);
databaseEntity.setCol3(value);
databaseEntityRepository.save(databaseEntity);
The above code will get a record and save its edited version into the database.
Now lets take a similar scenario but this time, the database is retrieving multiple records. from the database. Suppose multiple records are present against abcId column in my table. The changes in my code will be:
//Storing the result in the list as there are multiple records stored against 106
List<DatabaseEntity> databaseEntity = databaseEntityRepository.findByAbcId(106);
//What should I code here?
Now I am confused how to set values in multiple fields in a single go from spring boot. The values that I need to edit are also in another list and I tried iterating over both lists and change the database records accordingly but my approach is not a good one
//List of those records which I have edited
if(newRecordsList != null) {
//Using atomic Integer because only that can be changed within lambda expression
AtomicInteger outerLoopCounter = new AtomicInteger(0);
List<DatabaseEntity> databaseEntity = databaseEntityRepository.findByAbcId(Abc.getId());
databaseEntity.forEach(obj -> {
AtomicInteger innerLoopCounter = new AtomicInteger(0);
newRecordsList.forEach(newRecordsListObj -> {
//condition so that first record is updated according to the updated first record and other changes are updated accordingly.
if(outerLoopCounter.get() == innerLoopCounter.get()) {
obj.setName(newRecordsListObj.getName());
obj.setCondition(newRecordsListObj.getCondition());
obj.setValue(newRecordsListObj.getValue());
databaseEntityRepository.save(obj);
}
innerLoopCounter.incrementAndGet();
});
outerLoopCounter.incrementAndGet();
});
}
I know this is a very bad approach and I want to update my logic. So please help me to update these multiple records inside database.
Thanks in advance

Laravel: Storing an array of 3 strings inside one column of a table

I am wondering what the best way to accomplish this would be. I need to store an array such as:
['hello', 'world', 'love']
Inside one column in a table. I do not want to create a new table where each value in the array becomes a separate row. Is there a better way? I guess my biggest concern is performance.
So when creating the migration for this column, ideally I would insert an array with 3 default/nullable values. Then I can set actual values for the array when needed in my application.
Simple scenario: I have a table called desk and I want to store 3 items that are on top of the desk such as ['pencil', 'ruler', 'stapler'], but I do not want to create a separate table desk_items to have a row for each item. Hope this makes sense.
Using Laravel and MYSQL
Thank you.
make desc table as jsonb format
$table->jsonb('desk');
and in model use casts
protected $casts = [
'desk' => 'array',
];
If you want to store the array in Database table you should send it with json_encode .
$data = new Data;
$data->Name = json_encode($request->DataName);
$data->save();
Thus, you can store an array in the name column of your data table.
to use this data you need to call the data with json_decode.
$data = json_decode(Data::find(1)->Name);

Check whether any values in array match any values in json column

I have a problem of checking whether any values in an array match any values in json column which contains an array with a name.
suppose i have an array [25,36,45,52] and json column is {"values": [25,24,15]}.
I want to check whether any values in array match any of values in json column in xampp mysql. please provide a better solution of doing this. this image show table structure of my database
i have 4 tables.
user
profile
profile
jobs
user table (id,userid)
jobs table (id,user_id,skill_id)
skill table (id,job_id,)
profile table (id,user_id)
now i want to search all jobs that match some or at least one skills.
i have tried with this but this is giving all jobs with out skills filtered.
$jobs = Job::with(['user','profile'])->with(['skills' => function($query){
$query->whereJsonContains('skills->skills',[35]);
}])->where('jobs.is_completed',0);
please help me.
you can use where Clause easily for example you would like to get rows that match skills 35,54:
$users = DB::table('table')
-> whereJsonContains('skills->skills', [35,54])
->get();
for more details about how to querying json column check official docs :
https://laravel.com/docs/5.8/queries#json-where-clauses

Joomla 3x - Profile Fields

I have added a checkbox to the user profile and need to know how to get a list of all the user email addresses where they have checked the checkbox. I would like to just run a SQL query, but can't see where the value is stored.
All the profile data is stored in #__user_profiles table. To retrieve the data from the profile table you can perform this query
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select all records from the user profile table where key begins with "checkbox.".
// Order it by the ordering field.
$query->select($db->quoteName(array('user_id', 'profile_key', 'profile_value')));
$query->from($db->quoteName('#__user_profiles'));
$query->where($db->quoteName('profile_key') . ' LIKE '. $db->quote('\'checkbox.%\''));
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
You will get object list of all those users who clicked on your checkbox. Remember to use the profile_key name in place of checkbox
Ok - I have now managed to answer my own question (I took an export of the database as a SQL file, ticked the checkbox as a test user, made another export and then compared the files to see what had changed).
The values are stored in the jos_fields_values table. The query below assumes that there is only one checkbox - if you have more than one you will need to query the field_id as well, Replace xxx with the prefix of your Joomla installation.
SELECT email FROM xxx_users INNER JOIN xxx_fields_values ON xxx_users.ID=xxx_fields_values.item_id

CakePHP Invoicing App & Deleting Data From Another Controller/Model

I'm new to CakePHP and I could do with some newbie advice please.
I'm creating an invoicing application and the way I have it set up is with seperate tables (and so Models and Controlllers) for Invoices and Items. The Invoice table holds the data such as 'Invoice Number', 'Date' and 'Client ID' for example, and the Items table stores individual records/items that make up each invoice with 'Quantity', 'Unit Price' etc. The two are associated correctly.
When I call the 'edit' view for Invoices, obviously passing an ID, I retrieve the record and then also loop through the all associated records returned from the Items table to - all works as planned and are retrieved and displayed correctly.
What I want to be able to do from this view though, is to be able to delete individual items from the invoice by clicking on a delete button next to each - ideally with a confirmation box. How would this be acheived exactly? What would be the best method? Would I use a function in the Items controller to do this without leaving the page? How would I call the function in that controller from the Invoice controller/view? Or would another method such as the CakePHP query() function be used for this?
I hope I've explained this setup clearly enough for someone to offer any help and/or advice for which I would be very thankful.
Thanks in advance
The simple way:
In your Invoice/view.ctp
Inside your foreach loop you would do something like this:
//this is loose code since you have not pasted any in your question
<?php echo $this->Html->link('Delete', array('controller'=>'Item', 'action'=>'delete', $item['Item']['id'])); ?>
then create the function in your ItemController.php
function delete($id=NULL){
if($id){
$this->Item->delete($id);
$this->Session->setFlash(__('Invoice item deleted', true));
$this->redirect($this->referer());
}
}