I need To All Data In $qryy It Return Only Last Record - codeigniter

How I Return all data of $qryy it's return me only last record. How I need return all off data.
public function get_subcategory(){
$qry = $this->db->get("shopping_category");
foreach($qry->result() as $row){
$this->db->where("c_id",$row->id);
$qryy = $this->db->get("shopping_subcategory");
}
return $qryy;
}

You can try this
public function get_subcategory() {
$result = [];
$qry = $this->db->get("shopping_category");
foreach ($qry->result() as $row) {
$this->db->where("c_id", $row->id);
$qryy = $this->db->get("shopping_subcategory");
$result[] = $qryy;
}
return $result;
}

The reason you get the last record is, every time you loop data through foreach it keeps replacing $qryy = $this->db->get("shopping_subcategory");
to fix this you can simply change $qryy into an array $qryy[] like this.
To improve your query. you can simply try
$qryy = [];
$this->db->select("shopping_subcategory.*");
$this->db->from("shopping_category");
$this->db->join('shopping_subcategory', 'shopping_category.id = shopping_subcategory.c_id');
$sql= $this->db->get();
$qryy = $sql->result_array();
$data['category'] = $qryy;
$this->load->view('view_name', $data);
In view
$category will show your data

I think it solve your problem. would you mind if you give it a try?
since you want from table shopping_category is only id i try to fetch it so it's not a heavy duty on your server.
$result = [];
$qry = $this->db->select("id")->get('shopping_category');
foreach ($qry->result() as $row) {
$this->db->where("c_id", $row->id);
$qryy = $this->db->get("shopping_subcategory")->result();
$result[] = $qryy;
}
return $result;
Hope that helps :)

you should use another approach to resolve your problem: use joins to query the database only once:
$this->db->join("shopping_subcategory t2","t2.c_id=t1.id");
$qry = $this->db->get("shopping_category t1");
return $qry->result();
if you output above with print_r($qry->result()), you get similar to below:
Array
(
[0] => stdClass Object
(
[ID] => 1
[company_ID] => 1
[name] => aaa
)
[1] => stdClass Object
(
[ID] => 2
[company_ID] => 1
[name] => bbb
)
[2] => stdClass Object
(
[ID] => 4
[company_ID] => 2
[name] => ccc
)
)
to resolve your approach: you need to make $qryy an array, to store each subquery data, right now you are overwriting the variable $qryy with each loop and therefore only get the last result:
$qry = $this->db->get("shopping_category");
$qryy=array();
foreach($qry->result() as $i=>$row){
$this->db->where("c_id",$row->id);
$qryy[$i] = $this->db->get("shopping_subcategory")->result();
}
return $qryy;
if you output above with print_r($qryy), you get similar to below:
Array
(
[0] => Array
(
[0] => stdClass Object
(
[ID] => 1
[company_ID] => 1
[name] => aaa
)
[1] => stdClass Object
(
[ID] => 2
[company_ID] => 1
[name] => bbb
)
)
[1] => Array
(
[0] => stdClass Object
(
[ID] => 4
[company_ID] => 2
[name] => ccc
)
)
)
therefore, depending which approach you use, you'll need to take care of your data output differently.
helpful info on joins here

Related

How to create a single array using two iterating loop and than update_batch

How do I take id on every iteration from check_seeds array and add on each itteration into seeded[] array.
In more simple words, I want to take an item from the first iteration and add into the first iteration, take an item from the second iteration and add into the second iteration and so on...
Actually, on update_batch we need third parameter (primary key, index) to update array values in database rows where id from database rows matches with the id in update_batch.
$check_seeds = $this->tournament_model->get_seeds($tournament_id);
$seeds = $this->input->post('seed');
foreach ($seeds as $key => $value){
if(!empty($key) && !empty($value)){
$seeded[] = array(
'id' => (Add id here),
'tournament_id' => $tournament_id,
'stage_id' => $stage_id,
'seed_id' => $value,
'team_name' => $key,
);
$this->db->update_batch('tournament_seed', $seeded, 'id');
redirect('organizer/tournaments);
}
}
print_r($check_seeds)
Array
(
[0] => Array
(
[id] => 3
[tournament_id] => 713161746
[stage_id] => 3
[seed_id] => 3
[team_name] => -V-ATTAX
)
[1] => Array
(
[id] => 4
[tournament_id] => 713161746
[stage_id] => 3
[seed_id] => 3
[team_name] => -V-ATTAX
)
[2] => Array
(
[id] => 5
[tournament_id] => 713161746
[stage_id] => 3
[seed_id] => 3
[team_name] => -V-ATTAX
)
)
in your model function get_seeds() you can query the current max value of id as an alias and return it together with the query result:
function get_seeds($tournament_id) {
$this->db->select_max('id', 'max_id');
$this->db->where('tournament_id', $tournament_id);
$result = $this->db->get('tournament_seed');
return $result->result();
}
then in your controller's for_each() you increment that value:
$i=0;
foreach ($seeds as $key => $value){
$i++;
$seeded[] = array(
'id' => $value->max_id + $i,
//....
);
}
Codeigniter docs: Selecting Data, scroll down to select_max(), as there is no internal bookmark

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;
}

codeignite trying to get property of non-object not resolved

I am attempting to access the result set from a model query in the view. I have the following:
Controller:
$courseId = $this->session->userdata('courseId');
//echo "Course: ".$courseId;
if(isset($courseId) && $courseId != '')
{
$result = $this->Course_model->loadBasicDetailsEdit($courseId);
$data['basicCourseDetails'] = $result;
$this->load->view('course/basicDetails', $data);
}
Model:
function loadBasicDetailsEdit($courseId)
{
$this->db->select('*');
$this->db->where('course_id', $courseId);
$this->db->from('course');
$query = $this->db->get();
if ( $query->num_rows() > 0 )
{
return $query->result();
} else {
return FALSE;
}
}
and in the view I tried to print_r() and got this:
Array ( [0] => stdClass Object ( [course_id] => 8 [title] => Photography [summary] => [description] => [price] => [member_id] => 12 [category] => [audience] => [goals] => [date] => 2013-09-26 [production] => 0 ) )
I tried to access this using $basicCourseDetails->title or $basicCourseDetails['title']
but neither are working. Any hint as to why this is happening?
Regards,
try this:
foreach($basicCourseDetails as $basic){
echo($basic->title);
}
or something like this:
echo($basicCourseDetails[0]->title);
This is an array of objects
Array ( [0] => stdClass Object ( [course_id] => 8 [title] => Photography [summary] => [description] => [price] => [member_id] => 12 [category] => [audience] => [goals] => [date] => 2013-09-26 [production] => 0 ) )
Contains one stdObject in the array, so, first objects is 0, if there were more, then second item could have index 1 and so on. To retrieve data from the first (here is only one) stdobject you may use
echo $basicCourseDetails[0]->title; // title will be printed
You can send data to the view page by this line of code which is mentioned in above question.
$result = $this->Course_model->loadBasicDetailsEdit($courseId);
$data['basicCourseDetails'] = $result;
$this->load->view('course/basicDetails', $data);
But when you will access these data in view then you need to access all data one by one by using foreachloop in the view page.
For example if you have a view page like basic_details.php inside course folder then you need to write code like this to access these data.
foreach ($basicCourseDetails as $key => $value) {
$name = $value->name;
}
The above foreachloop can be written in view page where you want to access data.

Best way to make selects in laravel?

In order to use the Form::select, you need to add a name and two arrays.
In a normal case, we have one with the id and another one with the name. Both needs to be loaded from a query.
So in the model I have something like this:
public static function get_ids()
{
//return DB::query('select id FROM __roles');
return DB::table('roles')->get(array('id'));
}
//Returns an array with all the names
public static function get_names()
{
//return DB::query('select name FROM __roles');
return DB::table('roles')->get(array('name'));
}
However, this gives me this:
Array ( [0] => stdClass Object ( [id] => 1 ) [1] => stdClass Object ( [id] => 2 ) [2] => stdClass Object ( [id] => 3 ) )
I would like to get something like this:
Array ( [0] => '1' [id] => '2' [id] => '3' )
For the form
Form::select('role', array(Role::get_ids(), Role::get_names()));
Does this work?
public static function get_ids()
{
//return DB::query('select id FROM __roles');
return DB::table('roles')->get(array('id'))->to_array();
}
//Returns an array with all the names
public static function get_names()
{
//return DB::query('select name FROM __roles');
return DB::table('roles')->get(array('name'))->to_array();
}
I tried this option:
$projects = Project::where('user_id', Auth::user()->id)->lists('name', 'id');
In template:
{{ Form::select('project', $projects) }}

The output of the tree to a string

My table [cat_id,title,pid]. I need to get all the child sub-categories ID`s in the following format:
[1] =>Array ( [cat_id] => 2 [title] => TEST [pid] => 1 ),
[2] =>Array ( [cat_id] => 3 [title] => TEST [pid] => 1 ),
[3] =>Array ( [cat_id] => 4 [title] => TEST [pid] => 2 ),
[4] =>Array ( [cat_id] => 5 [title] => TEST [pid] => 3 )
Purpose - using ID`s of the childs, to get all of these items.
I tried to do something as the following code, but it does not work:
public function get_ids($tree,$cid = 0)
{
$data = $this->db->select('cat_id, title, pid')
->where('pid',$cid)
->get('catalog');
$result = array_push($tree,$data->result_array());
if($data->num_rows() > 0){
foreach ($data->result_array() as $r) {
return $this->get_ids($result,$r['cat_id']);
}
}
else{
return $result;
}
}
Maybe there is better way?
Try the below code to get all child ids by given parent id.
function fnGetChildCatelogs($id) {
$qry = "SELECT cat_id FROM catalog WHERE pid = $id";
$rs = mysql_query($qry);
$arrResult = array();
if(mysql_num_rows($rs) > 0) {
# It has children, let's get them.
while($row = mysql_fetch_array($rs)) {
# Add the child to the list of children, and get its subchildren
$arrResult[$row['cat_id']] = fnGetChildCatelogs($row['cat_id']);
}
}
return $arrResult;
}

Categories

Resources