Insert one time two table user for loop getting error - codeigniter

I want to insert script using CodeIgniter two table table1 and table2. table1 insert one time date but table2 three times date using for loop but getting an error. Loop is not working, kindly provide some suggestions.
Thanks
Code:
public function add_rdi() {
$user_id = $this -> input -> post('user_id');
$user_rdi = $this -> input -> post('user_rdi');
$zoneID = $this -> input -> post('zoneID');
$mtstock6 = $this -> input -> post('mtstock6');
$tfstank6 = $this -> input -> post('tfstank6');
$data = [
'user_id' => $user_id,
'user_rdi' => $user_rdi,
'zoneID' => $zoneID,
'mtstock6' => $mtstock6,
'tfstank6' => $tfstank6,
];
$data1 = ['fuelstock' => $gtfb,
'fuelrecrdi' => $gtfb,
'stockrdiname' => $user_rdi,
'vfstock' => $tfstank1,
'vfstock' => $tfstank2,
'vfstock' => $tfstank3,
'hfstock' => $tfstank4,
'hfstock' => $tfstank5,
'hfstock' => $tfstank6,
'user_id' => $user_id
];
$this -> load -> model('Mechanical_model_rdi');
$query = $this -> db -> where('rdiname', $rdiname);
$query = $this -> db -> get('fuel_rdi');
if ($query -> num_rows() < 1) {
$this -> Mechanical_model_rdi -> add_rdi($data);
for ($i = 1; $i <= 3; $i++) {
$this -> Mechanical_model_rdi -> add_rdi($data1);
}
} $this -> load -> view('mechanical/Mechanical_add_rdi');
}

Related

laravel DB::transaction how to update records?

I have a function in a controller like this:
DB::transaction(function () use ($request) {
for ($parts=0; $parts < count($request->name) ; $parts++) {
$parts_info = new PartsInfo;
$parts_info -> part_id = $request -> id;
$parts_info -> name = $request -> name[$parts];
$parts_info -> qty = $request -> qty[$parts];
$parts_info -> price = $request -> price[$parts];
$parts_info -> save();
}
});
Now I want to update the records. How do I do that? Here multiple records have the same part_id which has a one-to-many relationship with another table.
Note: Tried using $parts_info = PartsInfo::where('parts_id', $request->id)->get(); and without get()
Try this one
DB::transaction(function () use ($request) {
for ($parts=0; $parts < count($request->parts) ; $parts++) {
PartsInfo::where("parts_id", $request->id)->update([
"name" => $request->name[$parts],
"qty" => $request->qty[$parts],
"price" => $request->price[$parts]
]);
}
});
try this one
DB::transaction(function () use ($request) {
for ($parts=0; $parts < count($request->parts) ; $parts++) {
PartsInfo::where('id', $request->id[$parts])->update([
"name" => $request->name[$parts],
"qty" => $request->qty[$parts],
"price" => $request->price[$parts]
]);
}
});
You will have to check it with the id and because you are checking it with parts_id so it will not work
or You can first delete the child records and then recreate it
DB::transaction(function () use ($request) {
$count = $request->parts;
PartsInfo::where('parts_id', $request->id)->delete();
for ($parts=0; $parts < $count ; $parts++) {
PartsInfo::create([
'parts_id' => $request->id,
"name" => $request->name[$parts],
"qty" => $request->qty[$parts],
"price" => $request->price[$parts]
]);
}
});

how to add another value in exiting array from another code block in Laravel App

Pass/Append value in an array from different block of code in Laravel app. Actually, I want to pass value into existing array if file upload.
First block of code
$fileName = $request -> fullName."_".$request -> rollNumber.".jpeg";
$fileUploadPath = public_path().'/uploads/';
if (file_put_contents($fileUploadPath.$fileName, file_get_contents($request->profilePhoto))) {
$arr = ['profilePhoto' => $fileName]; //want to append this val
} else {
if (file_exists($fileName))
unlink($fileName);
//photo not selected to upload
}
Second block of code
$id = DB::table('students_personal_info_tbl')->insertGetId($arr = [
// 'profilePhoto' => $fileName,
'fullName' => $request -> fullName,
'gender' => $request -> gender
]

I want to fetch students data, but it only fetches a single line of data

I'm developing a student information system, and I'm stuck in this problem. Whenever I fetch the student data, it only returns a single row of data.
Can you help me? How to fix this problem? Thank you
controller
function Account_Validation() {
$this->form_validation->set_rules('Username', 'Username', 'required');
$this->form_validation->set_rules('Password', 'Password', 'required');
$this->form_validation->set_error_delimiters('<span class="text-danger">', '</p>');
if($this -> form_validation -> run() == FALSE) {
$this -> load -> view ('login');
} else {
$username = $this -> input -> post('Username');
$password = md5($this -> input -> post('Password'));
$check = $this -> Model_login -> login($username, $password);
if($check) {
foreach($check as $row);
$data = array (
'User_id' => $row -> User_id,
'Confirm_password' => $row -> Confirm_password,
'sectioning_id' => $row -> sectioning_id,
'default_section_id' => $row -> default_section_id,
'default_section' => $row -> default_section,
'yeargrade_id' => $row -> yeargrade_id,
'Username' => $row -> Username,
'status' => $row -> status,
'Level' => $row -> Level,
'first_name' => $row -> first_name,
'middle_name' => $row -> middle_name,
'last_name' => $row -> last_name,
'programs' => $row -> programs,
'userfile' => $row -> userfile,
);
$this -> session -> set_userdata($data);
if($this -> session -> userdata('Level') == "1") {
$this -> session -> set_flashdata('Success', "You've Successfuly Logged In");
redirect('Admin/Dashboard');
} elseif ($this -> session -> userdata('Level') == "2") {
$this -> session -> set_flashdata('Success', 'Successfuly Logged In');
redirect('Registrar/Dashboard');
}
} else {
$this -> session -> set_flashdata('error', 'Invalid Username/Password Combination');
redirect('login');
}
}
}
Model
function login($username, $password) {
$this -> db -> select('a.User_id, a.Username, a.Password, a.userfile, a.status, a.Level, a.first_name, a.middle_name, a.last_name, b.default_section_id, c.default_section, d.yeargrade_id, e.subject, a.Confirm_password');
$this -> db -> from('user_tbl a');
$this -> db -> join ('sectioning_tbl b', 'a.User_id = b.User_id', 'left');
$this -> db -> join ('default_section_tbl c', 'b.default_section_id = c.default_section_id', 'left');
$this -> db -> join ('studentyeartbl d', 'd.yeargrade_id = c.default_section_id', 'left');
$this -> db -> join ('default_subject_tbl e', 'e.yeargrade_id = c.yeargrade_id', 'left');
$this -> db -> where ('Username', $username);
$this -> db -> where ('Password', $password);
$result = $this -> db -> get ();
$this -> db -> limit (1);
return $result -> result();
}
I think you should remove this line from your model:
$this -> db -> limit (1);
That funciton on codeigniter puts LIMIT 1 in the query, so you'll only get one row.
On the other side, if that's a login, makes sense you only fetch one row. And your controller seems to have logic to handle several rows, but will redirect on the first match of username and password so if you have several users with the same login, maybe you'll give access to the wrong user.
Maybe this will work.
- Model
function login($username, $password) {
$this -> db -> select('a.User_id, a.Username, a.Password, a.userfile, a.status, a.Level, a.first_name, a.middle_name, a.last_name, b.default_section_id, c.default_section, d.yeargrade_id, e.subject, a.Confirm_password');
$this -> db -> from('user_tbl a');
$this -> db -> join ('sectioning_tbl b', 'a.User_id = b.User_id', 'left');
$this -> db -> join ('default_section_tbl c', 'b.default_section_id = c.default_section_id', 'left');
$this -> db -> join ('studentyeartbl d', 'd.yeargrade_id = c.default_section_id', 'left');
$this -> db -> join ('default_subject_tbl e', 'e.yeargrade_id = c.yeargrade_id', 'left');
$this -> db -> where ('Username', $username);
$this -> db -> where ('Password', $password);
$result = $this -> db -> get ();
$result = $result -> result();
return ( isset($result[0]) ? $result[0] : FALSE);
}
Controller
function Account_Validation() {
$this->form_validation->set_rules('Username', 'Username', 'required');
$this->form_validation->set_rules('Password', 'Password', 'required');
$this->form_validation->set_error_delimiters('<span class="text-danger">', '</p>');
if($this -> form_validation -> run() == FALSE) {
$this -> load -> view ('login');
} else {
$username = $this -> input -> post('Username');
$password = md5($this -> input -> post('Password'));
$row = $this -> Model_login -> login($username, $password);
if($row) {
$data = array (
'User_id' => $row -> User_id,
'Confirm_password' => $row -> Confirm_password,
'sectioning_id' => $row -> sectioning_id,
'default_section_id' => $row -> default_section_id,
'default_section' => $row -> default_section,
'yeargrade_id' => $row -> yeargrade_id,
'Username' => $row -> Username,
'status' => $row -> status,
'Level' => $row -> Level,
'first_name' => $row -> first_name,
'middle_name' => $row -> middle_name,
'last_name' => $row -> last_name,
'programs' => $row -> programs,
'userfile' => $row -> userfile,
);
$this -> session -> set_userdata($data);
if($this -> session -> userdata('Level') == "1") {
$this -> session -> set_flashdata('Success', "You've Successfuly Logged In");
redirect('Admin/Dashboard');
} elseif ($this -> session -> userdata('Level') == "2") {
$this -> session -> set_flashdata('Success', 'Successfuly Logged In');
redirect('Registrar/Dashboard');
}
} else {
$this -> session -> set_flashdata('error', 'Invalid Username/Password Combination');
redirect('login');
}
}

How to Show Categories and multilevel Subcategories in Codeigniter Dropdown list

Hello Friends i am New in Codeigniter i have no Experience in any project i am making Shopping Website for Learning Purpose only not Real Project.When Creating Admin Panel I am Able to Fetch Categories and Subcategories tree using Loop but how to get these categories in Dropdown Menu in Tree format.
Like this-
Electronics
->Phones
->Tablets
->Mobiles
->Computer
->Laptops
->Desktops
Furniture
->Tables
->Plastic Table
->Wooden Table
My Model is :-
function getcategories($parent_id = 0) {
$categories = array();
$this->db->where('parentid',$parent_id);
$query = $this->db->get('categories');
$result = $query->result_array();
foreach ($result as $mainCategory) {
$category = array();
$category['id'] = $mainCategory['id'];
$category['categoryname'] = $mainCategory['categoryname'];
$category['parentid'] = $mainCategory['parentid'];
$category['sub_categories'] = $this->getcategories($category['id']);
$categories[$mainCategory['id']] = $category;
}
return $categories;
}
My Categories array is Look like this:-
Array
(
[category] => Array
(
[1] => Array
(
[id] => 1
[categoryname] => Electronics
[parentid] => 0
[sub_categories] => Array
(
[2] => Array
(
[id] => 2
[categoryname] => Smartphones
[parentid] => 1
[sub_categories] => Array
(
[4] => Array
(
[id] => 4
[categoryname] => Sony
[parentid] => 2
[sub_categories] => Array
(
)
)
)
)
[3] => Array
(
[id] => 3
[categoryname] => Televisions
[parentid] => 1
[sub_categories] => Array
(
)
)
)
)
)
)
my database table look like this:-
it is my database table
This code works:
function getcategories($parent_id = 0) {
$categories = array();
$category = array();
$subcategories = array();
$q = $this->db->get_where('categories', array('parent_id' => $parent_id));
$results = $q->result();
foreach ($results as $mainCat) {
$q2 = $this->db->get_where('categories', array('parent_id' => $mainCat->id));
$results2 = $q2->result();
foreach ($results2 as $key => $subCat) {
$subcategory['id'] = $subCat->id;
$subcategory['name'] = $subCat->name;
$subcategory['parentid'] = $subCat->parent_id;
$subcategories[$subCat->id] = $subcategory;
}
$category['id'] = $mainCat->id;
$category['name'] = $mainCat->name;
$category['parentid'] = $mainCat->parent_id;
$category['subcat'] = $subcategories;
$categories[$mainCat->id] = $category;
$subcategories='';
}
return $categories;
}

codeigniter: call multiple data in the view

I can't pass properly multiple data in the view and use it in foreach.
this is my function in controller:
public function pdf() {
$this->load->model("model_get");
$tabela = 'qualities';
$qualities['qualities'] = $this->model_get->get_data($tabela);
$tabela = 'language';
$language['language'] = $this->model_get->get_data($tabela);
$data = array($qualities, $language);
$this->load->view('download_pdf', $data);
}
and this is my view:
<?php
foreach ($qualities as $row) {
echo $row -> qualities;
}
foreach ($language as $row) {
echo $row -> language;
echo $row -> speaking;
echo $row -> listening;
echo $row -> reading;
echo $row -> writing;
}
?>
with model everything is fine, and controller is ok too, i gues.
Try to pass this $data array to the view:
$data['qualities'] = $qualities;
$data['language'] = $language;
$this->load->view('download_pdf', $data);
Basically you make a associative array(An array with a key and a value) where the key of the array is the variable where you call the array in the view. Because when you pass an array to the view it will unfold into variables.
Example
Controller:
$data['qualities'] = array( 'key1' => 'test', 'key2' =>'test1');
$data['language'] = array( 'key3' => 'test3', 'key4' =>'test4');
Will be unfolded to
View:
$qualities = array( 'key1' => 'test', 'key2' =>'test1');
$language = array( 'key3' => 'test3', 'key4' =>'test4');
Hope this was helpful.

Resources