How to add data after comparison in codeigniter - php

I have two tables (mark and enroll). Mark table contains two students (ally, hassan) while Enroll table contains four students (ally, hassan, zuwena, saada).
I need to compare these two tables, then after comparison to add missing students from enroll table into mark table without duplication.
But I'm using codeigniter.
I tried this but I got errors'
$data['exam_id'] = $this->input->post('exam_id');
$data['class_id'] = $this->input->post('class_id');
$data['section_id'] = $this->input->post('section_id');
$data['subject_id'] = $this->input->post('subject_id');
$data['year'] = $this->db->get_where('settings' , array('type'=>'running_year'))->row()->description;
$query = $this->db->get_where('mark' , array(
'exam_id' => $data['exam_id'],
'class_id' => $data['class_id'],
'section_id' => $data['section_id'],
'subject_id' => $data['subject_id'],
'year' => $data['year']
))->result_array();
$query1 = $this->db->get_where('enroll', array(
'class_id' => $data['class_id'],
'section_id' => $data['section_id'],
'year' => $data['year']))->result_array();
$query3 = count($query1);
$query4 = count($query);
if ($query3 != $query4){
$students = $this->db->get_where('enroll' , array(
'class_id' => $data['class_id'] , 'section_id' => $data['section_id'] , 'year' => $data['year']))->result_array();
foreach($students as $row)
{
$data['student_id'] = $row['student_id'];
$this->db->not_like('mark' , $data);
$query5 = $this->db->from('mark');
$this->db->insert('mark' , $query5);
}

Based on your previous codes, if you want to add the missing mark data from enroll data, you could change your foreach block to :
if ($query3 != $query4){
// first get all the mark student_id data
$mark_student_id = $this->db->select('student_id')->get('mark')->result_array();
// then filter enroll data which does not contain mark student_id data
$new_students = $this->db->where( array(
'class_id' => $data['class_id'] , 'section_id' => $data['section_id'] , 'year' => $data['year']
))->where_not_in('student_id' , array_column($mark_student_id, 'student_id'))->get('enroll')->result_array();
// then insert filtered enroll data to mark table
$this->db->insert('mark' , $new_students);
you could restore the previous codes on your question

Thanks #Hasta Dhana, I have done a little changes to your answer according to my needs and to solve this error "you have to use the set method to update an entry" as follows,
if ($query3 != $query4){
// first get all the mark student_id data
$this->db->where(array('exam_id' => $data['exam_id'],'subject_id' => $data['subject_id']));
$mark_student_id = $this->db->select('student_id')->get('mark')->result_array();
if(count($mark_student_id)>0){
// then filter enroll data which does not contain mark student_id data
$new_students = $this->db->where( array(
'class_id' => $data['class_id'] , 'section_id' => $data['section_id'] , 'year' => $data['year']
))->where_not_in('student_id' , array_column($mark_student_id, 'student_id'))->get('enroll');
// then insert filtered enroll data to mark table
if($new_students->num_rows()>0){
foreach ($new_students->result_array() as $row){
$data['student_id'] = $row['student_id'];
$this->db->insert('mark' , $data);
}
}
}else {
$data1['exam_id'] = $this->input->post('exam_id');
$data1['class_id'] = $this->input->post('class_id');
$data1['section_id'] = $this->input->post('section_id');
$data1['subject_id'] = $this->input->post('subject_id');
$data1['year'] = $this->db->get_where('settings' , array('type'=>'running_year'))->row()->description;
$students = $this->db->get_where('enroll' , array(
'class_id' => $data1['class_id'] , 'section_id' => $data1['section_id'] , 'year' => $data1['year']))->result_array();
foreach($students as $row)
{
$data1['student_id'] = $row['student_id'];
$this->db->insert('mark' , $data1);
}
}
}
That is how was done, thanks.

Related

Update a field in mysql

I have this code here:
$userid = Secure($_POST[ 'username' ]);
$user = $db->where('username', $userid)->getOne('users',array('id','web_device_id','is_pro'));
$is_user_matches = $db->where('user_id', $user['id'])->where('like_userid', self::ActiveUser()->id)->getOne('likes', array('id'));
$id = $db->where('user_id', self::ActiveUser()->id)->where('like_userid', $user['id'])->getValue('likes', 'id');
$saved = false;
if ($id > 0) {
$saved = $db->where('id', $id)->update('likes', array(
'user_id' => self::ActiveUser()->id,
'like_userid' => $user['id'],
'is_like' => 1,
'is_dislike' => 0,
'created_at' => date('Y-m-d H:i:s')
));
} else {
if(isUserLiked($user['id']) === false) {
$saved = $db->insert('likes', array(
'user_id' => self::ActiveUser()->id,
'like_userid' => $user['id'],
'is_like' => 1,
'is_dislike' => 0,
'created_at' => date('Y-m-d H:i:s')
));
}
}
These are the actions that occur when a user presses the LIKE button!
Now, my question is. How can I add in addition to these codes, an UPDATE for a table in mysql?
I would like that when a user gives a Like to a person, he will count automatically.
mysql_query("
UPDATE users
SET likes = likes + 1
WHERE id = '".$user['id']."'
");
I tried to add this code but I couldn't. I don't think I'm adding it where it should be. Can you show me an example please?

codeigniter php Event management system.. I want to make notification when the event is created

Good Day sir. can someone help me i'm trying to code for notification. Event management system. The admin will create the event for scholars. During the process the creation of the event the admin will choose what scholarship is the event for. When the admin finish creating the event this scholars from this scholarship will automatically receive their notification. My problem is i can't insert the event_id into my notification table in the database. i don't know why my $this->db->insert_id() in $data7. the $data5 and $data6 is working
public function create_event_scholar()
{
$now = date('Y-m-d');
$start=$_POST['start_date'];
$end=$_POST['end_date'];
if(($start==$now)||$end==$now){
$event_status= "Ongoing";
}
else if(($start < $now)&&($end>$now)){
$event_status= "Ongoing";
}
else if(($start<$now)&&($end<$now)){
$event_status= "Done";
}
else {
$event_status= "Upcoming";
}
$data = array
(
'event_id' => '',
'event_title' => $this->input->post('event_title',true),
'event_desc' => $this->input->post('event_desc',true),
'start_date' => $this->input->post("start_date",true),
'end_date' => $this->input->post("end_date",true),
/*'event_status' => $event_status,*/
'event_sponsor' => $this->input->post('event_sponsor',true),
'Semester' => $this->input->post("Semester",true),
'school_year' => $this->input->post('school_year',true),
'event_for' => 'Scholar',
'start_time' => $this->input->post('start_time',true),
'venue' => $this->input->post('venue',true),
'end_time' => $this->input->post('end_time',true),
'user_id' => $this->input->post('user_id',true),
'color ' => $this->input->post('color',true)
);
$sql1= $this->db->insert('event',$data);
$earlier = new DateTime($_POST['start_date']);
$later = new DateTime($_POST['end_date']);
$diff = $later->diff($earlier)->format("%a");
$data = array(
'security_id' => "",
'event_id' =>$this->db->insert_id(),
'scholarship' =>$this->input->post('scholarship',true),
'max_attendees' =>$this->input->post('Participants',true),
'pre_reg' =>$this->input->post('pre',true),
'total_attendance'=>$diff,
'event_duration' =>$this->input->post('event_duration',true),
);
$sql2=$this->db->insert('event_security',$data,'event_id');
$data3=array(
'assign_id' => '',
'user_id' =>$this->input->post('user_id',true),
'event_id' =>$this->db->insert_id()
);
$sql3= $this->db->insert('tbl_staff_events',$data3);
$data5 = array(
'notification_id' =>'',
'subject' =>$this->input->post('event_title',true),
'body' =>"There is an event",
'event_id' =>$this->db->insert_id()
);
$this->db->insert('notification',$data5);
$subject=$this->input->post('scholarship',true);
$this->db->select('*');
$this->db->from('tbl_scholar');
$this->db->where('scholarship_id',$subject);
$get= $this->db->get()->result_array();
foreach ($get as $value) {
$data6=array(
'participants_id' => '',
'event_id' =>$this->db->insert_id(),
'user_id' =>$value['user_id']
);
$this->db->insert('participants',$data6);
}
$data7= array(
'sent_id' =>'',
'read' =>'0',
'recieve' =>'0',
'notification_id' =>$this->db->insert_id(),
'participants_id' =>$this->db->insert_id()
);
$this->db->insert('sent_notification',$data7);
if($sql1===true && $sql2===true && $sql3 === true)
{
return true;
}
else
{
return false;
}
}
This code may help you,
$sql1= $this->db->insert('event',$data);
$event_id = $this->db->insert_id();
$this->db->insert('notification',$data5);
$notification_id = $this->db->insert_id();
$this->db->insert('participants',$data6);
$participants_id = $this->db->insert_id();
$this->db->insert_id(); returns the last executed query id

How to select from 2 different table and insert to 1 table

I have a function is select from books table insert to the orders table.
Now I want to select from books and users table for insert to the orders table.
This is book table
This is the orders table
This is the users table
model
public function history($book_id){
$this->db->select('book_id,book_title,pickup,return');
$this->db->from('books');
$this->db->where('book_id', $book_id);
$query = $this->db->get();
if ( $query->num_rows() > 0 ) // if result found
{
$row = $query->result_array();
foreach($row as $values){
$data = array(
'book_id' => $values['book_id'],
'title' => $values['book_title'],
'pickup' => $values['pickup'],
'return_time' => $values['return'],
);
$this->db->insert('orders', $data);
}
return true;
}
else{
return false;
} }
You will join the tables like this
$this->db->join('users_table_name', 'users_table_name.id = books.user_id');
Everything else should work the same
for user_id u can get from session,
for example :
$data = [
'book_id' => $values['book_id'],
'user_id' => $this->session->userdata('session_name'),
'title' => $values['book_title'],
'pickup' => $values['pickup'],
'return_time' => $values['return'],
];
hopes this helps
public function insert_record($book_id, $user_id) {
$book = $this->db->select('book_id, book_title, pickup, return')
->from('books')
->where('book_id', $book_id)
->get()
->result_array();
if(!empty($book)) {
$data = array(
'book_id' => $book[0]['book_id'],
'user_id' => $user_id,
'title' => $book[0]['book_title'],
'pickup' => $book[0]['pickup'],
'return_time' => $book[0]['return'],
);
$this->db->insert('orders', $data);
}
}

PHP mysql select to array and check if value exist

I have this MySQL table:
Where I want to get count of each platform (windows, linux, mac, unknown).
Notice that in table is no mac or unknown data, there for num will be 0
Conditions:
order must be like in wanted output
check if plaform is in table, if not add zero to output
Question: How to sort in query to have order like in wanted output, and add zero if no platform in table?
Wanted output:
array (
'0' => array('name' => 'windows', 'num' => 3),
'1' => array('name' => 'linux', 'num' => 3),
'2' => array('name' => 'mac', 'num' => 0),
'3' => array('name' => 'unknown', 'num' => 0)
);
My try:
PHP:
function get_platforms() {
$query = "
SELECT platform, COUNT(platform) AS num
FROM stats
GROUP BY platform
ORDER BY platform ASC
";
$select = mysqli_query($this->c, $query);
while ($row = mysqli_fetch_array($select)) {
$data[] = array(
'name' => $row['platform'],
'num' => $row['num']
);
}
return $data;
}
Current outpup:
array (
'0' => array ('name' => 'linux', 'num' => 3)
'1' => array ('name' => 'windows', 'num' => 3)
);
Because you don't have any mac or unknown data in your database, you will not get any result to this platform, not even 0. What you can do is to have a preconfigured array with the platforms you are expecting to get:
$data = array(
'windows' => 0,
'linux' => 0,
'mac' => 0,
'unknown' => 0
)
Now in your get_platforms function do this:
function get_platforms() {
$query = "
SELECT platform, COUNT(platform) AS num
FROM stats
GROUP BY platform
ORDER BY platform ASC
";
$select = mysqli_query($this->c, $query);
while ($row = mysqli_fetch_array($select)) {
$data[$row['platform']] = $row['num'];
}
return $data;
}
you will end up with this:
$data = array(
'windows' => 3,
'linux' => 3,
'mac' => 0,
'unknown' => 0
)
And then you can reformat the data in the way that you want. For example you can do this:
$result = array();
foreach($data as $platform => $count){
$result[] = array('name' => $platform, 'count' => $count);
}
And you will end up with the result in the same format you're asking.
First of all you should define an order of desired output, lets say you have array
$osList = array('linux', 'windows', 'mac', 'something');
Now fetch your mysql results to temporary assoc array:
$data = array();
$query = "SELECT platform, COUNT(platform) AS num"
. " FROM stats"
. " GROUP BY platform";
$result = mysqli_query($this->c, $query);
while ($row = mysqli_fetch_array($result)) {
$data[$row['platform']] = (int) $row['num'];
}
And here you can form output array:
$output = array();
foreach ($osList as $os) {
$output[] = array(
'name' => $os,
'count' => isset($data[$os]) ? $data[$os] : 0
);
}
You must have another table which has all the platforms listed there. For example we call it platforms which has a column platform_name with all the unique names of platforms (including mac), then the query would be:
SELECT platforms.`platform_name`, COUNT(stats.`platform`) FROM `platforms`
LEFT JOIN `stats` ON platforms.`platform_name` = stats.`platform`
GROUP BY platforms.`platform_name`

active record issue to return array (codeigniter)

I have a table called uploaded_files in my database where I have the following fields:
id, file_name, upload_id, filetype_id
this last field (filetype_id) is a Foreign Key to a table called filetype where I have these fields:
id, filetype_desc
I'm trying to create a method in my model to return all the fields in table uploaded_files, in an array following this structure:
array(
'file_type_1' => array(
'id' => 1,
'file_name' => "lalala.txt",
'upload_id' => $uploadId,
'filetype_id' => 1
),
'file_type_2' => array(
'id' => 1,
'file_name' => "lelele.txt",
'upload_id' => $uploadId,
'filetype_id' => 2
),
'file_type_3' => array(
'id' => 1,
'file_name' => "blabla.txt",
'upload_id' => $uploadId,
'filetype_id' => 3
),
);
}
Until now, I came to this:
public function get_UploadedFilesFromUploadId($uploadId){
//$query = "select * from uploaded_files where upload_id =".$uploadId;
$query = $this->db->get_where('uploaded_files', array(
'upload_id' => $uploadId
)
);
return $this->db->query($query);
}
$query = $this->db->get_where('uploaded_files', array(
'upload_id' => $uploadId
)
);
$res = $this->db->query($query);
$result = array();
foreach ($res->result_array() as $row)
{
$key = 'file_type_'.$row['filetype_id'];
$result[$key] = $row;
}
$query = $this->db->get_where('uploaded_files',array('upload_id' => $uploadId));
foreach($query->result() as $row)
{
echo $row->filetype_id;
}
I think it's help you.

Categories