Showing the lowest pid / parent id number first - codeigniter

I have this function below which get the pid numbers from my database. The pid is parent id.
public function test() {
$arr = array();
foreach ($this->make_parent_list() as $result) {
$arr[] = $result;
}
print_r(implode(',', $arr));
}
The out put is as follows 4, 1
But I need it to print the lowest number first. 1, 4
Question How can I make sure when i view the parent id / pid that it
will show the lowest number first I have tried
$this->db->order_by('pid', 'desc'); and $this->db->order_by('pid',
'asc');
public function make_parent_list() {
$this->db->where('fid', '5');
$query = $this->db->get('forum');
$return = array();
foreach ($query->result() as $category)
{
$this->db->where('fid', $category->pid);
$this->db->order_by('pid', 'desc');
$query = $this->db->get('forum');
$return[$category->pid] = $category->pid;
foreach ($query->result() as $category)
{
$return[$category->pid] = $category->pid;
}
}
return $return;
}

Solved
I had to created a $results variable and then wrap in sort() outside foreach loop
$arr = array();
$results = $this->make_parent_list();
sort($results);
foreach ($results as $result) {
$arr[] = $result;
}
echo implode(',', $arr);
Now out put 1,4

I think you got a bit lost... The order_by asc,desc does work. Your function has order_by desc and an unnecessary foreach etc.
Assumptions: The forum table has at least the two columns pid and fid. I created a table with
fid , pid
5 1
5 4
If you simplify your function to...
// Given the fid, return an array of pid values in ascending order
public function make_parent_list() {
$this->db->where('fid', '5'); // Hardcoded for testing.
$this->db->order_by('pid', 'asc');
$query = $this->db->get('forum');
$pid_list = array();
foreach ($query->result() as $category) {
$pid_list[] = $category->pid;
}
return $pid_list;
}
Then you only need
$results = $this->make_parent_list();
echo implode(',', $results);
That will give you a result of 1,4.
If you change asc to desc in the order_by in the function then you will get 4,1.

Related

Refactor Query Builder Laravel

I want to do repetitive get data and foreach on several tables (see example below). Is there a way to write the code in a cleaner way instead of repeating the same code for all the tables?
$xs = DB::table('table1')->where('text', 'like', '%string')->get();
foreach ($xs as $x) {
..
}
$ys = DB::table('table2')->where('text', 'like', '%string')->get();
foreach ($ys as $y) {
..
}```
My approach is using array and foreach
$tables = ['table1', 'table2'];
results = [];
foreach($tables as $table) {
$data = DB::table($table)->where('text', 'like', '%string')->get();
foreach($data as $d) {
// your logic here
}
$results[] = ; // return a value from each query to array
}
You can write a base function and pass tableName to it and execute certain action
public function getData($tableName) {
$query = DB::table($tableName)->where('text', 'like', '%string')->get();
foreach ($query as $row) {
...
}
// return result;
}
$tables = ['table1', 'table2', 'table3'];
$queryResponse = [];
foreach($tables as $tableName) {
$queryResponse[$tableName] = $this->getData($tableName);
}

Persisting query data through pagination pages

I'm trying to filter homes by Price Ascending, Price Descending, Most Recent, A-Z, Z-A and I've have success with exception to the fact that the queries to not persist after the first page. Can anyone please show me a way to do it?
public function index(Request $request)
{
$query = $request->input('queryOptions');
if($query == 'Price Ascending'){
$properties = Property::orderBy('price', 'asc');
}
if($query == 'Price Descending'){
$properties = Property::orderBy('price', 'desc');
}
if($query == 'Most Recent'){
$properties = Property::latest();
}
if($query == 'A-Z'){
$properties = Property::orderBy('city', 'asc');
}
if($query == 'Most Recent'){
$properties = Property::latest();
}
$properties = $properties->paginate(10);
return view('property.index', compact('properties', 'query'));
}
You can use the appends function within your declaration in your view - and use it against your $query as declared in your controller
{{ $properties->appends(['queryOptions' => $query ])->links() }}
https://laravel.com/docs/master/pagination

Nested loop caused slow in performance

foreach ($id_prs as $ids) {
list($id_pr, $id_cart) = explode('-', $ids);
foreach ($id_cart as $id) {
$r = Cart::find($id);
/// column to update value
$r->save();
}
}
This is how my loop looks like.
E.g. $id_prs consist of 10 data, while each id_prs may consist of 20 data etc.
From there, i found will take longer time to loop when a lots of data from each $id_cart.
How can i solve the performance issue, is there any solution?
This is known as n+1 problem, every time you iterate inside foreach additional query is created.
So this line is the evil, because it's querying data every iteration.
$r = Cart::find($id);
You can make it better like this:
$cart = Cart::all();
foreach ($id_prs as $ids) {
list($id_pr, $id_cart) = explode('-', $ids);
foreach ($id_cart as $id) {
// Get it from collection rather than query it from database
$cart->where('id', $id)->first()->save();
}
}
Where you acctualy query all carts inside variable as a collection, and you just manipulating with that collection localy without touching database (only when you hit save method).
Your code and variables are not clear enough. But a better solution is:
foreach ($id_prs as $ids) {
list($id_pr, $id_cart) = explode('-', $ids);
$items = Cart::whereIn('id', $id_cart) -> get();
foreach($items as $item) {
Cart::where('id', $item -> id) -> limit(1) -> update([
// Columns to update
]);
}
}
If you want to update same value for all the records
$allIds = [];
foreach ($id_prs as $ids) {
list($id_pr, $id_cart) = explode('-', $ids);
foreach ($id_cart as $id) {
$allIds[] = $id;
}
}
$cart = Cart::whereIn('id',$allIds)->update(['columns'=>'value']);

Laravel foreach "where" with eloquent

I have certain fields and data values which cannot be hardcoded into the query. I'm trying to get something like this:
return Listing::where('id', $id)
->where(function($query) use ($input) {
->where('field_1', 'foo_1')
->where('field_2', 'foo_2')
->where('field_3', 'foo_3')
}
->get();
**Here's what I have **
return Listing::where('id', $id)
->where(function($query) use ($input) {
$i = 0;
foreach ($input as $key => $value) {
$i++;
// ->where('field_1', red_1); // Desired output
->where("where(field_{$i},".$value."_1)");
// $query = $query."where(field_{$i},".$value."_1)"."<br>";
// return $query prints out the following
/*
field_1 red_1,
field_2 foo_1,
field_3 bar_3
*/
}
})
->get();
Something like this should work:
$listing = Listing::where('id', $id);
foreach ($input as $key => $value) {
$i++;
// ->where('field_1', red_1); // Desired output
$listing->where("where(field_{$i},".$value."_1)");
}
$results = $listing->get();
$query = Listing::where('id', $id);
$i = 0;
foreach ($input as $key => $value) {
$i++;
$query->where('field_'.$i,$value.'_'.$i);
}
return $query->get();
One you're not chaining correctly and two you are mis-using the querybuilder closure. If you want to execute logic like a loop then you have to break down the query. Furthermore using a where closure is like writing a parenthesis around your where conditions.
Something like:
$query->where('bacon', $foo)
$query->where(function ($query) use ($bar, $baz){
$query->where('apple', $bar);
$query->orWhere('orange', $baz)
});
Would roughly translate to:
WHERE bacon = $foo AND (apple = $bar OR orange = $baz)

codeigniter LEFT JOIN array issue

Can someone tell me how to write this properly?
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$records = $this->db->where('us.group_id', '3');
$data=array();
foreach($records->result() as $row)
{
$data[$row->id] = $row->first_name;
}
return ($data);
}
I'm trying to populate a drop down menu using an array, but i need to only grab users that are part of users_group/group_id = 3
therefore in my very limited knowledge I'm needing:
select X from Users LEFT JOIN users_groups WHERE group_ID = 3
You need to call $this->db->get() in order to actually run your query.
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$this->db->where('us.group_id', '3');
$records = $this->db->get();
$data = array();
foreach($records->result() as $row){
$data[$row->id] = $row->first_name;
}
return $data;
}

Resources