I have a table called positions and in that table there are 4 positions
On my model function below if my layout table has a position set then
I am trying to be able to get the positions that are not set
Currently only column_left and content_top are set.
Layouts table
Question: From the positions table how can I get the positions that are not set. It should be able to get column_right and content_bottom
public function getlayoutpositions($name) {
$this->db->select('*');
$this->db->from('layouts');
$this->db->where('name', $name);
$positions = $this->db->get();
foreach ($positions->result_array() as $layouts) {
$this->db->select('position');
$this->db->from('positions');
$this->db->where('position !=', $layouts['position']);
$query = $this->db->get();
return $query->result_array();
}
}
Returns incorrect should only show column_right and content_bottom
Array
(
[0] => Array
(
[position] => column_right
)
[1] => Array
(
[position] => content_top
)
[2] => Array
(
[position] => content_bottom
)
)
In think in below code you have done a small mistake:
foreach ($positions->result_array() as $layouts) {
$this->db->select('position');
$this->db->from('positions');
$this->db->where('position !=', $layouts['position']);
$query = $this->db->get();
return $query->result_array();
}
In above code, For loop iterates with all layout objects(rows) and gives you data in $layouts. You are checking only first $layouts object and finding it in POSITION table and returning the result.
You need to create a array for all positions retrieved from LAYOUT table.
In For loop you should fill that array, means store the value of $layouts['position'] in that array.
So after the loop you will get an array of all positions present in the layout table.
Now after the For loop write your code:
$this->db->select('position');
$this->db->from('positions');
$this->db->where_not_in('position', /* Pass your array here */ );
$query = $this->db->get();
return $query->result_array();
I think this help.
Got it working now
Controller functions
public function index() {
$positions = $this->getpositions($this->getlayoutpositions($this->router->class));
echo '<pre>';
print_r($positions);
echo '</pre>';
}
Model functions
public function getpositions($positions = array()) {
$this->db->select('*');
$this->db->from('positions');
foreach ($positions as $position) {
$this->db->where('position !=', $position['position']);
}
$query = $this->db->get();
return $query->result_array();
}
public function getlayoutpositions($name) {
$this->db->select('*');
$this->db->from('layouts');
$this->db->where('name', $name);
$query = $this->db->get();
return $query->result_array();
}
Correct Result
Array
(
[0] => Array
(
[position_id] => 2
[position] => column_right
)
[1] => Array
(
[position_id] => 4
[position] => content_bottom
)
)
Try this query - it could simplify things there:
$query = $this->db->query('SELECT * FROM positions LEFT OUTER JOIN layouts ON positions.position = layouts.position WHERE layouts.layout_id IS null');
Related
I have a dB query where I would like to groupBy() only when conditions are met without using union because of pagination.
Unfortunately groupBy() seems to only work when called on the entire query outside of the loop.
This was made for dynamic filtering from $filterArr. Depending on the array I need to select from different columns of the table.
When the $key=='pattern' I would need the distinct results from its column.
the query looks something like this
select `col_1`, `col_2`, `col_3`
from `mytable`
where (`color` LIKE ? or `pattern` LIKE ? or `style` LIKE ?)
group by `col_2` //<< i need this only for 'pattern' above and not the entire query
Heres the model:
// $filterArr example
// Array ( [color] => grey [pattern] => stripe )
$query = DB::table('mytable');
$query = $query->select(array('col_1', 'col_2', 'col_3'), DB::raw('count(*) as total'));
$query = $query->where(function($query) use ($filterArr){
$ii = 0;
foreach ($filterArr as $key => $value) {
if ($key=='color'){
$column = 'color';
}else if ($key=='style'){
$column = 'style';
}else if ($key=='pattern'){
$column = 'pattern';
$query = $query->groupBy('col_2'); // << !! does not work
}
if($ii==0){
$query = $query->where($column, 'LIKE', '%'.$value.'%');
}
else{
$query = $query->orWhere($column, 'LIKE', '%'.$value.'%');
}
$ii++;
}
});
$query = $query->orderBy('col_2', 'asc')->simplePaginate(30);
I think you can simplify your code a bit:
$query = DB::table('mytable');
$query = $query->select(array('col_1', 'col_2', 'col_3'), DB::raw('count(*) as total'));
$query = $query->where(
collect($filterArr)
->only(['color','style','pattern'])
->map(function ($value, $key) {
return [ $key, 'like', '%'.$value.'%', 'OR' ];
})->all()
)->when(array_key_exists('pattern', $filterArr), function ($query) {
return $query->groupBy('col_2');
});
$query = $query->orderBy('col_2', 'asc')->simplePaginate(30);
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.
I want to fetch the database data by descending order of posted date.Database containing a field named as posted_date. This is my code(in model) for fetch the data.
public function get_data_by_volunteer_id($fields = "*", $volunteer_id)
{
$this->db->select($fields);
$query = $this->db->get_where(self::$tbl_name, array(
"user_id" => $volunteer_id));
return( $query->result() );
}
You can try this
function get_data_by_volunteer_id($fields, $volunteer_id)
{
$query = $this->db->query("SELECT * FROM table_name WHERE user_id = '$volunteer_id' ORDER BY posted_date DESC");
$result = $query->result_array();
return $result;
}
Add following in your query
$this->order_by('posted_date', 'desc');
So your final query would be
$this->db->select($fields);
$this->db->get_where(self::$tbl_name, array(
"user_id" => $volunteer_id));
$this->order_by('posted_date', 'desc');
$query=$this->db->get();
return $query->result();
$data is an array which contain user post data for fetch record from orders table
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
$data = array(
'customer_id' => $this->input->post('custId')],
'paided' => 2
);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
try this :
public function function_name (){
$data = array (
'customer_id' => $this->input->post('custId'),
'paided' => 2
);
$this->db->select('*');
$this->db->from('ordere');
$this->db->where($data);
$query = $this->db->get();
return $query->result_array();
}
You have done all good just need to put result() if you get multiple row or row() if you get one row
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$result= $this->db->get()->result(); //added result()
print_r($result);
as simple use
$custId = $_post['custId'];
$query = $this->db->query("SELECT * FROM orders WHERE customer_id= '$custId' AND paided='2'");
$result = $query->result_array();
return $result;//result will be array
This is the plus of using framework, you don't need to write that much of code,
$where = array('customer_id' => $this->input->post('custId'),'paided'=>2)
$result = $this->db->get_where('orders', $where);
and for fetching them, use $result->row() for single record retrieval.
If you want all records, use $result->result()
Here is documentation link, if you want to learn more.
What You should need to be Correct And Why
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*'); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$this->db->get(); // this will not return data this is just return object
So Your Code Should be
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select(); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$query = $this->db->get();
$data = $query->result_array();
// or You can
$data= $this->db->get()->result_array();
here result_array() return pure array where you can also use result()
this will return array of object
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;
}