( ! ) Parse error: syntax error, unexpected '[', expecting ')' in codeigniter - codeigniter

public function login($email,$password)
{
$query = $this->db->where(['email'=>$email,'password'=>$password])
->get('registration');
if ($query->num_rows() > 0) {
return $query->row();
}
}
i am new in programming. There is syntax error . i couldn't understand the problem's solution

Try this
public function login($email, $password)
{
$array = array('email' => $email, 'password' => $password);
$this->db->where($array);
$query = $this->db->get('registration');
if ($query->num_rows() > 0) {
return $query->row();
// return true;
}
return false;
}
Read through the manual codeigniter database library has lots of features.
http://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-data
Associative array method:
$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->where($array)

Related

ErrorException Array to string conversion Laravel

Here is my code in the controller.
I want multiple data to insert into a database but I have a problem with Array:
public function postCreate(Request $request)
{
$data = $request->all();
$lastid = Tr_header::create($data)->id;
if (count($request->id_product) > 0)
{
foreach($request->id_product as $item => $value)
$datax = array(
'id_tr_header' => $lastid,
'id_product' => $request->id_product[$item],
'qty' => $request->qty[$item],
'date_kembali' => $request->date_kembali[$item],
'information' => $request->information[$item],
);
Tr_detail::insert($datax);
}
return redirect()->back();
If you want to insert multiple row at a time try this:
{
$data = $request->all();
$lastid = Tr_header::create($data)->id;
if (count($request->id_product) > 0)
{
$datax = [];
foreach($request->id_product as $item => $value)
array_push($datax ,[
'id_tr_header' => $lastid,
'id_product' => $request->id_product[$item],
'qty' => $request->qty[$item],
'date_kembali' => $request->date_kembali[$item],
'information' => $request->information[$item],
]);
Tr_detail::insert($datax);
}
return redirect()->back();
You are overwritting your $datax variable, you need to create an array of arrays to pass on to your insert() function:
public function postCreate(Request $request)
{
$data = $request->all();
$lastid = Tr_header::create($data)->id;
if (count($request->id_product) > 0) {
$datax = [];
foreach ($request->id_product as $item => $value)
$datax[] = array(
'id_tr_header' => $lastid,
'id_product' => $request->id_product[$item],
'qty' => $request->qty[$item],
'date_kembali' => $request->date_kembali[$item],
'information' => $request->information[$item],
);
Tr_detail::insert($datax);
}
return redirect()->back();
}

Print custom message if Laravel phpunit test fail

I'm working with Laravel and my code is like that
$this->actingAs($user)
->json('post', '/graphql/admin', ['query' => $query])
->assertStatus(200)
->assertJsonStructure($expected);
I would like to print the variable $query if this test fails.
My ideal is add in the end of code some like:
if ($this->isFail()) {
echo $query;
}
In the file TestCase.php add this function
// tests/TestCase.php
public function assertJsonStructure(
string $url,
array $params,
array $expected
) {
$response = $this->json('post', $url, $params);
try {
$response->assertStatus(200)->assertJsonStructure($expected);
} catch (\Exception $ex) {
$this->printDie($params, $expected, $ex, $response);
}
}
And this function:
// tests/TestCase.php
private function printDie($params, $expected, \Exception $ex, $response, $user_id = null)
{
$content = substr($response->getContent(), 0, 1500);
$trace = debug_backtrace();
$error = [
'class' => static::class.'::'.$trace[2]['function'],
'params' => $params,
'expected' => $expected,
'user' => $user_id,
'error' => $ex->toString(),
'content' => $content,
];
dd($error);
}
Now, you can test your query:
$this->assertJsonStructure(
'/graphql',
[
'query' => $query,
],
$expected
);
And, if it fail, it will print the error info, and it will easier to debug. You can see a preview here

How to use the same insert_id() in CodeIgniter from one function to another function [duplicate]

I have a problem with insert_id. I'm using CodeIgniter. I have a method in which I insert some values and in it I return
$insert_id=$this->db->insert_id();
return $insert_id;`
I want to use this value in another method in the same model. I tried that: $insert_id=$this->add_teacher_subject(); but in this way first method runs again and I doesn't receive last_insert_id , but this value +1. Please, tell me how could I solve this problem?
My model is:
public function add_teacher_subject() {
$date = new DateTime("now");
$data=array(
'teacher_id'=>$this->input->post('teacher'),
'user_id'=>$this->session->userdata['user_id'],
'created_at'=>$date->format('Y-m-d H:i:s')
);
$this->db->insert('student_surveys',$data);
$insert_id=$this->db->insert_id();
return $insert_id;
}
public function survey_fill()
{
$insert_id=$this->add_teacher_subject();
if ($this->input->post('answer')) {
$answers= $this->input->post('answer');
if (null !==($this->input->post('submit'))) {
$date = new DateTime("now");
foreach($answers as $question_id=>$answer)
{
$data = array(
'user_id'=>$this->session->userdata['user_id'],
'question_id'=>$question_id,
'answer'=>$answer,
'student_survey_id' => $insert_id
);
$this->db->insert('survey_answers', $data);
}
}
You can use SESSION[] for keeping last insert id:
public function add_teacher_subject() {
$date = new DateTime("now");
$data = array(
'teacher_id' => $this->input->post('teacher'),
'user_id' => $this->session->userdata['user_id'],
'created_at' => $date->format('Y-m-d H:i:s')
);
$this->db->insert('student_surveys', $data);
$insert_id = $this->db->insert_id();
$newdata = array('insert_id' => $insert_id);
$this->session->set_userdata($newdata);
return $insert_id;
}
public function survey_fill() {
$insert_id = $this->session->userdata('insert_id');
if ($this->input->post('answer')) {
$answers = $this->input->post('answer');
if (null !== ($this->input->post('submit'))) {
$date = new DateTime("now");
foreach ($answers as $question_id => $answer) {
$data = array(
'user_id' => $this->session->userdata['user_id'],
'question_id' => $question_id,
'answer' => $answer,
'student_survey_id' => $insert_id
);
$this->db->insert('survey_answers', $data);
}
}
}
}

Check number of affected rows in CodeIgniter

I am trying to check in my model method if there was affected any rows if so return true else return false, but I don't know how to get it?:
class User_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function save_new_user ($name, $email, $password)
{
$data = array(
'name' => $name,
'email' => $email,
'password' => $password,
'time' => time()
);
$this->db->insert('users', $data);
// if (affected rows > 0) return true else return false
if ([AFFECTED_ROWS] > 0) return true;
else return false;
}
}
The method you are looking for is $this->db->affected_rows().
So your return statement could look like this:
return $this->db->affected_rows() > 0;

How to set the logged in user ID on a session (CodeIgniter)?

I am having trouble, getting the logged in user ID from the database...
This is my controller code :
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if ($query){
$this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
This is my model function where I return the user id from the database:
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users');
foreach ($query->result() as $row)
{
$user_id = $row->id;
}
return $user_id;
}
Everything works perfect, except that $user_id returns empty... What am I doing wrong?
Try:
$user_id= $this->users_model->get_userID($this->input->post('email'));
try modifying the model function to this -
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users')->row_array(); // retrieves only first-row.
return $query['id'];
}
In controller the function needs just a bit of modification as follows -
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if($query){
//$user_id was not assigned before :)
$user_id = $this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
hope it helps,
faisal ahmed
blog / site

Resources