how to print value in controller in codeigniter - codeigniter

am using code as below but am not getting any value.i gave as print_r($this->title); what i have to do to get the value. thanks
if(isset($_POST['is_ajax']) && $_POST['is_ajax']) {
print_r($this->title);
$respondentArray = array(
'state' => $_POST['state'],
'name' => $_POST['name'],
'title' => $_POST['title'],
'dline' => $_POST['directline'],
'email' => $_POST['email'],
'organization' => $_POST['organization'],
'address' => $_POST['address'],
'city' => $_POST['city'],
'state1' => $_POST['state1'],
'zip' => $_POST['zip'],
'generalphone' => $_POST['generalphone'],
'fax' => $_POST['fax'],
);
$this->session->set_userdata($respondentArray);

i guess this is what you need
echo $this->input->post('title') ;
$this->input->post(postedValues) this gets all the input values that is posted...

Use var_dump($this->title); to test

Related

I cant get lastInsertId on laravel 7 project

$sql = DB::table('laravel_products')
->insert(array(
'name' => $name,
'price' => $price,
'qty' => $qty,
'description' => $description,
'uruu' => $uruu,
'garage' => $garage,
'duureg' => $duureg,
'tagt' => $tagt,
'talbai' => $talbai,
'haalga' => $haalga,
'tsonh' => $tsonh,
'shal' => $shal,
'tsonhtoo' => $ttsonh,
'hdawhar' => $bdawhar,
'lizing' => $lizing,
'utas' => $utas,
'email' => $email,
'hereg' => $hereg,
'bairshil' => $bairshil,
'bairlal' => $bairlal,
'ashig' => $ashigon,
'zahi' => $zahi,
'image' => $data
));
$lastInsertedID = $sql->lastInsertId();
When I try to insert its responses:
"Call to a member function lastInsertId() on bool"
I used insertGetId but its cant save multiple rows of pictures on mysql.
If you want to get the last inserted ID like that you can call that method on the PDO instance directly:
$id = DB::getPdo()->lastInsertId();
If the table has an auto-incrementing id, use the insertGetId method to insert a record and then retrieve the ID:
$id = DB::table('users')->insertGetId(
['email' => 'john#example.com', 'votes' => 0]
);
from : https://laravel.com/docs/5.8/queries#inserts
$data = new LaravelProducts(); //LaravelProducts is your Model Name
$data->name= $name; //here 'name' is your column name
$data->price= $price; //here 'price' is your column name
$data->qty= $qty; //here 'qty' is your column name
$data->description= $description; //here 'description' is your column name
..........
..........
$data->image= $image; //here 'image' is your column name
$data->save();
$lastInsertedId = $data->id;
You don't have to write a new query to collect last inserted id from database.
$laravel_product = DB::table('laravel_products')
->insertGetId( array(
'name' => $name,
'price' => $price,
'qty' => $qty,
'description' => $description,
'uruu' => $uruu,
'garage' => $garage,
'duureg' => $duureg,
'tagt' => $tagt,
'talbai' => $talbai,
'haalga' => $haalga,
'tsonh' => $tsonh,
'shal' => $shal,
'tsonhtoo' => $ttsonh,
'hdawhar' => $bdawhar,
'lizing' => $lizing,
'utas' => $utas,
'email' => $email,
'hereg' => $hereg,
'bairshil' => $bairshil,
'bairlal' => $bairlal,
'ashig' => $ashigon,
'zahi' => $zahi,
'image' => $data
)
);
foreach ($filenamesToSave as $filename) {
DB::insert('INSERT INTO laravel_products_images ( product_id, filename ) VALUES ( ?, ? )',[$laravel_product->id, $filename]);
return view('createproduct');
} // Foreach Closing
// Echo your inserted ID Like Below
echo $laravel_product->id;
It should be 100% working for you.

Laravel Carbon Maatwebsite Format Date

I am trying to format date with Carbon before importing my data due to the following reason
when importing dates they are changed to a value resembling this 101010(example) even though the excel value looks like 01/01/1001
Here is a discussion on it however I have had no luck with there suggested ideas and am wondering if there are any other recommenadations or if I am missing something?
here is my import ClientsImport.php
public function model(array $row)
{
return new Client([
'category' => $row[0],
'referral_type' => $row[1],
'first_name' => $row[2],
'middle_initial' => $row[3],
'last_name' => $row[4],
'occupation' => $row[5],
'dob' => \Carbon\Carbon::createFromFormat('m/d/Y', $row['6']),
'email' => $row[7],
'cell_phone' => $row[8],
'work_phone' => $row[9],
'has_spouse' => $row[10],
'spouse_first_name' => $row[11],
'spouse_middle_initial' => $row[12],
'spouse_last_name' => $row[13],
'spouse_occupation' => $row[14],
'spouse_dob' => \Carbon\Carbon::createFromFormat('m/d/Y', $row['15']),
'spouse_email' => $row[16],
'spouse_cell_phone' => $row[17],
'spouse_work_phone' => $row[18],
'street_address' => $row[19],
'city' => $row[20],
'state' => $row[21],
'postal_code' => $row[22],
]);
}
I have tried \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['6'])
and I get this error A non well formed numeric value encountered
And also tried \Carbon\Carbon::createFromFormat('m/d/Y', $row['6'])
and I get this error The separation symbol could not be found ↵The separation symbol could not be found

Importing excel has an error of "Illegal string offset" in my controller

I'm trying to import an excel to my database table 'users' but it has an error saying Illegal string offset "email". I tried deleting the "email" then it says that Illegal string offset "username" now. Is it really the error in controller? Or maybe the reason is that i also have a repository.
This is my code for the controller
public function userImport()
{
if( Input::file('file_import') ) {
$path = Input::file('file_import')->getRealPath();
$inserts = [];
Excel::load($path,function($reader) use (&$inserts)
{
foreach ($reader->toArray() as $rows){
foreach($rows as $row){
$inserts[] = ['email' => $row['email'], 'username' => $row
['username'], 'password' => $row['password'], 'first_name' => $row['first_name'],'middle_name' => $row['middle_name'], 'last_name' => $row['last_name'], 'gender' => $row['gender'],
'civil_status' => $row['civil_status'], 'spouse' => $row['spouse'], 'religion' => $row['religion'],'emergency_no' => $row['emergency_no'],'previous_work' => $row['previous_work'],
'remarks' => $row['remarks'],'course' => $row['course'],'biometrics' => $row['biometrics'],'immediate_head' => $row['immediate_head'],'designation' => $row['designation'],'level' => $row['level'],
'emp_status' => $row['emp_status'],'dependents' => $row['dependents'],'date_hired' => $row['date_hired'],'regularization_date' => $row['regularization_date'],'remmitance_date' => $row['remmitance_date'],
'tin' => $row['tin'],'philhealth' => $row['philhealth'],'pagibig' => $row['pagibig'],'sss' => $row['sss'],'umid' => $row['umid'],'phone' => $row['phone'],'avatar' => $row['avatar'],
'address' => $row['address'],'country_id' => $row['country_id'],'role_id' => $row['role_id'],'birthday' => $row['birthday'],'status' => $row['status']];
}
}
});
}
if (!empty($inserts)) {
DB::table('users')->insert($inserts);
return back()->with('success','Inserted Record successfully');
}
return back();
}
As per your dumped $rows, it looks like that you don't need another foreach inside another foreach, modify your code.
// readability purpose
$rows = $reader->toArray();
foreach ($rows as $row){
$inserts[] = ['email' => $row['email'], 'username' => $row
['username'], 'password' => $row['password'], 'first_name' => $row['first_name'],'middle_name' => $row['middle_name'], 'last_name' => $row['last_name'], 'gender' => $row['gender'],
'civil_status' => $row['civil_status'], 'spouse' => $row['spouse'], 'religion' => $row['religion'],'emergency_no' => $row['emergency_no'],'previous_work' => $row['previous_work'],
'remarks' => $row['remarks'],'course' => $row['course'],'biometrics' => $row['biometrics'],'immediate_head' => $row['immediate_head'],'designation' => $row['designation'],'level' => $row['level'],
'emp_status' => $row['emp_status'],'dependents' => $row['dependents'],'date_hired' => $row['date_hired'],'regularization_date' => $row['regularization_date'],'remmitance_date' => $row['remmitance_date'],
'tin' => $row['tin'],'philhealth' => $row['philhealth'],'pagibig' => $row['pagibig'],'sss' => $row['sss'],'umid' => $row['umid'],'phone' => $row['phone'],'avatar' => $row['avatar'],
'address' => $row['address'],'country_id' => $row['country_id'],'role_id' => $row['role_id'],'birthday' => $row['birthday'],'status' => $row['status']];
}
$rows already represents each row, so you should probably rename it to $row.

How can i get rid of the max an min validation with having error in laravel 4.*?

I am having a problem with validation - the validation doesn't pass and it also doesn't show what type of error is making it
$rules = array(
'first_name' => 'required|min:3',
'last_name' => 'required|min:3',
'email' => 'required|email|unique:users',
'company_name' => 'required|min:3',
'zip_code' => 'required|integer|between:3,10',
'address_1' => 'required|min:3',
'address_2' => 'required|min:3',
'city' => 'required|min:3',
'country' => 'required|min:3',
'state' => 'required|min:3',
'phone_num' => 'required|integer|between:4,10',
'security_answer' => 'required|min:10',
'password' => 'required|min:3|confirmed',
'password_confirmation' => 'required'
);
This is how I am retrieving errors
{{ $errors->first('first_name') }}
{{ $errors->first('last_name') }}
and so on
if I remove |between:4,10 from the phone_num and zip_codeeverything works fine otherwise it doesn't show me any error but also doesn't pass the validation
Is there something wrong I am doing here? I tried finding the answer online but couldn't understand the problem here
You should change validation rules
'zip_code' => 'required|integer|between:3,10'
'phone_num' => 'required|integer|between:4,10'
to
'zip_code' => 'required|digits|between:3,10'
'phone_num' => 'required|digits|between:4,10'
Your validation rule now for zip code expects an integer between 3 to 10. I suppose you need numeric value with length from 3 to 10.

CakePHP: validation( ) fails

My validation fails, so my test for this message throws an ValidationException every time.
"Not all fields are filled out correct". Why? Analoguosly, it works for users (another method).
MODEL:
public $validate = array(
'name' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Name can not be empty',
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'This name is already used.',
),
),
'address' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Address can not be empty',
),
...
public function saveNewInstitution($institutionData, $userId) {
$institutionData[$this->alias]['is_active'] = 1;
$institutionData[$this->alias]['user_id'] = $userId;
$this->create();
$this->set($institutionData);
if ($this->validates()) {
return $this->save();
} else {
throw new ValidationException('Not all fields are filled out correct');
}
}
TESTclass:
public function testSaveNewInstitution() {
$data = array(
'Institution' => array(
'name' => 'Spitex Staufen',
'address' => 'Hauptweg 4',
'postcode' => '1234',
'city' => 'huuh',
'phone_number' => '123 456 78 90',
'email' => 'staufen#spitex.huuh',
'comment' => '',
'institution_type_id' => 5
));
$expected = array(
'Institution' => array(
'id' => 2,
'name' => 'Spitex Staufen',
'address' => 'Hauptweg 4',
'postcode' => '1234',
'city' => 'huuh',
'phone_number' => '123 456 78 90',
'email' => 'staufen#spitex.huuh',
'comment' => '',
'is_active' => TRUE,
'user_id' => 2,
'institution_type_id' => 5
));
$result = $this->Institution->saveNewInstitution($data, 2);
$this->assertEqual($result, $expected);
}
and all the Exceptionhandling methods like this:
public function testSaveNewInstitutionExceptionName() {
$this->setExpectedException('ValidationException');
$this->Institution->saveNewInstitution($this->__nameEmpty, 2);
}
I don't know if i got it really - is your Testclass failing or is the saveNewInstitution() function making you troubles? Did you try to check if the saveNewInstitution() is working correct by hand - put some data in a form an pass it to your function?
Maybe it's just a small thing and you have an entry with id=2 in your database an you got a duplicate key exception overwritten by your own exception method testSaveNewInstitutionExceptionName().

Resources