Yii2 dataprovider- exclude first 2 elements - yii2

How can i exclude first 2 elements in dataprovider and get other elemets by every 3?
1 2 3 4 5 6 7 8 9 0 10 12 13 14
x x | | | | |
I need such results
3 4 5
6 7 8
9 0 10
12 13 14

You can retrieve all models form existing dataprovider, then filter items using PHP as you want, then make new data provider from these items using ArrayDataProvider.
For example:
// Some prepared data provider
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
$dataProvider->query
->orderBy(['created_at' => SORT_DESC]);
// Retrieving all models
$allModels = $dataProvider->getModels();
// Some manipulations with $allModels array
// ...
// Preparing new data provider from modified array of models
$dataProvider = new ArrayDataProvider();
$dataProvider->allModels = $allModels;
$dataProvider->key = 'id';
$dataProvider->pagination->setPageSize($maxRows);

Related

Printing rows with certain text in column 2 with app script

I need to print off certain rows in a google sheet depending on what is in column 2 of that row. I know how to find the rows with a for loop but the rest eludes me. Perhaps my googling skills are rusty.
This is what I have.
var app = SpreadsheetApp;
var rows = app.getActive().getSheetByName("Sheet").getMaxRows().toString();
var rows = rows.replace(".0","");
function findRows(){
for (var counter = 1; counter <= rows; counter = counter+1){
if(app.getActive().getSheetByName("Sheet").getRange(counter, 2) == "example" || "example2"){
}
}
Find the correct rows
function findrows() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const osh = sh.getSheetByName("Sheet1");
const vs = sh.getDataRange().getValues();
let s = vs.map(r => {
if(r[1] == "Example" || r[1] == "example2") {
return r;
}
}).filter(e => e);
Logger.log(JSON.stringify(s));
//you can output to a sheet with something like
//sheet.getRange(1,1,s.length,s[0].length).setValues(s);
osh.getRange(1,1,s.length,s[0].length).setValues(s);//put on another sheet
}
Execution log
4:56:34 PM Notice Execution started
4:56:35 PM Info [[2,"Example",4,5],[5,"Example",7,8],[9,"Example",11,12],[12,"Example",14,15]]
4:56:35 PM Notice Execution completed
Data:
COL1
COL2
COL3
COL4
1
2
3
4
2
Example
4
5
3
4
5
6
4
5
6
7
5
Example
7
8
6
7
8
9
7
8
9
10
8
9
10
11
9
Example
11
12
10
11
12
13
11
12
13
14
12
Example
14
15
13
14
15
16
BTW Printing is not easily done from Javascript or Google Apps Script

Codeigniter - left join tables

i have a table for my sql like this :
result_podium
id_result_podium
id_race
id_rider
position
point
and
result_dnf
id_result_dnf
id_race
id_rider
So what im trying to do is to join those two table and shown it as one, and here is the result and what i have tried so far :
Attempt 1 :
$this->db->select_sum('result_podium.point');
$this->db->select('riders.name AS rider_name, teams.name AS team_name');
$this->db->from('result_podium');
$this->db->join('riders','riders.id_rider = result_podium.id_rider');
$this->db->join('result_dnf','result_dnf.id_rider = riders.id_rider', 'left');
$this->db->join('teams','teams.id_team = riders.id_team');
$this->db->where('riders.is_deleted','0');
$this->db->order_by("SUM(result_podium.point) DESC");
$this->db->group_by(array('result_podium.id_rider','result_dnf.id_rider'));
$query = $this->db->get();
return $query->result();
And the result is something like this :
No
rider
point
1
Nick
10
2
A
5
3
D
5
4
CC
4
5
B
4
it managed to get data from table result_podium but not managed to join data from table result_dnf, and here is my another attempt :
Attempt 2 :
$this->db->select_sum('result_podium.point');
$this->db->select('result_dnf.*, riders.name AS rider_name, teams.name AS team_name');
$this->db->from('result_dnf');
$this->db->join('riders','riders.id_rider = result_dnf.id_rider');
$this->db->join('teams','teams.id_team = riders.id_team');
$this->db->join('result_podium','result_podium.id_rider = riders.id_rider', 'left');
$this->db->where('riders.is_deleted','0');
$this->db->order_by("SUM(result_podium.point) DESC");
$this->db->group_by(array('result_dnf.id_rider','result_podium.id_rider'));
and here is the result :
No
rider
point
1
A
5
2
D
5
3
B
4
4
CC
4
5
Bri
6
Brum
the point data forBri and Brum, is null because they came from result_dnf, but i did not managed to get Nick data from result_podium.
And here what i was expecting for the data to shown in my view :
No
rider
point
1
Nick
10
2
A
5
3
D
5
4
CC
4
5
B
4
5
Bri
6
Brum
is there a way to join those two table?, anyhelp is really appreciated thank you!.

How to get the all child rows recursively using CodeIgniter and MySQL?

How to get the all child rows and their child rows using CodeIgniter and MySQL?
Here is the Table structure
Id user_id sponser_id
1 1 0
2 2 1
3 3 1
4 4 1
5 5 2
6 6 2
7 7 2
8 8 3
9 8 3
10 10 4
I want to get all the child row which is connected under particular sponser_id. I have trid this query but I can't get proper detail.
$this->db->select('u_fname,u_lname,sponsor_id');
$this->db->from('user_reg');
$this->db->where('sponsor_id', $user_data['otc_id']);
$query = $this->db->get();
Here is the graphical representation of what i am doing. I want all children row of A and same as b to O.
I am calling this function from my controller to get the desired output.
Controller Code:
public function index()
{
$all_level_array = array();
$user_data = $this->common->user_data_db();
$get_user_level = $this->u_buildup_wallet_model->get_user_buildup_level($user_data['otc_id']);
if(!empty($get_user_level)){
foreach ($get_user_level as $userData) {
$test = $this->u_buildup_wallet_model->get_user_buildup_level($userData['otc_id']);
print_r($test);
}
}
print_r($all_level_array);
exit
$this->load->view();
}
Model Code:
public function get_user_buildup_level($user_otc)
{
$this->db->select('u_fname,u_lname,sponsor_id,otc_id');
$this->db->from('user_reg');
$this->db->where('sponsor_id', $user_otc);
$query = $this->db->get();
$result = $query->result_array();
return $result;

array_search result not getting properly

here i have two tables namely the rooms and student_hostel.
rooms look like this
id rm_number capacity bed_no class hostel is_vaccant
40 1 5 1A,1B,1C,1D,1E 27 7 1
41 2 4 2A,2B,2C,2D 28 7 1
42 3 3 3A,3B,3C 29 10 1
43 4 4 4A,4B,4C,4D 30 10 1
44 5 6 5A,5B,5C,5D,5E,5F 27 7 1
45 6 7 6A,6B,6C,6D,6E,6F,6G 29 10 1
student_hostel looks like this
id first_name stud_id hostel class room bed status
175 siraj WPGH00175 7 28 41 2A P
176 nesru WPGH00176 7 28 41 2B P
180 dsf WPGH00180 7 27 40 1A G
Here is the code that i tried to get all occupied rooms
the controller looks like this
public function beds_occupied($rm)
{
$data['bed'] = $this->admin_model->ajax_bed($rm)->row();
$data['beds'] = explode(',',$data['bed']->bed_no);
$data['bed_no'] = $this->admin_model->get_bed_no($rm)->result();
foreach ($data['bed_no'] as $row)
{
if (($key = array_search($row->bed, $data['beds'])) !== false)
{
unset($data['beds'][$key]);
$data['beds'] = array_values($data['beds']);
}
}
return $data['beds'];
}
the model looks like this
public function ajax_bed($val=Null)
{
if(isset($val))
{
$this->db->where('id',$val);
}
return $this->db->get('rooms');
}
public function get_bed_no($id)
{
return $this->db->get_where('student_hostel',array('room'=>$id));
}
the ouput iam getting is all vaccant beds instead of occupied,
the result iam getting is like this.
SI no Rooms Class Hostel Occupied
1 1 periyar1 periyar 1B,1C,1D,1E
2 2 periyar2 periyar 2C,2D
3 3 pamba1 pamba 3A,3B,3C
4 4 pamba 2 pamba 4A,4B,4C,4D
5 5 periyar1 periyar 5A,5B,5C,5D,5E,5F
6 6 pamba1 pamba 6A,6B,6C,6D,6E,6F,6G
but i want to get it like this
SI no Rooms Class Hostel Occupied
1 1 periyar1 periyar 1A
2 2 periyar2 periyar 2A,2B
3 3 pamba1 pamba
4 4 pamba 2 pamba
5 5 periyar1 periyar
6 6 pamba1 pamba
You are unsetting the variables that DO exist. You want to keep the variables that DO exist as they are taken beds. Try this:
public function beds_occupied($rm)
{
$occupied = array();
$data['bed'] = $this->admin_model->ajax_bed($rm)->row();
$data['beds'] = explode(',',$data['bed']->bed_no);
$data['bed_no'] = $this->admin_model->get_bed_no($rm)->result();
foreach ($data['bed_no'] as $row)
{
$key = array_search($row->bed, $data['beds'];
if ($key)
{
$occupied[] = $data['beds'][$key]);
}
}
return $occupied;
}

how to select/add a column to pandas dataframe based on a non trivial function of other columns

This is a followup question for this one: how to select/add a column to pandas dataframe based on a function of other columns?
have a data frame and I want to select the rows that match some criteria. The criteria is a function of values of other columns and some additional values.
Here is a toy example:
>> df = pd.DataFrame({'A': [1,2,3,4,5,6,7,8,9],
'B': [randint(1,9) for x in xrange(9)],
'C': [4,10,3,5,4,5,3,7,1]})
>>
A B C
0 1 6 4
1 2 8 10
2 3 8 3
3 4 4 5
4 5 2 4
5 6 1 5
6 7 1 3
7 8 2 7
8 9 8 1
I want select all rows for which some non trivial function returns true, e.g. f(a,c,L), where L is a list of lists and f returns True iff a and c are not part of the same sublist.
That is, if L = [[1,2,3],[4,2,10],[8,7,5,6,9]] I want to get:
A B C
0 1 6 4
3 4 4 5
4 5 2 4
6 7 1 3
8 9 8 1
Thanks!
Here is a VERY VERY hacky and non-elegant solution. As another disclaimer, since your question doesn't state what you want to do if a number in the column is in none of the sub lists this code doesn't handle that in any real way besides any default functionality within isin().
import pandas as pd
df = pd.DataFrame({'A': [1,2,3,4,5,6,7,8,9],
'B': [6,8,8,4,2,1,1,2,8],
'C': [4,10,3,5,4,5,3,7,1]})
L = [[1,2,3],[4,2,10],[8,7,5,6,9]]
df['passed1'] = df['A'].isin(L[0])
df['passed2'] = df['C'].isin(L[0])
df['1&2'] = (df['passed1'] ^ df['passed2'])
df['passed4'] = df['A'].isin(L[1])
df['passed5'] = df['C'].isin(L[1])
df['4&5'] = (df['passed4'] ^ df['passed5'])
df['passed7'] = df['A'].isin(L[2])
df['passed8'] = df['C'].isin(L[2])
df['7&8'] = (df['passed7'] ^ df['passed8'])
df['PASSED'] = df['1&2'] & df['4&5'] ^ df['7&8']
del df['passed1'], df['passed2'], df['1&2'], df['passed4'], df['passed5'], df['4&5'], df['passed7'], df['passed8'], df['7&8']
df = df[df['PASSED'] == True]
del df['PASSED']
With an output that looks like:
A B C
0 1 6 4
3 4 4 5
4 5 2 4
6 7 1 3
8 9 8 1
I implemented this rather quickly hence the utter and complete ugliness of this code, but I believe you can refactor it any way you would like (e.g. iterate over the original set of lists with for sub_list in L, improve variable names, come up with a better solution, etc).
Hope this helps. Oh, and did I mention this was hacky and not very good code? Because it is.