The output of the tree to a string - codeigniter

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

Related

Import CSV file, remove empty rows and export it immediately without storing it into database - laravel excel

I am trying to remove all the empty rows from a csv and make it downloadable. In this process, there is no involvement of database/model.
My flow looks like:
1) Import csv file.
2) Filter empty rows.
3) Export the data after all the empty rows are removed.
My code looks like:
Controller
public function formatCSV()
{
$path = storage_path('app/files/') . 'example.csv';
Excel::import(new FormatCSV, $path);
}
app/Imports/FormatCSV
<?php
namespace App\Imports;
use App\Exports\ExportFormattedCSV;
use App\Http\Services\AmenityService;
use Maatwebsite\Excel\Concerns\ToArray;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Excel;
class FormatCSV implements ToArray, WithChunkReading
{
private $table,$service,$model;
public function __construct()
{
$this->service = new AmenityService();
}
public function array(Array $rows)
{ $rec_arr = array();
foreach ($rows as $row)
{
$rec_arr[] = array_values($row);
}
$records_arr = $this->service->trimArray($rec_arr);
$export = new ExportFormattedCSV($records_arr);
//print_r($export);
return Excel::download($export, 'csv.csv');
}
public function chunkSize(): int
{
return 10;
}
}
trimArray function
public function trimArray($arr)
{
$final = array();
foreach($arr as $k => $v)
{
if(array_filter($v)) {
$final[] = $v;
}
}
return $final;
}
app/Exports/ExportFormattedCSV
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromArray;
class ExportFormattedCSV implements FromArray
{
protected $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function array(): array
{
return $this->data;
}
}
With this code it does nothing, shows blank at the end.
However, if I uncomment the line print_r($export)
It shows data as:
App\Exports\ExportFormattedCSV Object
(
[data:protected] => Array
(
[0] => Array
(
[0] => First Name
[1] => Last Name
[2] => Roll No
)
[1] => Array
(
[0] => Ram
[1] => Patel
[2] => 1
)
[2] => Array
(
[0] => Rajuv
[1] => Roy
[2] => 2
)
[3] => Array
(
[0] => Sunny
[1] => Deol
[2] => 5
)
[4] => Array
(
[0] => Akshya
[1] => Kumar
[2] => 6
)
[5] => Array
(
[0] => Amir Khan
[1] => 7
[2] =>
)
[6] => Array
(
[0] => Salman
[1] => Khan
[2] => 9
)
[7] => Array
(
[0] => Bobby
[1] => Deol
[2] => 10
)
)
)
The File I am testing is example.csv
First Name,Last Name, Roll No
Ram,Patel,1
Rajuv,Roy,2
,,
Sunny,Deol,5
Akshya,Kumar,6
Amir Khan,7
,,
Salman,Khan,9
Bobby,Deol,10,
Barun,Dhawan,11
,,
Virat,Kohli,13
Rohit,Sharma,14

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

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

Linking arrays according to common values

I have some array like
0: [id: '1',department: 'xyz',date: '10-10-2019',time: '12:50']
1: [id: '1',department: 'xyz',date: '11-10-2019',time: '10:30']
2: [id: '2',department: 'abc',date: '09-09-2019',time: '09:50']
3: [id: '2',department: 'abc',date: '07-07-2019',time: '03:20']
I want them to be merged according to the id and department
so the 0 and 1 array will be merged together and the output should be something like
0:[id: '1',department: 'xyz',[[date: '10-10-2019',time: '12:50'],[date: '11-10-2019',time: '10:30']]]
and the 2 and 3 array will be merged together and the output should be something like
1:[id: '2',department: 'abc',[[date: '09-09-2019',time: '09:50'],[date: '07-07-2019',time: '03:20']]]
how can I do it?
Try this..
$input = [
0 =>['id' => '1','department' => 'xyz','date' => '10-10-2019','time' => '12:50'],
1 => ['id' => '1','department' => 'xyz','date' => '11-10-2019','time' => '10:30'],
2 => ['id' => '2','department' => 'abc','date' => '09-09-2019','time' => '09:50'],
3 =>['id' => '2','department' => 'abc','date' => '07-07-2019','time' => '03:20']];
$output = [];
$id_array = [];
foreach ($input as $values)
{
$id_array [] = $values['id'];
}
$unique_id_array = array_unique($id_array);
foreach($input as $key => $in)
{
if(array_key_exists($key,$unique_id_array))
{
$output[] = [
'department' => $in['department'],
'id' => $in['id']
];
}
}
foreach($input as $in)
{
foreach($output as $key => $out)
{
if($out['id'] == $in['id'] && $out['department'] == $in['department'])
{
$output[$key]['date_time'][] = ['date' =>$in['date'],'time' => $in['time']];
}
}
}
print_r($output);die();
The output is going to be
Array
(
[0] => Array
(
[department] => xyz
[id] => 1
[date_time] => Array
(
[0] => Array
(
[date] => 10-10-2019
[time] => 12:50
)
[1] => Array
(
[date] => 11-10-2019
[time] => 10:30
)
)
)
[1] => Array
(
[department] => abc
[id] => 2
[date_time] => Array
(
[0] => Array
(
[date] => 09-09-2019
[time] => 09:50
)
[1] => Array
(
[date] => 07-07-2019
[time] => 03:20
)
)
)
)
Hello i think this will works fine,and it's easy and best way to achieve your output.
i tried many codes in my system with data given by you,just try this
i hope this will work for you as well.
<?php
$inputArr = [0 =>['id' => '1','department' => 'xyz','date' => '10-10-2019','time' => '12:50'],1 => ['id' => '1','department' => 'xyz','date' => '11-10-2019','time' => '10:30'],2 => ['id' => '2','department' => 'abc','date' => '09-09-2019','time' => '09:50'],3 =>['id' => '2','department' => 'abc','date' => '07-07-2019','time' => '03:20']];
print_r($inputArr);
$second_copy = $inputArr;
foreach ($inputArr as $key => $single)
{
$goalArr[$single['id']] = getRow($single['id'],$single['department'],$second_copy);
}
function getRow($id,$department,$second_copy)
{
$returnArr['id'] = $id;
$returnArr['department'] = $department;
foreach ($second_copy as $key => $single)
{
if($id == $single['id'] && $department == $single['department'])
{
$returnArr['date'][] = $single['date'];
$returnArr['time'][] = $single['time'];
}
}
return $returnArr;
}
echo "<br>Output is: <br>";
print_r($goalArr);
?>

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.

Resources