Hello guys I am trying to insert data into my database. Some values contain the same id. I want to sum during insertion and not add a new row:
if ($q->num_rows() >0)
{
$this->db->where('student_id',$id);
$this->db->set('mark_obtained', ' mark_obtained+ '. $obtained_marks['mark_obtained'], FALSE);
$this->db->update('position' , array('mark_obtained' => $obtained_marks , 'student_id' =>$row['student_id'] , 'exam_id'=> $row['exam_id'],'subject_id' =>$row['subject_id'],'section_id'=>$row['section_id'], 'class_id'=>$row['class_id']));
}
else {
$this->db->insert('position' , array('mark_obtained' => $obtained_marks , 'student_id' =>$row['student_id'] , 'exam_id'=> $row['exam_id'],'subject_id' =>$row['subject_id'],'section_id'=>$row['section_id'], 'class_id'=>$row['class_id']));
}
Before going to Query you should sum the same id value in array then pass the data into Your Query check this link to
Sum the array having same keys
Related
I have a search form with 4 inputs such as username, text, fromDate, toDate, and users can search with just one field , the problem is i don't know how to build a query with inputs which have values, I can compare them if each one of them has value or not like this :
if ($request->input('fromdatepicker') && $request->input('todatepicker') && $request->input('search-text')){
$query = \App\InstaPost::WhereFullTextWithTimestamp($request->input('search-text'), $from_timestamp, $to_timestamp)->paginate(12);
}else if ($request->input('search-text') && empty($request->input('fromdatepicker')) && empty($request->input('todatepicker'))){
$query = \App\InstaPost::WhereFullText($request->input('search-text'))->paginate(12);
} else if(empty($request->input('search-text')) && $request->input('fromdatepicker') && $request->input('todatepicker')){
$query = InstaPost::WhereTimestamp($from_timestamp, $to_timestamp)->paginate(12);
}
i have different scenarios:
as you can see so many scenarios,
but as you know it'll be a huge mess ! these if's is just for 3 inputs! and i should compare them for each scenarios , hope you understand the problem and help me.
I'm using Laravel-Mongodb(jessengers) and i should find out which input has value and then make a query and add them to this code block :
public function scopeWhereFullTextWithTimestamp($query,$search,$from_timestamp , $to_timestamp)
{
$query->getQuery()->projections = ['score'=>['$meta'=>'textScore']];
$query->orderBy('post.taken_at_timestamp','DESC');
return $query->whereRaw([
'$text' => ['$search' => $search],
'post.taken_at_timestamp'=> [
'$gte' => $from_timestamp,
'$lte' => $to_timestamp
]
]);
this is for search-text , fromDate and toDate inputs,
You can concatenate queries based on the input, something in this direction should do the trick.
$base_query = App\InstaPost;
if(!is_null($request->input('search-text'))){
$base_query->WhereFullText($request->input('search-text'));
}
if...
if..
$result = $base_query->get(); //or paginate()
I have array i want insert into the values in database using codeigniter, i don't know how to insert , i trying but i am not able to get the answer
My model
print_r($subjectHandling);
Array
(
[subjectId] => Array
(
[0] => 1
[1] => 2
)
)
now i want insert the values in database in this values.
I am trying like this
foreach($subjectHandling as $key=>$value) {
$reg_dat = array(
'statffId' => '1',
'subjectId' => $value,
);
$this->db->insert("subject_handling" , $reg_dat);
}
I m getting error ** Array to string conversion** , so how do this. i want to insert two roes in databse
This should work
$subjectHandling['subjectId'] = array(1, 2);
$reg_dat = array();
foreach($subjectHandling['subjectId'] as $key => $value) {
$reg_dat[] = array('staffId'=> 1, 'subjectId' => $value);
}
$this->db->insert_batch('subject_handling', $reg_dat);
https://www.codeigniter.com/userguide3/database/query_builder.html#inserting-data
I have created a calculated field, in my vardefs the field is non-db, sortable and type varchar. I have a logic hooks on retrieve and a function with the calculation, works like a charm in edit and list view. But when I click on the column name calculated field (listview) the query return 0 records.
I tried several things with no success, I'm missing something.
the query when I click on the calculated column (field = 'difference_c'):
SELECT aos_quotes.id , aos_quotes.number , aos_quotes.name , aos_quotes.stage , aos_quotes.billing_contact_id, LTRIM(RTRIM(CONCAT(IFNULL(jt0.first_name,''),' ',IFNULL(jt0.last_name,'')))) billing_contact , aos_quotes.billing_account_id, jt1.name billing_account , aos_quotes.total_amount , aos_quotes.currency_id , aos_quotes.expiration , LTRIM(RTRIM(CONCAT(IFNULL(jt2.first_name,''),' ',IFNULL(jt2.last_name,'')))) assigned_user_name , jt2.created_by assigned_user_name_owner , 'Users' assigned_user_name_mod, aos_quotes.date_entered , aos_quotes.enddate_c , aos_quotes.startdate_c , aos_quotes.assigned_user_id FROM aos_quotes LEFT JOIN contacts jt0 ON aos_quotes.billing_contact_id = jt0.id AND jt0.deleted=0 LEFT JOIN accounts jt1 ON aos_quotes.billing_account_id = jt1.id AND jt1.deleted=0 LEFT JOIN users jt2 ON aos_quotes.assigned_user_id=jt2.id AND jt2.deleted=0 AND jt2.deleted=0 where aos_quotes.deleted=0 ORDER BY difference_c ASC
Unfortunately you can NOT order ListView records by non-db field. To disallow users to do so, you should set 'sortable' key to false in field definition in listviewdefs.php files like:
'MY_CUSTOM_NON-DB_FIELD' =>
array (
'type' => 'char',
'label' => 'LBL_MY_CUSTOM_NON-DB_FIELD',
'width' => '15%',
'sortable' => false,
),
I have following query:
var accounts =
from account in context.Accounts
from guranteer in account.Gurantors
select new AccountsReport
{
CreditRegistryId = account.CreditRegistryId,
AccountNumber = account.AccountNo,
DateOpened = account.DateOpened,
};
return accounts.AsEnumerable()
.Select((account, index) => new AccountsReport()
{
RecordNumber = FormattedRowNumber(account, index + 1),
CreditRegistryId = account.CreditRegistryId,
AccountNumber = FormattedAccountNumber(account.AccountType, account.AccountNumber)}).OrderByDescending(c => c.StateChangeDate);
It works fine except one problem and that is it returns records number in reverse order like 3, 2,1 because of .OrderByDescending(c => c.StateChangeDate);
Can I show the record number in ascendeing order while keeping the records in descending order.
Please suggest.
Thanks
Try using OrderBy on RecordNumber and then ThenByDescending on StateChangeDate
.OrderBy(c => c.RecordNumber).ThenByDescending(c => c.StateChangeDate);
.OrderBy(y => y.Year).ThenBy(m => m.Month);
How to set descending order?
EDIT:
I tried this:
var result = (from dn in db.DealNotes
where dn.DealID == dealID
group dn by new { month = dn.Date.Month, year = dn.Date.Year } into date
orderby date.Key.year descending
orderby date.Key.month descending
select new DealNoteDateListView {
DisplayDate = date.Key.month + "-" + date.Key.year,
Month = date.Key.month,
Year = date.Key.year,
Count = date.Count()
})
//.OrderBy(y => y.Year).ThenBy(m => m.Month)
;
And it seems working. Is it wrong to use orderby twice like I used it here?
You can get the descending ordering by using a different pair of methods:
items.OrderByDescending(y => y.Year).ThenByDescending(m => m.Month);
Using LINQ query, you can write:
from date in db.Dates
orderby date.Key.year descending, date.Key.month descending
select new { ... }
The trick is that you need only one orderby clause - if you add multiple of these, the list will be re-sorted each time. To sort elements that have the first key equal using another key, you need to separate them using , (orderby is translated to OrderBy or OrderByDescending while , is translated to ThenBy or ThenByDescending).