black_belt
black_belt

Reputation: 6799

How to query my database to get the result as an array?

my database table structure is like following:

wdt        date            teacherid
1       2011-01-11           1001
2       2011-01-11           1002
3       2011-01-12           1001
4       2011-01-12           1002

Now what I want to do is select the date field from the table where teacherid="1001" and then return the result as an array like this- array("2011-01-11","2011-01-12")

so that in my view file I can check if a particular data exists in the array result- like following :

 if (in_array("2011-05-18", $date))       // here the $date would be the array result which -
                                        //  I am expecting to get from model via controller
        {
        echo "Found"; 
        } 

My controller looks like following:

function index(){


    $this->load->model('mod_teacher_workingday');
    $data['date']= $this->mod_teacher_workingday->get();

    $data['main_content']='view_teacher_workingday';
    $this->load->view('includes/template',$data);
  }

Would you please kindly show me how my model should look like in order to get the result as an array in my view file and I can check if a particular data exists in that array? Just for your information I am using Codeigniter.

Thanks :)

Upvotes: 0

Views: 129

Answers (3)

Philip
Philip

Reputation: 4592

Do yourself a favour, download an IDE that has database support. Netbeans lets you connect to a database through a JDBC driver. You can do a raw query on the fly right inside your application. Then once you have your query nailed, simply transfer it to Codeigniter Active record or whatever you are using or just use Active records raw query.

Upvotes: 0

Damien Pirsy
Damien Pirsy

Reputation: 25435

Should be:

$query = $this->db->select('date')
                  ->from('teachers')
                  ->where('teacherid',1001)
                  ->get();
$dates = array();
foreach($query->result() as $date)
{
  $dates[] = $date->date;
}
return $dates;

The returned array will be just empty in case of no results found for that ID.

Upvotes: 2

Derk Arts
Derk Arts

Reputation: 3460

Why don't you look for the date with mysql, much more efficient.

Select teacherid from teachers where date = "2011-05-18" AND teacherid = 1001

If you get 0 results, you know that that combination does not exist

Upvotes: 1

Related Questions