<?php

class Tablas extends CI_Controller {

	function __construct()
	{
        parent::__construct();
	}
	
	function index()
	{
		$consulta = "select name from sysobjects where type='U'";
		$query = $this->db->query($consulta);
		//echo $query->num_rows();
		foreach($query->result() as $row) {
		
			echo '<p><a href="'.base_url().'tablas/view_tabla/'.url_title($row->name).'">'.$row->name.'</a></p>';
			//echo $row->name.'<br />';
		}
	}
	
	function view_tabla($tabla=FALSE,$criterios=FALSE,$orden=FALSE,$dir='ASC',$limit=20) {
	
		if (!$tabla) {exit('No hay tabla seleccionada');}
		
		$consulta = "SELECT TOP ".$limit." * FROM ".$tabla;
		//criterios
		if ($criterios) 
		{
			$consulta .= " WHERE 1=1 ";
			
			$criterios = explode("#",$criterios);
			//criterios será un array
			if (is_array($criterios)) {
			 foreach ($criterios as $key) {
			 
			 	list($key,$valor) = explode("~",$key);
				
			 	$consulta .= " AND ".$key."'".$valor."' ";
			 }
			}
		}
		
		if ($orden) {
			$consulta .= " ORDER BY ".$orden." ".$dir;
		}
		
		$query = $this->db->query($consulta);
		
		print_r($query->result());
		
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */