codeigniter , mysql - php

I'm new to CI and I'm having a problem with MySQL
Table1
id | house_id |
1 | 1 |
2 | 4 |
3 | 3 |
Table2
house_id | image_name |
4 | a.jpg |
4 | b.jpg |
3 | c.jpg |
How I can select (distinctively) image_name for each house_id by using CodeIgniter Active Record class?

http://codeigniter.com/user_guide/database/active_record.html
this is the API you are looking for $this->db->distinct();

Just use a simple method group_by in codeigniter model
function get_house_image(){
$items = $this->db->select('house_id', 'image_name')
->from("table1")
->join("table2", "table1.house_id = table2.house_id")
->group_by("table1.house_id")
->get();
return $items->result_array();
}

In your Model
function get_house_image($id){
$this->db->where('house_id',$id);
$query = $this->db->get('table2');
return $query->result();
}
In your cotroller
function house($id){
$data['house'] = $this->your_model->get_house_image($id);
$this->load->view('your_view',$data);
In your view foreach the result and use it. This gets the house's picture which id you pass in url.

You can use left join of tables to get records as follow:
$this->read->select('t1.house_id,t2.image_name');
$this->read->join('table2 as t2','t1.house_id = t2.house_id','left');
$result = $this->read->get('table1 as t1');
if($result){
$data = $result->result_array();
print_r($data);
}

Related

How to display name instead of id if records are in one table?

I am using CodeIgniter. I have an employee table and records are
id |firstname | lastname | mobileno | created_by
2 |mnb | nbgfv | 1452145625 | 1
3 |jhg | uhgf | 1452365478 | 2
4 |poi | ijuy | 1458745632 | 2
5 |tgf | tgfd | 1458745254 | 2
6 |wer | qwes | 1523654512 | 2
Now My issue is in the column created_by. When I am displaying the record of any id value then I am getting the output like
id |firstname | lastname | mobileno | created_by
3 |jhg | uhgf | 1452365478 | 2
But my expected output
id |firstname | lastname | mobileno | created_by
3 |jhg | uhgf | 1452365478 | mnb nbgfv
I have to display the name of created_by
I tried only this query.
$get_single_emp_record = array('id' => 3);
$this->db->where($get_single_emp_record);
$query = $this->db->get('tbl_employee');
$result = $query->row();
if($result)
{
return $result;
}
else
{
return 0;
}
I have a hint (maybe this can give solution in your problem).
Try query like this :
SELECT
t1.id , t1.firstname , t1.lastname ,t1.mobileno,
CONCAT(t2.firstname ," ",t2.lastname ) AS createby
FROM tbl_employee AS t1
JOIN tbl_employee AS t2 ON t2.id = t1.created_by
WHERE t1.id = '3'
With above query you no need create temporary table or other additional tables.
I Tested it. >>>
http://sqlfiddle.com/#!9/e693cf/2/0
Thanks
you will have to create another table maybe tbl_creator which will have the id, creator_name then in your query you will perform a Join operation

select rows with the same value using Codeingiter

How to select all with the same value using codeigniter.
+-------+-------------+------------+
| id | coupon_code | Barcode |
+-------+-------------+------------+
| 1 | COUPON02 | 12542 |
| 2 | COUPON02 | 11229 |
| 3 | COUPON03 | 11823 |
| 4 | COUPON03 | 47875 |
+-------+-----------+--------------+
public function fetch_available_coupon(){
$coupon_code = $this->uri->segment(3);
$this->db->select('*');
$this->db->from('barcode');
$this->db->where('coupon_code', $coupon_code);
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}else{
return false;
}
}
in my result it only show 1 row not the not all the same Coupon_Code
Im using codeigniter as my active record. Please Help thank you
use where condition instead of group by
$this->db->where('coupon_code', $coupon_code);
You can use GROUP BY to group values from a column, and, if you wish, perform calculations on that column. You can use COUNT, SUM, AVG, etc., functions on the grouped column.

Get total comments of article in PHP with NotORM

My table ofcomments looks like:
| ID | article_id | user_id | ...
|-------------------------------|
| 1 | 1 | 1 | ...
| 2 | 2 | 2 | ...
| 3 | 2 | 1 | ...
| 4 | 3 | 2 | ...
AndI need to get top 5 articles with the most comments. When I use this statement in SQL console SELECT 'article_id', count(*) as 'total' FROM 'comments' GROUP BY 'article_id' ORDER BY 'total' LIMIT 5, then I get everything I want. But I need to do this with NotORM and this is where I stucked at. This is my function to get these articles:
function getBestActive() {
$items = $this->db->comments()
->select("article_id, count(*) as 'total'")
->order("total DESC")
->limit(5);
$articles = array();
foreach($items as $item) {
$article = $this->db->article('id', $item['article_id'])->fetch();
$article['img'] = "thumb/{$article['uri']}.jpg";
$article['comments'] = $item['total'];
$articles[] = $article;
}
return $articles;
}
But it returns me array with only 1 article (the most commented) and I need the most 5 articles. Or is it possible to execute custom SQL statement with NotORM (that could be answer too)?
Oh now I see. I forget to add group() function. So using this selection everything works:
$items = $this->db->comments()
->select("article_id, count(*) as 'total'")
->group("article_id")
->order("total DESC")
->limit(5);

Zend Framework 1. Fetch comments

I can't create an appropriate query which could select all comments connected with one particular image and get those comments authors.
I would like to create a query something like:
select a comment where comment_id == image_id && user_id(table comments) == user_id(table users)
This is MySQL part:
Table 'comments'
id | comment | user_id | image_id |
1 | test 1 | 1 | 1 |
2 | test 2 | 1 | 2 |
3 | test 3 | 2 | 1 |
Table 'users'
id | name |
1 | test1 |
2 | test2 |
Table 'images'
id | img |
1 | test.jpg |
2 | test.jpg |
3 | test.jpg |
4 | test.jpg |
Controller Part:
$imageId = $filter->filter ($request->getParam('id'));
$this->view->imageId = $filter->filter ($request->getParam('id'));
$this->view->imageChosen = $images->fetchRow($images->select()->where('id = ?', $imageId));
$users = new Users();
$userChosen = new Users();
$comments = new Comments();
$this->view->comments = $comments->fetchAll();
$this->view->userChosen = $users->fetchRow($users->select()->where('id = ?', $this->view->imageChosen->author_id));
$this->view->commentsChosen = $comments->fetchAll($comments->select()->where('id = ?', $imageId));
View part:
for ($i=0; $i < count($this->commentsChosen); $i++) {
echo $this->commentChosen[$i]->comment;
}
Right now I only get the very first comment.
What I mean is I need all comments belonging to each picture as well as authors info.
Thanks for your help!
As you've said, you can fetch the image info with your query, I'll extend it in order to fetch the user info too:
$select = $comments->select()->setIntegrityCheck(false)
->from('comments')
->joinInner('users', 'users.id = comments.user_id')
->where('comments.image_id = ?', $this->view->imageChosen->id);
$this->view->commentsChosen = $comments->fetchAll($select);
The generated query would be:
SELECT comments.* users.* FROM comments
INNER JOIN users ON users.id = comments.user_id
WHERE comments.image_id = [Your_id_here]
I hope this helps!
I've managed to get all comments belonging to each picture.
Controller:
$this->view->commentsChosen = $comments->fetchAll($comments->select()->where('image_id = ?', $this->view->imageChosen->id));
View:
for ($i=0; $i<count($this->commentsChosen); $i++) {
echo $this->commentsChosen[$i]->comment;
//Have 'user_id' only
echo $this->commentsChosen[$i]->user_id;
}
However, I still can't get authors details.

Table join: unable to show the data what I want

If I have table like this:
ID | Title | Topic | Summary
1 | A | Technology | ...
2 | B | Health | ...
3 | C | Sport | ...
This is my CI_Model:
function show($limit, $offset)
{
$this->db->select('document.id, document.title, document.summary, document.id_topic AS topic');
$this->db->from('document');
$this->db->join('topic', 'topic.id_topic = document.id_topic');
$this->db->limit($limit, $offset);
$this->db->order_by('id', 'asc');
return $this->db->get()->result();
}
This is my Controller:
$docdata = $this->Trainingmodel->show($this->limit, $offset);
...
$this->table->set_heading('ID', 'Title', 'Topic', 'Summary');
foreach ($docdata as $doc)
{
$this->table->add_row($doc->id, $doc->title, $doc->topic, $doc->summary);
}
Evidently the topic shows it's id, not name.
For example:
ID | Title | Topic | Summary
1 | A | 1 | ...
2 | B | 2 | ...
3 | C | 3 | ...
What should I do? I want to show topic's name, not topic's id.
Looking at your table structure you posted as comments in the other answers, I think you need topic.topic in your select() and topic.id = document.id_topic in your join() -
$this->db->select('document.id, document.title, document.summary, topic.topic');
$this->db->from('document');
$this->db->join('topic', 'topic.id = document.id_topic');
$this->db->select('document.id, document.title, document.summary, topic.topic');
$this->db->from('document');
$this->db->join('topic', 'topic.id = document.id_topic');
Maybe its because of this document.id_topic
$this->db->select('document.id, document.title, document.summary, document.id_topic AS topic');
should it be something like document.topic or document.name_topic ?

Categories