0

I have a select query which outputs several rows. Query is working but what I need is, I want to insert a custom column with value into the above result. I am using Codeigniter 3 with PostgreSQL. The query looks like,

SELECT * FROM tbl_name ORDER BY timestamp ASC LIMIT 6;

The result I got is something like,

Array
(
    [0] => Array
        (
            [id] => 1
            [title] => Adventure Honeymoon
            [nights] => 5
            [price] => ₹37,000.00
            [discount] => 0
        )
    [1] => Array
        (
            [id] => 2
            [title] => Wild Side Of Kerala
            [nights] => 4
            [price] => ₹24,000.00
            [discount] => 0
        )
...
)

I want to add a custom column named 'category' with the result, which should looke like the below,

Array
(
    [0] => Array
        (
            [id] => 1
            [title] => Adventure Honeymoon
            [nights] => 5
            [price] => ₹37,000.00
            [discount] => 0
            [category] => Honeymoon Tours                                                                                     
        )
    [1] => Array
        (
            [id] => 2
            [title] => Wild Side Of Kerala
            [nights] => 4
            [price] => ₹24,000.00
            [discount] => 0
            [category] => Wild Life Tours                                                                                     
        )
...
)

Quick help will be appreciated.

3
  • 1
    From where to get or assign category values? Commented Apr 16, 2019 at 10:35
  • @jainvikram444 I have an array of customised category lists. Commented Apr 16, 2019 at 10:39
  • 1
    It's inside of database table or in array of PHP? Commented Apr 16, 2019 at 10:41

2 Answers 2

1

Try this

Model

public function get_data(){
   $query = $this->db->select('*')
   ->from('tlb_name')
   ->order_by('timestamp', 'ASC')
   ->limit(6)
   ->get();
   return $query->result_array();  

}

Controller

public function custom_addition(){

   $data = $this->model_name->get_data();

   foreach($data as $row){
       $row['category'] = 'Value';
   }

   print_($data);

}

$data the result of query

Sign up to request clarification or add additional context in comments.

5 Comments

This worked a bit, only producing the last result row while I return the result in model to controller. There are 6 rows in the query result.
Only last row getting the value of $data['category'] ?
All I get is a single row while there are 6 rows if I run the query and return the same without looping with foreach.
After the foreach loop check the data first print_r($data);
0

You will add column and add values as per condition using case or static or joining tables. You have to change ? to values/column_name as per requirement:

SELECT T1.*, (case when T1.column_name = ? then ? else ? end) as 'CutomColumnName'
FROM TableName T1
ORDER BY T1.timestamp ASC
 LIMIT 6;

Ex:

SELECT T1.*, 
   (case when T1.CategoyId=1 then 'Category-1'  
         when T1.CategoyId=2 then 'Category-2'
         else 'Not Found'
    end) as 'CutomColumnName'
    FROM TableName T1
    ORDER BY T1.timestamp ASC
     LIMIT 6;

Or

SELECT T1.*, C1.name 
FROM TableName T1
left outer join category C1 on C1.id=T1.category_id
ORDER BY T1.timestamp ASC
 LIMIT 6;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.