Codeigniter : get array value from view and send to contoller and model - postgresql

view:insert_view.php
for($i=0;$i<10;$i++){
<input type="text" name="val[]" />
}
Table:
create table val_ex(val character varying);
controller:insert_ctl.php
$data['val'] = $this->input->post('val')
but it doesn't work it
.And i don't know how to write model to insert data to table

I don't know postgresql I just say How to insert records in model
Controller :-
$data['val'] = $this->input->post('val');
foreach($this->input->post('val') as $value) {
$this->model_name->insert_ctl($value);
}
Model :-
public function insert_ctl($value) {
$Array = array ('val' => $value);
$this->db->insert("val_ex", $Array);
}

Related

Property does not exist on this collection instance while trying to iterate through foreach

I'm trying to display an edit form with current value from the database.
Here's the Controlller.
public function edit(PenerimaanHadiahSponsor $id)
{
$collection = PenerimaanHadiahSponsor::find($id);
$dataanak = Anak::find($id);
$datatp = TipeHadiahSponsor::find($id);
// dd($dataanak);
return view('sponsor.penerimaan.updatetrx', compact('collection', 'dataanak', 'datatp'));
}
For now, I try to iterate the $datanak collection, but it returns that property [id] does not exist on the collection.
Here's the blade view file
<div class="mb-3">
<label class="form-label" for="basic-form-gender">Pilih Anak</label>
<select class="form-select" id="basic-form-gender" aria-label="Default select example" name="id_anak">
<option selected="selected">Pilih Anak yg Dapat Hadiah</option>
#foreach ($dataanak as $dtanak)
<option value="{{ $dtanak -> id }}">{{ $dtanak -> fullname }}</option>
#endforeach
</select>
</div>
I expect that it could return this data because it has relation with my curent model table which named penerimaan_data_sponsors
https://cdn.discordapp.com/attachments/856336794068189208/1051057589376532580/image.png
When I try to dd($dataanak) with this method:
$dataanak = Anak::where('id', 1)->get();
It returns the collection using id = 1, but when I used the current data id which is 12, it's returns blank array. Because the current data I want to edit had id 12.
https://cdn.discordapp.com/attachments/856336794068189208/1051058294464188456/image.png
the fix was this. I removed the model 'PenerimaanHadiahSponsor' from edit function.
public function edit($id)
{
$collection = PenerimaanHadiahSponsor::find($id);
$dataanak = Anak::find($id);
$datatp = TipeHadiahSponsor::find($id);
dd($dataanak->fullname);
// return view('sponsor.penerimaan.updatetrx', compact('collection', 'dataanak', 'datatp'));
}

Is there a way to use create() method on laravel eloquent model where data will call there corresponding set{field}Attribute method?

I'm trying to use create() method to create an object say 'user' from laravel-excel Import class say 'UserImport'. In the collection method, I grabbed the first row as the properties of the user and every cell in the subsequent row bears the data for the user on each row.
Using the create method will ensure that field that is not in my fillable will not be inserted as fields are dynamically gotten. I need the create method to use setGenderAttribute defined on the User model so as to transform 'male', 'm' in the excel gender column to constant User::GENDER_MALE and 'female', 'f' to constant User::GENDER_FEMALE.
public function collection(Collection $rows)
{
$keys = [];
// Extract spreadsheet head
foreach ($rows[0] as $key) {
$keys[] = Str::snake($key);
}
$j = 0;
foreach ($rows as $row) {
// Offset the head from the data set
if ($j == 0) {
$j++;
continue;
}
// get data from each row
$data = [];
$i = 0;
foreach ($keys as $key) {
$data[$key] = $row[$i];
$i++;
}
// Create user from each row
$user = User::create($data);
event(new MemberAdded($user));
}
}
It throws the following error integer value: 'male' for column users.gender
I've gotten to the answer. Laravel create method on eloquent actually call all the set{field}Attribute before actually doing database insertion. The problem was in one of the 'set' method. There was a bug in it and laravel fall back to the original field which was a string whose column was defined as integer in the database table schema.

Foreach loop problem in model using Codeigniter

Model:
public function fetch_emp()
{
$this->db->select('emp_id');
$this->db->from('emp');
$res=$this->db->get();
foreach($res->result() as $row)
{
$id=$row->emp_id;
$this->db->select('emp_name');
$this->db->from('employee_master');
$this->db->where('emp_id',$id);
$res=$this->db->get();
return $res->result();
}
}
Controller:
public function employee()
{
$result=$this->emp_model->fetch_emp();
if($result!=false)
$res['cmp_name']=$result;
else
$res['cmp_name']='NA';
}
$this->loadViews("emp/emp_views", $this->global, $res , NULL);
}
View:
<?php print_r($cmp_name);?>
Once I fetch the id from db I got all the id not problem at all, but when I used in foreach loop to search the id and get their names from another db It display only the 1st id name... For eg:If there are 5 id, but when I search the id to get the name from another db it shows the 1st id name only.What's the problem I don't know pls help me.
You could get it done (getting names list) by directly using join() method like this :
public function fetch_emp()
{
$this->db->select('emp_name');
$this->db->from('emp');
$this->db->join('employee_master', 'employee_master.emp_id = emp.emp_id');
$res=$this->db->get();
return $res->result();
}

Retrieve specific column from database and display on screen using CodeIgniter

I have a database table called input_types with a various input types:
[id] [input_type_Name]
1 text
2 radio
3 checkbox
4 select
... ...
I want to retrieve the names from the table and put them in an array so that I can then use form_dropdown to show them to the user in a dropdown.
The problem with how I'm doing it now is that I create optiongroups.
How I do it now:
Model
function get_inputTypes() {
$this->db->select('input_type_name');
$query = $this->db->get('input_types');
if($query->num_rows() > 0) {
return $query->result_array();
}
else {
return false;
}
}
Controller
$results = $this->survey_model->get_inputTypes();
$data['inputTypes'] = $results;
View
<label for="inputType">Input type:</label>
<?php echo form_dropdown('inputType', $inputTypes); ?>
This however doesn't create the desired effect. My dropdown gets populated, but because I have a multidimensional array the dropdown has optgroups.
I just want to have my selected data in a simple array. Why is this so freaking hard in CodeIgniter and php in general.
C# is much easier :/
Solution
The solution is very simple, use a foreach to loop through the multidimensional array in the model:
foreach($query->result() as $input_type) {
$data[] = $input_type->input_type_name;
}
return $data;
What do you mean by "This however doesn't create the desired effect" ?
Make sure you pass the $data to your view:
$this->load->view('viewname', $data);
The solution is very simple, use a foreach to loop through the multidimensional array in the model:
foreach($query->result() as $input_type) {
$data[] = $input_type->input_type_name;
}
return $data;

ZF last inserted id

does this code gives me the last inserted id of the record, even on a heavy load page?
db = Zend_Db_Table::getDefaultAdapter();
$db->insert($this->_name, $fields_values);
$idAddress = $db->lastInsertId($this->_name);
Regards
Andrea
i am using this...
db = Zend_Db_Table::getDefaultAdapter();
$lastInsertId = $db->insert($this->_name, $fields_values);
The Zend_Db_Adapter_Abstract::lastInsertId() method is a proxy to PDO::lastInsertId(). According to the documentation:
PDO::lastInsertId — Returns the ID of the last inserted row or sequence value
Note:
This method may not return a meaningful or consistent result across different PDO drivers, because the underlying database may not even support the notion of auto-increment fields or sequences.
Now you know. Use it as your own risk!
public function saveData(array $data) {
$this->_setName('user');
// this will return all field name for user table in $field variable
$fields = $this->info($this->getConstCol());
// By this foreach loop we will set data in $data array
foreach ($data as $field => $value)
{
if (!in_array($field, $fields))
{
unset($data[$field]);
}
}
// It will call insert function of (Zend_Db_Table_Abstract Parent of Zend_Db_Table)
// It will return $pkData which is primary key for user table
return $this->insert($data); // It will return last insert ID
}