I want to build a query nested in codeigniter
here is the my controller
public function advancedSearch(){
if (!$this->session->userdata('logged_in')) {
redirect('main/login', 'refresh');
}
$session_data = $this->session->userdata('logged_in');
$data['code'] = $session_data['code'];
$start = $this->input->post('start');
$end =$this->input->post('end');
$ticket = $this->input->post('ticket');
$type =$this->input->post('type');
$this->db->select('*');
$this->db->from('table');
if ($start && $end) {
$this->db->where('date >=',$start);
$this->db->where('date <=',$end);
} else if ($ticket) {
$this->db->where('ticket','yes');
} else if ($type) {
$this->db->where('type',$type);
}
$query = $this->db->get();
if ($query->num_rows() > 0) {
$data['data'] = $query->result();
}
//print_r($data);
$this->load->view('advanced-search',$data);
with this code I just get the first input a click and search, If I click just ticket I get the $this->db->where('ticket','yes'); I hope I explained well.
Greetings
Hope this will help you :
instead of using if else use it with only if with empty check
if ( ! empty($start))
{
$this->db->where('date >=',$start);
}
if (! empty($end))
{
$this->db->where('date <=',$end);
}
if (! empty($ticket))
{
$this->db->where('ticket','yes');
}
if (! empty($type))
{
$this->db->where('type',$type);
}
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$data['data'] = $query->result();
}
//print_r($data);
$this->load->view('advanced-search',$data);
Related
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 5 years ago.
I have an array like below,
[{
"name":"Daniel",
"connection_status":"1"
},
{
"name":"Danny",
"connection_status":"3"
},
{
"name":"Moris",
"connection_status":"2"
},
{
"name":"Manny",
"connection_status":"1"
}]
I want to sort my array by status like 1,2,3 in this order.
This is my code,
public function getProfileDataForMySociety($user_id)
{
$this->db->select('*');
$this->db->from('profile');
$this->db->where('profile_id!=', $user_id);
$query = $this->db->get();
$list = $query->result();
$friends = $this->checkFriends($list, $user_id);
return $friends;
}
public function checkFriends($list, $user_id)
{
$array = [];
foreach ($list as $k => $v) {
// print_r(json_encode($list));
$friends = $this->checkStatus($v->profile_id);
//print_r($friends);
$relationship = '';
$relation_id = '';
foreach ($friends as $kk => $vv) {
if ($user_id == $vv->sent_id) {
if ($vv->status == 1) {
$relationship = 1;
}
if ($vv->status == 2) {
$relationship = 2;
}
} else if ($user_id == $vv->recieved_id) {
// pending
if ($vv->status == 1) {
$relationship = 3;
$relation_id = $vv->sent_id;
}
if ($vv->status == 2) {
$relationship = 4;
}
}
}
$list[$k]->connection_status = $relationship;
$list[$k]->relation_id = $relation_id;
}
return $list;
}
public function checkStatus($id)
{
$this->db->select('*');
$this->db->from('requests');
$this->db->where('sent_id', $id);
$this->db->or_where('recieved_id', $id);
$query = $this->db->get();
$list = $query->result();
return $list;
}
connection status is not a db field.
Where $list is my o/p array.Can anyone help me.Thanks in advance.
I want to sort my array based on my connection_status.
if i understand you correctly this may help you
public function getProfileDataForMySociety($user_id)
{
$this->db->select('*');
$this->db->from('profile');
$this->db->where('profile_id!=', $user_id);
$this->db->order_by('status', 'asc');
$query = $this->db->get();
$list = $query->result();
return $list;
}
You can apply order by in query
public function getProfileDataForMySociety($user_id)
{
$this->db->select('*');
$this->db->from('profile');
$this->db->where('profile_id!=', $user_id);
$this->db->order_by('status'); // USE ORDER BY
$query = $this->db->get();
$list = $query->result();
return $list;
}
public function getProfileDataForMySociety($user_id)
{
$this->db->select('*');
$this->db->from('profile');
$this->db->where('profile_id!=', $user_id);
$query = $this->db->order('status asc')->get();
$list = $query->result();
return $list;
}
Hi I need hel I'm trying to get pagination with search results.
I don't know what I am doing wrong. Because my results don't show up.
Here is my Model
function search_posts($query, $pagina, $por_pagina)
{
$this->db->select();
$this->db->from('posts');
$this->db->like("post_title", $query, 'both');
$this->db->or_like("post", $query, 'both');
$this->db->order_by('post_id', 'desc');
$this->db->limit($pagina, $por_pagina);
$query = $this->db->get();
return $query->result_array();
}
public function contar_filas()
{
$this->db->select();
$this->db->from('posts');
$query = $this->db->count_all_results();
return $query;
}
Here is my Controller
public function buscar()
{
$this->load->model('M_Articulos');
$data['posts'] = '';
$data['sin_resultados'] = '';
$query = trim($this->input->get('query', TRUE));
if (sizeof($query) == 0)
{
show_404();
}
if ($query)
{
if ($this->uri->segment(3))
{
$pagina = $this->uri_segment(3);
}
else
{
$pagina = 0;
}
$por_pagina = 4;
$data['posts'] = $this->M_Articulos->search_posts($query, $pagina, $por_pagina);
if ($data['posts'] != FALSE)
{
$data['posts'] = $this->M_Articulos->search_posts($query, $pagina, $por_pagina);
$config['base_url'] = base_url() . 'admin/buscar?query=' . $query;
$config['per_page'] = $por_pagina;
$config['total_rows'] = $this->M_Articulos->contar_filas();
$config['uri_segment'] = 3;
$config['enable_query_strings'] = TRUE;
$config['query_string_segment'] = 'page';
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
}
else
{
$data['sin_resultados'] = 'nada';
}
}
else
{
$data['sin_resultados'] = 'nada';
}
$this->load->view('admin/layouts/header');
$this->load->view('admin/modules/buscar', $data);
$this->load->view('admin/layouts/footer');
}
does anyone know how to make it work?
I'm going to be more specific. What i want is that when i search somethin in search page there is a pagination. Thanks
I want to insert of replace if exist in database with codeigniter
this my model
public function upload($id_akun,$data){
$query = $this->db->query("SELECT * FROM akun WHERE
id_akun = '{$data['id_akun']}' ");
$result = $query->result_array();
$count = count($result);
if (empty($count)) {
$this->db->insert('foto', $data);
}
elseif ($count == 1) {
$this->db->where('id_akun', $data['id_akun']);
$this->db->update('foto', $data);
}
I'm succesful replace(update) data if exist but it not inserting data if (empty($count))
conditional ($count == 1)---> it's ok
condtional (empty($count))--->problem?
Try this
In Model
public function upload($id_akun,$table){
$query = $this->db->query("SELECT * FROM akun WHERE id_akun = '$id_akun' ");
$result = $query->result_array();
$count = count($result);
if (empty($count)){
$this->db->insert('foto', $table);
return 0;
}
elseif ($count == 1) {
$this->db->where('id_akun', $id_akun);
$this->db->update('foto', $table);
return 1;
}
elseif (($count >= 1) {
return 2;
}
}
In Controller
$id_akun = 25;
$table = 'table_name';
$result = $this->model_name->upload($id_akun,$table);
switch ($result) {
case 0:
$output = 'Data Inserted'
break;
case 1:
$output = 'Record Inserted'
break;
case 2:
$output = 'Multiple Record Found'
break;
}
You can try like this ..
public function upload($id_akun,$data){
$this->db->where('id_akun',$data['id_akun']);
$this->db->from('akun');
$query = $this->db->get();
$rowcount = $query->num_rows();
if ($rowcount==0) {
$this->db->insert('foto', $data);
}
else {
$this->db->where('id_akun', $data['id_akun']);
$this->db->update('foto', $data);
}
Your problem is you're checking to see if the result of a count() is empty. That will always evaluate to false.
public function upload($id_akun, $data)
{
//use active record methods, otherwise, you need to sanitize input
$this->db->select('*');
$this->db->where('id_akun', $data['id_akun']);
$query = $this->db->get('akun');
if($query->num_rows() == 0){
$this->db->update...
}else{
....
}
}
Explanation
The function result_array() results empty array if nothing is find in fetching record. Hence when you fetch record it returns you the empty array and you are taking count of this empty array which returns you the zero length which is not equals to empty hence your condition is always remain false.
You may use the following Code.
Code
function upload($id_akun, $data) {
//use active record methods, otherwise, you need to sanitize input
$this->db->select('*');
$this->db->where('id_akun', $data['id_akun']);
$query = $this->db->get('akun');
if ($query->num_rows() == 0) {
//write your Insert query here
} elseif ($query->num_rows() > 0) {
//write your Update query here
}
}
There are several benefits to using Active Record (or Query Builder as its going to be called soon):
Security
Code-Cleanliness
Database-Agnostic
so, all time try to use active record.
<?php
public function upload($id_akun,$data){
$query = $this->db->select('*')->where('id_akun', $data['id_akun'])->from('akun')->get();
$count = $query->num_rows();
$result = $query->result_array();
if ($count == 0)
return $this->db->insert('foto', $data);
}
elseif ($count > 1) {
$this->db->where('id_akun', $data['id_akun']);
return $this->db->update('foto', $data);
}
}
Controller[In Article Page Article Properly work with pagination, store user email id in 'articles' database , now i tried to get the user firstname, and lastname from users table but not work properly ]
public function articles()
{
$data['title'] = "Articles";
$config = array();
$config["base_url"] = base_url() . "sd/articles/";
$config["total_rows"] = $this->model_users->record_count_articles();
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->model_users->fetch_result_articles($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
if ($this->session->userdata ('is_logged_in')){
$data['profile']=$this->model_users->profilefetch();
$this->load->view('sd/header',$data);
$this->load->view('sd/articles', $data);
$this->load->view('sd/footer', $data);
} else {
$this->load->view('sd/sdheader', $data);
$this->load->view('sd/articles', $data);
$this->load->view('sd/sdfooter', $data);
}
}
Model [ Get Users Name in Article Page ]
public function record_count_articles() {
return $this->db->where('status','1')->count_all("articles");
}
public function fetch_result_articles($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->where('status','1')->order_by('id', 'DESC')->get("articles");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Add These Lines [ But Not Work]
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
$query = $this->db->select('firstname')->select('lastname')->where('email',$data[0]->email)->get("users");
$data['name_info']=$query->result_array();
}
return $data;
}
return false;
You have 2 problem here. please have a look on comments in code.
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
//1) $data[0]->email keep repeating same email.
// inner $query variable should be different.
$innerQuery = $this->db->select('firstname,lastname')->where('email',$row->email)->get("users");
//2) you need to store query result on array.
// $data['name_info'] stores only single record.
$data[]=$innerQuery ->result_array();
}
return $data;
}
return false;
You should avoid query in loop if you can achieve it by join
EDIT: Lets try this with join
public function fetch_result_articles($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db
->join('users u','u.email = a.email','left')
->where('a.status','1')->order_by('a.id', 'DESC')->get("articles a");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
I have not tested the code. but it is better way than loop.
Hello guys i am trying to create a blog. The first home page is ok.. i am getting the shrot description and the categories from the database but...i have problems with the links:
this are my controller functions:
public function index()
{
$this->load->model('Model_cats');
$data['posts'] = $this->Model_cats->getLivePosts(10);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = 'Welcome to Paul Harbuz Blog Spot!';
$data['main'] = 'public_home';
$this->load->vars($data);
$this->load->view('template', $data);
}
public function category($id)
{
$data['category'] = $this->Model_cats->getCategory($id);
$data['posts'] = $this->Model_cats->getAllPostsByCategory($id);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = $data['category']['name'];
$data['main'] = 'public_home';
$this->load->vars($data);
$this->load->view('template', $data);
}
public function post($id)
{
$data['post'] = $this->Model_cats->getPost($id);
$data['comments'] = $this->Model_cats->getComments($id);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = $data['post']['title'];
$data['main'] = 'public_post';
$this->load->vars($data);
$this->load->view('template');
}
this are my model function:
function getTopCategories()
{
$this->db->where('parentid',0);
$query = $this->db->get('categories');
$data = array();
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[$row['id']] = $row['name'];
}
}
$query->free_result();
return $data;
}
function getLivePosts($limit)
{
$data = array();
$this->db->limit($limit);
$this->db->where('status', 'published');
$this->db->order_by('pubdate', 'desc');
$query = $this->db->get('posts');
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
function getCategory($id)
{
$data = array();
$this->db->where('id',$id);
$this->db->limit(1);
$query = $this->db->get('categories');
if($query->num_rows() > 0)
{
$data = $query->row_array();
}
$query->free_result();
return $data;
}
function getAllPostsByCategory($catid)
{
$data = array();
$this->db->where('category_id', $catid);
$this->db->where('status', 'published');
$query = $this->db->get('posts');
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
function getPost($id)
{
$data = array();
$this->db->where('id',$id);
$this->db->limit(1);
$query = $this->db->get('posts');
if ($query->num_rows() > 0)
{
$data = $query->row_array();
}
$query->free_result();
return $data;
}
and in the view page i have something like this:
if ( count($posts) )
{
foreach ($posts as $key => $list)
{
echo '<h2>'.$list['title'].'</h2>';
echo auto_typography(word_limiter($list['body'], 200));
echo anchor('post/'.$list['id'],'read more >>');
}
echo '<br/><br/>';
}
I'm getting the post id in the url but.. i don't know why the page is not found.
You have to add the controller name to the anchor uri segments.
echo anchor('CONTROLLER/post/'.$list['id'],'read more >>');
More on this topic in the CodeIgniter URLs documentation.
If you want a URL like http://example.com/post/123 then you have to add the following to your application/config/routes.php file:
$route['post/(:num)'] = "CONTROLLER/post/$1";
More on routing is also available in the documentation.