How do I join tables for a search query in Codeiginter - php

Hi I have the following function that search uses to find information:
function get_search(){
$property_location = $this->input->post('property_location');
$property_bedroom = $this->input->post('property_bedroom');
$property_bathroom = $this->input->post('property_bathroom');
$property_status = $this->input->post('property_status');
$property_type = $this->input->post('property_type');
$this->db->like('property_location',$property_location);
$this->db->like('property_beds',$property_bedroom);
$this->db->like('property_bath',$property_bathroom);
$this->db->like('property_state',$property_status);
$this->db->like('property_type',$property_type);
$query = $this->db->get('property');
error_log($this->db->last_query());
return $query->result();
}
How would I join another table say property_images to the search so that data is included in the result?
Here is my view:
<div class="container">
<div class="page-header">
<h4>Your search returned the following result(s):</h4>
</div>
<?php foreach ($results as $item): ?>
<div class="row">
<div class="col-md-3">
<div class="thumbnail">
<img
src="<?php echo base_url() . 'data/images/property_images/' . $item->image_name; ?>"
class="img-responsive">
</div>
</div>
<div class="col-md-9">
<h4 style="margin-top: 0;">
<?php echo $item->property_name; ?>
</h4>
<p><?php echo $item->property_description;?></p>
<p>Location: <?php echo $item->property_location ?> | Price: R<?php echo $item->property_price ?></p>
</div>
</div>
<hr>
<?php endforeach; ?>
</div>

You can use join in your query:
function get_search(){
$property_location = $this->input->post('property_location');
$property_bedroom = $this->input->post('property_bedroom');
$property_bathroom = $this->input->post('property_bathroom');
$property_status = $this->input->post('property_status');
$property_type = $this->input->post('property_type');
$this->db->like('property_location',$property_location);
$this->db->like('property_beds',$property_bedroom);
$this->db->like('property_bath',$property_bathroom);
$this->db->like('property_state',$property_status);
$this->db->like('property_type',$property_type);
$this->db->join('property_images','property.property_image_id=property_images.property_image_id'); //add this line in your code
$query = $this->db->get('property');
error_log($this->db->last_query());
return $query->result();
}
Hope it will help you....

You can do it using join() method of active records class you are using.
join($table, $cond[, $type = ''[, $escape = NULL]])
Parameters:
$table (string) – Table name to join
$cond (string) – The JOIN ON condition
$type (string) – The JOIN type
$escape (bool) – Whether to escape values and identifiers
Returns:
CI_DB_query_builder instance (method chaining)
Return type:
CI_DB_query_builder
Adds a JOIN clause to a query.
Reference
In your example:
$this->db->like('property_location',$property_location);
$this->db->like('property_beds',$property_bedroom);
$this->db->like('property_bath',$property_bathroom);
$this->db->like('property_state',$property_status);
$this->db->like('property_type',$property_type);
$this->db->like('property_images.imageField', $imageFieldValue); // Observe change here.
$query = $this->db->get('property');
$this->db->join('property_images','property.your_id_field = property_images.your_id_field'); // Observe change here.

Related

How to display the datas from two tables using search query?

I need to display datas from particular based on the results.
I have two tables movies_info => has all movies info & tv_shows_info => has TV shows info. I have search box if i enter the search it should search from both the table and display the datas but the condition in the results if it has movie_id, it should show movie details and if the result has tv_show_id it should show tv-show details.
as of now i have used basic search to fetch datas from one table and i have tried of using two but its not working.
<?php
// sql query for retrieving data from database
$sql_query = "SELECT * FROM `movies_info`";
$result = mysqli_query($connection, $sql_query);
// SQL Query for filter
if(isset($_POST['search_button']))
{
$value_to_search = $_POST['value_to_search'];
// search in all table columns
// using concat mysql function
$search_query = "SELECT * FROM `movies_info` WHERE CONCAT(`movie_name`, `movie_original_name`, `release_year`, `movie_genre`, `movie_country`, `movie_stars`, `movie_director`) LIKE '%".$value_to_search."%'";
//$search_query = "SELECT * FROM `movies_info`,`tv_shows_info` WHERE CONCAT(`movie_name`, `release_year`, `movie_genre`, `movie_country`, `tv_show_name`) LIKE '%".$value_to_search."%' GROUP BY `movie_id`";
//$search_query = "SELECT * FROM `movies_info` UNION ALL `tv_shows_info` WHERE CONCAT(`movie_name`, `release_year`, `movie_genre`, `movie_country`, `tv_show_name`) LIKE '%".$value_to_search."%'";
//$search_query2 = "SELECT * FROM `tv_shows_info` WHERE CONCAT(`tv_show_name`, `tv_show_start_year`, `tv_show_end_year`, `tv_show_genre`, `tv_show_country`) LIKE '%".$value_to_search."%'";
//$search_query .= $search_query2;
$search_result = filterTable($search_query);
}
else {
$search_query = "SELECT * FROM `movies_info`";
$search_result = filterTable($search_query);
//echo 'No Search Results Found';
}
?>
<!-- /w3l-medile-movies-grids -->
<div class="general-agileits-w3l">
<div class="w3l-medile-movies-grids">
<!-- /movie-browse-agile -->
<div class="movie-browse-agile">
<!--/browse-agile-w3ls -->
<div class="browse-agile-w3ls general-w3ls">
<div class="tittle-head">
<h4 class="latest-text">Search Results for : "<?php echo $value_to_search ?>"</h4>
<div class="container">
<div class="agileits-single-top">
<ol class="breadcrumb">
<li>Home</li>
<li class="active" style="text-transform:Capitalize;">Search Results </li>
</ol>
</div>
</div>
</div>
<div class="container">
<div class="browse-inner">
<?php
echo $search_result;
$rowcount = mysqli_num_rows($search_result);
for($i=1;$i<=$rowcount;$i++){
$row=mysqli_fetch_array($search_result);
?>
<div class="col-md-2 w3l-movie-gride-agile">
<a href="movie.php?movie_id=<?php echo $row['movie_id']; ?>" class="hvr-shutter-out-horizontal"><img src="<?php echo $row['movie_image']; ?>" title="<?php echo $row['movie_name']; ?>" alt=" " />
<div class="w3l-action-icon"><i class="fa fa-play-circle" aria-hidden="true"></i></div>
</a>
<div class="mid-1">
<div class="w3l-movie-text">
<h6><?php echo $row['movie_name']; ?></h6>
</div>
<div class="mid-2">
<p><?php echo $row['release_year']; ?></p>
<div class="block-stars">
<ul class="w3l-ratings">
<li>
<span>IMDB <i class="fa fa-star" aria-hidden="true"></i> <?php echo $row['movie_rating']; ?> </span>
</li>
</ul>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="ribben two">
<p>NEW</p>
</div>
</div> <?php } ?>
</div>
as of now i can get the values from one table.,
My exact need is it can be Movie or TV-Show but i should get the datas from both the table if it is a movie it should show some particular info and if thats a TV-show it should show someother info.
First, you need to use prepared statements which you can use even if you are using the LIKE function in MySQL. Then you need to add spaces between the column names to prevent the values from being blended together like "star warsstar wars". This will cause a movie like "star wars to be a result when a user searches for "ss" which would be inaccurate.
$search_result = array();
$search_query = "SELECT * FROM `movies_info` WHERE CONCAT(`movie_name`,' ', `movie_original_name`,' ',`release_year`,' ',`movie_genre`,' ',`movie_country`,' ',`movie_stars`,' ',`movie_director`) LIKE CONCAT('%',?,'%')";
if($stmt = $db->prepare($search_query)){
$stmt->bind_param('ii',$_SESSION['iidn_reference'],$o['parent_ref_id']);
if($stmt and $stmt->execute()){
$result = $stmt->get_result();
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$search_result[] = $row;
}
}
}
After that, you could combine these tables with MySQL UNION but I strongly recommend just searching both tables and aliasing the table names to match.
SELECT 'movie' as `type`, `movie_id` as `id`, `movie_name` as `name`...
SELECT 'show' as `type`, `show_id` as `id`, `show_name` as `name`...

Display data from three tables with common id in foreach

So I currently have three tables like this given below:
AI = auto increment.
ci_users: user_id(AI), username, slug, email, biography.
ci_terms: term_id(AI), title, slug, body.
ci_relationship: id(AI), term_id, user_id, type.
I'm trying to display all the users from a specific term_id(where type is skill),
for example, let's say term_id 3(where term_id has the title of 'CSS' from ci_terms).
To make it more clear,the above paragraph basically says show me all users with skill of CSS but I want the displayed users to show all of their other skills which are stored in the ci_relationship table.
PARAGRAPHS BELOW ARE AFTER CLICKING A SPECIFIC TERM_ID(SKILL).
This is the function which shows me all of the users that have the term_id as their skill.(from my previous Stackoverflow question. Thanks to pradeep who helped me solve that query:
// All users with specific skill
public function get_user_skill($id)
{
$this->db->select('*, count(ci_relationship.term_id)');
$this->db->from($this->table);
$this->db->join('ci_relationship', 'ci_relationship.user_id = ci_users.id', 'INNER');
$this->db->order_by('ci_relationship.user_id', 'DESC');
$this->db->group_by('ci_relationship.user_id');
$this->db->where('ci_relationship.term_id', $id);
$this->db->where('ci_relationship.type', 'skill');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}
else
{
return false;
}
}
As I said before the code above just display the users with the specific term_id. Now this is the method in the controller which shows the expected data(users with specific term_id):
This is my controller :
public function skill($id){
// Get data
$data['users'] = $this->User_model->get_user_skill($id);
$term = $this->Terms_model->get($id);
// Get skill name
$data['skill'] = $this->Terms_model->get($id);
// Get skills per foreach --- HERE IS WHERE I NEED HELP
$data['skills'] = $this->User_model->skills($id);
// Meta
$data['title'] = $this->settings->title.' | '. ucfirst($term->title);
// Load template
$this->template->load('public', 'default', 'users/skill', $data);
}
This is the method that I'm trying to create to displayed the term_id per user_id:
// Skill for user foreach
public function skills($id){
$this->db->select('*');
$this->db->from($this->relationship);
$this->db->where('term_id', $id);
$this->db->where('type', 'skill');
$this->db->order_by('id', 'DESC');
$query = $this->db->get();
if($query->num_rows() >= 1){
return $query->result();
} else {
return false;
}
}
The view is the following code:
<?php if($users) : ?>
<div class="row">
<?php foreach($users as $user) : ?>
<div class="col-lg-3 col-sm-6">
<div class="card text-center panel">
<div class="cardheader panel-heading" style="background: url(<?php if($user->user_id) : echo base_url('assets/img/users/covers/'.get_username($user->user_id).'/'.get_username_cover($user->user_id)); else : echo base_url().'assets/img/noavatar.jpg'; endif ?>)no-repeat center center;"></div>
<div class="avatar">
<a target="_self" href="<?= base_url('users/profile/'.get_username($user->user_id)); ?>"><img class="img-circle" alt="<?= get_username($user->user_id); ?> profile picture" title="<?= get_username($user->user_id); ?> profile picture" src="<?php if($user->user_id) : echo base_url('assets/img/users/avatars/'.get_username($user->user_id).'/'.get_username_avatar($user->user_id)); else : echo base_url().'assets/img/noavatar.jpg'; endif ?>"></a>
</div>
<div class="info panel-body">
<div class="title">
<a target="_self" href="<?= base_url('users/profile/'.get_username($user->user_id)); ?>"><?= get_username($user->user_id); ?></a>
</div>
<div class="desc"><?php if(get_occupation($user->user_id)) : ?><?= get_occupation($user->user_id); ?><?php else : echo 'No Job'; endif ?> <?php if(get_company($user->user_id)) : ?> - <b><?= get_company($user->user_id); ?></b><?php else : echo '- <b>No Company</b>'; endif ?></div>
<!-- BEGINNING OF HERE IS WHERE I NEED HELP -->
<?php if($skills) : ?>
<?php foreach($skills as $skill) : ?>
<span class="label label-orange"><?= get_term($skill->term_id) ?></span>
<?php endforeach ?>
<?php endif ?>
<!-- END OF HERE IS WHERE I NEED HELP -->
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php else : ?>
<div class="alert alert-danger">No user with <?= $skill->title ?> skill found</div>
<?php endif; ?>
This is the actual output:
And this is the expected output:
I hope I could explain myself well enough to make it easy to understand,thanks in advance.
Hope this will help you :
Create a helper method get_skill_by_user just like get_username or get_occupation and use it in your view
function get_skill_by_user($user_id, $id)
{
$ci = & get_instance();
$ci->db->select('*');
$ci->db->from('ci_relationship');
$ci->db->where('term_id', $id);
$ci->db->where('user_id', $user_id);
$ci->db->where('type', 'skill');
$ci->db->order_by('id', 'DESC');
$query = $ci->db->get();
if($query->num_rows() > 0)
{
return $query->result();
} else
{
return false;
}
}
In your view your skills loop should be like this :
<?php $user_skills = get_skill_by_user($user->user_id, $user->term_id); if($user_skills) : ?>
<?php foreach($user_skills as $skill) : ?>
<span class="label label-orange"><?= get_term($skill->term_id) ?></span>
<?php endforeach ?>
<?php endif ?>

How to pass two different ids of different models from a controller to a view

I am having an issue with passing two ids in blog posts. The first id
displays a single post and the second one counts the number of posts.
Each works well separately but on integrating the two, it selects a
wrong post.
This is my code for the first model that fetches the comments
class News_model extends CI_Model {
function get_comment($id)
{
$this->db->select('comments.*,users.username');
$this->db->from('comments');
$this->db->join('users','users.id = comments.user_id', 'left');
$this->db->where('post_id',$id);
$this->db->order_by('date_added','asc');
$query = $this->db->get();
return $query->result_array();
}
}
This is the model that fetches the posts
class News_model extends CI_Model {
function get_one_news($news_id)
{
$this->db->select('*, news.id as id');
$this->db->from('news');
$this->db->join('users' , 'users.id = news.user_id');
$this->db->where('news.id' , $news_id);
$query = $this->db->get();
return $query->first_row('array');
}
function latestnews() {
$this->db->select("
news.*,users.*,comments.*,COUNT(comments.post_id) AS num_comments");
$this->db->from('news');
$this->db->join('users' , 'users.id = news.user_id');
$this->db->join('comments' , 'comments.post_id = news.id');
$this->db->group_by('comments.post_id');
$this->db->order_by('news.date_added','desc');
$this->db->limit(4);
$query = $this->db->get();
return $query->result_array();
}
}
And here is my controller
function view($id)
{
$data['news'] = $this->news_model->get_one_news($id);
$data["latest_news"] = $this->news_model->latestnews();
$data['comments'] = $this->m_comment->get_comment($id);
$data['content'] = 'single'; // template part
$this->load->view('includes/template',$data);
}
And finally the view file
<div id="sidebar" class="col-md-3 fix-left">
<!---- Start Widget ---->
<div class="widget wid-vid">
<div class="heading">
<h4>Latest News</h4>
</div>
<div class="content">
<div class="tab-content">
<div id="most" class="tab-pane fade in active">
<div class="post wrap-vid">
<?php if(count($latest_news)) {
foreach($latest_news as $l_news){ ?>
<div class="zoom-container">
<div class="zoom-caption">
</div>
<img src="<?php echo base_url('assets/images/'.$l_news['image'])?>"
style="height:80px;width:100px;"/>
</div>
<div class="wrapper">
<h5 class="vid-name"><a href="<?php echo base_url('bulletins/view/'.$l_news['id'])?>"><?php echo
substr(strip_tags($l_news['title']), 0, 15).'..';?></a></h5>
<div class="info">
<h6>By <?php echo $l_news['username']?></h6>
<span><i class="fa fa-calendar"></i><?php echo date( 'F jS, Y' , strtotime($l_news['date_added']))?></span>
<span><i class="fa fa-comment-o">/i><?php echo
$l_news['num_comments'];?></span>
</div>
</div>
<?php }}?>
</div>
</div>
</div>
</div>
</div>
Please check below mentioned solution. You have error in your select cause. When you want data from multiple table in CI and if columnName is same than CI query builder will overwrite value of that column. In this case you need to give alias to columnName. Please check below.
$this->db->select('news.id as newsId,users.id as userId,news.*,users.*,comments.*, COUNT(comments.post_id) as num_comments');
And in your view file you need to change below code.
Find $l_news['id'] and replace with $l_news['newsId']
This will resolve your issue. Let me know if it not works for you.

Category is not placed at the correct section

I have a problem while printing categories and sections (for a forum). So I have 2 tables : forum_section and forum_category -> the cat_id column from forum_section is linked with the section column from forum_category. Here's my query:
$forum = $con->query("SELECT a.*, b.*
FROM `forum_section` a
INNER JOIN `forum_category` b ON a.`cat_id` = b.`section` GROUP BY a.`cat_name` ORDER BY a.`cat_id`");
And that's how I'm trying to print them:
while($row = mysqli_fetch_object($forum)) {
echo '<div id="section">
<div class="section-head"><h3 class="yellow_text">'.$row->cat_name.'</h3></div>
'.printCategories($row->cat_id).'
</div>';
}
}
That's my printCategories function:
function printCategories($id) {
global $con;
$categories = $con->query("SELECT * FROM `forum_category` WHERE `section`='$id'");
while($row = mysqli_fetch_object($categories)) {
echo '<div class="cat">
<div class="cat-title">
<img src="styles/default/images/icons/'.$row->icon.'.png" alt="Icon">
<span>'.$row->name.'</span>
</div>
<div class="cat-topics">
asd
</div>
<div class="cat-posts">
asd
</div>
<div class="cat-lasttopic">
last topic name goes here
</div>
</div>';
}
}
So it does print them but not in the right place as you can see in this picture:
printCategories needs to return the result, not echo it, so that the concatenation in the caller will put it into the DIV that it's echoing. Your code is echoing before it returns anything, so it gets printed before the caller concatenates the result and prints its DIV.
function printCategories($id) {
global $con;
$categories = $con->query("SELECT * FROM `forum_category` WHERE `section`='$id'");
$result = '';
while($row = mysqli_fetch_object($categories)) {
$result += '<div class="cat">
<div class="cat-title">
<img src="styles/default/images/icons/'.$row->icon.'.png" alt="Icon">
<span>'.$row->name.'</span>
</div>
<div class="cat-topics">
asd
</div>
<div class="cat-posts">
asd
</div>
<div class="cat-lasttopic">
last topic name goes here
</div>
</div>';
}
return $result;
}

Yii: Retrieving data using Cdbcriteria

I am yiibie. What I am trying to do is to retrieve data from my database, and for that i am using cDbCriteria. I want to get the last 4 images from my database table which is named Event. and then want to display those 4 images individually. I am using this method but not getting any result, please help me with this.
<?php
$Criteria = new CDbCriteria();
$Criteria->limit = 4;
$Criteria->order = "image DESC";
$Criteria->select = "id, image";
$Events = Event::model()->findAll($Criteria);
foreach ( (array)$Events as $Event)
{
$Event[1]=$image1;
$Event[2]=$image2;
$Event[3]=$image3;
$Event[4]=$image4;
}
?>
<div class="row">
<h3>Events</h3>
<div class="col-md-3">
<div class="thumbnail">
<img src="<?php echo $image1 ?>">
<div class="caption">
<button class="btn btn-primary center-block">Join</button>
</div>
</div>
</div>
findAll() method retrieves an array of ActiveRecords, so you need a foreach loop. Try:
foreach ( (array)$Events as $Event)
{
echo "
<div class='col-md-3'>
<div class='thumbnail'>
<img src='$Event->image' >
<div class='caption'>
<a href='join.php'><button class='btn btn-primary center-block'>Join</button></a>
</div>
</div>
</div>
";
}
$Criteria = new CDbCriteria();
$Criteria->limit = 4;
$Criteria->order = "id DESC";
$Criteria->select = "id, image";
$Events= Event::model()->findAll($Criteria);
This will gives last 4 records
1st way
if(isset($Events[0]))
echo $Events[0]->image;
if(isset($Events[1]))
echo $Events[1]->image;
if(isset($Events[2]))
echo $Events[2]->image;
if(isset($Events[3]))
echo $Events[3]->image;
i am here checking isset() because there may b 3 entry. This is static
2nd way
foreach($Events as $event)
{
echo $event->image;
}
this is dynamic binding.

Categories