0

I've just started using Codeigniter, and am loving MVC. I have a simple mysql query in a model that returns an array of rows to my controller.

$query = $this->db->get('shows');
return $query->result();

Date information is stored in the database as mysql date (yyyy-mm-dd) and I explode to get month mm and day dd.

What I'm wondering is, is there any way to manually add the variables for month and day of each row to the query result using a simple foreach? I know I could manually add each database field's value to an array, and include the month and day variables, but I'm hoping there's a simpler way of inserting them into the already existing array created by the query.

2 Answers 2

1

@slier had the code, but below is transformed to codeigniter

$this->db->select("*, MONTH(date_column) as date_month, DAY(date_column) as date_day");
$query = $this->db->get('shows');
return $query->result();
1
  • I'd actually come across that method of pulling the month and day, but the post I read said that it couldn't be combined with SELECT * so I started looking elsewhere. Thanks @Aaron! Commented Jul 8, 2011 at 13:55
1

Ok im not familiar with codeigniter, but if you want to get month and day in raw mysql you can simply do something like below

$sql = 'SELECT MONTH(date) AS month, DAY(date) AS day FROM shows';
$result = $this->db->query($sql);

Im assuming your date column is in date dataype

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.