Welcome Guest, Not a member yet? Register   Sign In
How to get all records of a table ?
#1

[eluser]Unknown[/eluser]
Hello,

I'm trying to create a page displaying (biological) data stored in a table named MARKER (Oracle database 11.2.0.1.0, CI 2.1.0).

I've got a model with a function :

Quote:
public function getMarkers($limit = NULL , $offset = NULL )
{
$this->db->limit($limit, $offset);
return $this->db->get('MARKER')->result();
}
public function getNumMarkers(){
return $this->db->count_all('MARKER');
}

I've got my controller :
Quote: public function allMarkers()
{
$per_page = 1000;
//page informations
$data=array() ;
$data['title']="Markers List" ;
$data['heading']="Markers List" ;
$data['table_header']=array('MARKER_ID','MARKER_NAME','TAXON_ID');

//ask model for data
$this->load->model('marker_mod', '', TRUE);
$data['markers']=$this->marker_mod->getMarkers($per_page, $this->uri->segment(3)) ;

$this->load->library('pagination');

$config['base_url'] = '/admin/TestCodeIgniter/index.php/marker/allMarkers';
$config['total_rows'] = $this->marker_mod->getNumMarkers();
$config['num_links'] = 10;
$config['per_page'] = $per_page;
$config['uri_segment'] = '3';
$this->pagination->initialize($config);

$data['links'] = $this->pagination->create_links();

//call the view and pass the data
$this->load->view('paginated_marker_view', $data) ;
}

It works well, I have managed to get a paginated view of my 47 607 markers currently stored (49 pages with 1000 markers on each).

I have tried to add a link in order to download a file holding all these values (users want to get all data in a file).

Code:
public function getMarkersFileTest()
{
  $this->load->helper('file');
  $path = "MarkersTest.txt";
  delete_files($path);

  write_file($path, "Line\tmarker_id\tmarker_name\n", 'a+');

  $nb = $this->getNumMarkers();

  $line = 1;
   foreach ( $this->getMarkers($nb) as $marker)
   {
    $data = "$line\t$marker->MARKER_ID\t$marker->MARKER_NAME\n";
    $line++;
    if ( ! write_file($path, $data, 'a+'))
    {
         echo('Unable to write the file') ;
    }
   }
  return($path);
}

This failed, the file is not created ! And I do not get any error (even with error_reporting(ALL); and ini_set('display_errors', 1); in index.php).

I must use the limit and offset param and make a loop until all results are gotten :
Code:
public function getMarkersFile()
{
  $this->load->helper('file');
  $path = "Markers.txt";
  delete_files($path);

  write_file($path, "Line\tmarker_id\tmarker_name\n", 'a+');

  $nb = $this->getNumMarkers();

  $line = 1;
  for ($i = 0; $i <= $nb; $i+= 10000) {
   foreach ( $this->getMarkers(10000, $i) as $marker)
   {
    $data = "$line\t$marker->MARKER_ID\t$marker->MARKER_NAME\n";
    $line++;
    if ( ! write_file($path, $data, 'a+'))
    {
         echo('Unable to write the file') ;
    }
   }
  }
  return($path);
}

Using Limit and Offset param is easy using Active Record Class but when I want to launch more complex queries (SELECT M.marker_id, m.marker_name, t.scientific_name FROM marker m , taxon t WHERE m.TAXON_ID = t.taxon_id), I don't know what to do.

Creating a simple "select * from table" and backup these data into a file seems to be impossible with CodeIgniter ?


Thank you for any idea !

Fred




Theme © iAndrew 2016 - Forum software by © MyBB