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;
Related
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!.
I'm new to programing. I have table
check_1,check_2,check_3 ..etc
------------------------------
1 1 1
0 0 1
1 0 1
And I want this output :
column_name, count_true
-----------------------
check_1 2
check_2 1
check_3 3
I've tried it with mysql using the union function, but when I try to apply it in laravel I have trouble with union. Is there a unionless query that can produce such output?
Thanks in advance
You can do this way. One query in db
$records = DB::table('your_table')->get();
$check1Count = $records->where('check_1', 1)->count();
$check2Count = $records->where('check_2', 1)->count();
$check3Count = $records->where('check_3', 1)->count();
......
Or
$records = DB::table('your_table')->get();
$columns = ['check_1', 'check_2', 'check_3', ...];
$data = [];
foreach($columns as $column) {
$data[] = [
'column_name' => $column,
'count_true' => $records->where($column, 1)->count();
];
}
Also you can do this way but it is many query
$check1Count = DB::table('your_table')
->selectRaw('COUNT(check_1) as count')
->where('check_1', 1)
->first()
->count;
$check2Count = DB::table('your_table')
->selectRaw('COUNT(check_2) as count')
->where('check_2', 1)
->first()
->count;
.....
A normalised approach might look like this:
response_id checkbox
1 1
1 2
1 3
2 3
3 1
3 3
You don't need union, just query all the data and process it on Laravel. Let say you query all the data using eloquent to $data variable, then you should do it like this:
//preparing a variable to hold all 24 database field value
$total = [];
foreach ($data as $d) {
//do this for all 24 database field
if ($d->check_1) {
$total[1]++;
}
if ($d->check_2) {
$total[2]++;
}
...
}
By using that way you can't check the resutl on $total[index] variable. And yes, there is a better way to store your data instead of saving all the field for each user. You can just store all checked value in database that look like this :
user_id checkbox_id
1 3
1 5
1 9
1 24
2 23
3 2
3 3
It more efficient since you don't need to save the unchecked checkbox value, if they more likely not to checked most of the checkbox.
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);
I have 2 tables structure as following:
category(id,cat_name , parent_id);
product(id,category_id ,pro_name);
Relation in Product Model
public function getCategory()
{
return $this->hasOne(Category::className(), ['id' => 'category_id']);
}
Category
id cat_name parent_id
1 Electronic 0
2 Fruit 0
3 TV 1
4 Apple 2
Product
id category_id pro_name
1 1 Samsung
2 3 Sony
3 3 Panasonic
4 2 Apple
5 2 Orange
What I want to do is when I select on category (1) Electronic
I want to get
Samsung,Sony,Panasonic from table Product
So, you must have a relational function in your Category model calling more Categories. And another relational function calling the Products. I'm assuming the names are getParentId() and getProducts(). (If you dont have, you can use Gii to generate them for you)
Here is a recursive approach you can do inside your Category model:
public function getSubCategories()
{
if ($categories = $this->parentId) { // the name of your relational function.
foreach ($this->parentId as $sub) {
$categories = array_merge($categories, $sub->subCategories);
}
}
return $categories;
}
public function getProductsWithSubcategories()
{
$products = $this->products; // the name of your relational function.
foreach ($this->subCategories as $sub) {
$products = array_merge($products, $sub->products);
}
return $products;
}
// given $id is your current toplevel category
$cat_ids = Category::find()->select('id')->where(['parent_id' => $id])->asArray()->all();
$ids = [];
foreach($cat_ids as $value)
{
$ids[] = $value['id'];
}
$ids = implode(',',$ids);
$categories = Products::find()->where('category_id IN ('.$ids.','.$id.')')->all();
Some object to array functionallity there that you might be able to clean up. This was quick and dirty, but should work. You get the point.
table name : mytable
id name salt value
1 a 10 39
2 a 20 13
3 a 10 14
4 b 40 39
mymodel.php
function get_value_by_name($name)
{
$this->db->select("*");
$this->db->from("mytable");
$this->db->where('salt', '10');
$this->db->where('name', $name);
$this->db->order_by('id',"ASC");
$this->db->limit(1);
$query=$this->db->get();
if($query != null){
return $query->result();
}
else{
return array();
}
}
I want to get latest row with name= a and salt=10 (row with id = 3). But my current code return 1st row.
Change
$this->db->order_by('id',"ASC");
By
$this->db->order_by('id',"DESC");