Welcome Guest, Not a member yet? Register   Sign In
SELEC LEFT
#1

[eluser]Unknown[/eluser]
Hello, I am now beginning to use the CI, so I can be writing nonsense and apologize to my English by google translation.

In a project I'm working, I had to limit the amount of characters that came from a column in my database, I tried and did not find any function of the CI to do it, and did not want to use PHP for this, or work it in my control or in my view, wanted to do this in my model, and did not want to use SQL directly.

It was then that I had the idea to create the $this-> db-> select_left to make SQL SELECT LEFT ('column', 200) AS column.

So I wanted to give my suggestion about it, I love CI and I think that it will become the best framework of all, and I hope my contribution helps. While believing that the development team should already have thought about it.

Actually I created this based on another meted used to _sum _MAX _min.

DB_active_rec.php

Code:
// --------------------------------------------------------------------
/**
  * Processing Function for the four functions above:
  *
  *  select_left()
  *
  * @param string the field
  * @param string an alias
  * @return object
  */
protected function _left($select = '', $alias = '', $type = 'LEFT')
{
  if ( ! is_string($select) OR $select == '')
  {
   $this->display_error('db_invalid_query');
  }

  $type = strtoupper($type);

  if ( ! in_array($type, array('LEFT')))
  {
   show_error('Invalid function type: '.$type);
  }

  if ($alias == '')
  {
   $alias = $this->_create_alias_from_table(trim($select));
  }

  $sql = $type.'('.$this->_protect_identifiers(trim($select)).','.$alias.') AS '.$select;

  $this->ar_select[] = $sql;

  if ($this->ar_caching === TRUE)
  {
   $this->ar_cache_select[] = $sql;
   $this->ar_cache_exists[] = 'select';
  }

  return $this;
}

Using:

Code:
$this->db->select_left('column',175);

Remembering that this is just a suggestion, I'm still new to CI.

Thank you!
#2

[eluser]Unknown[/eluser]
I used SQL directly until I googled it and saw this thread.
I don't know if it is possible to use this method in previous versions.
But for any novice who just learned CI,I can show you how this trick works.

For example,I have a table looks like:

-------------
| ID | name |
| 1 | セタさん |
--------------

and the code:

Code:
$this->db->select('LEFT(`name`,2)',FALSE);
$temp=$this->db->get('mytable');
$data=$temp->result_array();
var_dump($data);

You may not only know how select works,but also what that FALSE means.
(If you don't ,read the user guide again.)
But there's a trick stopping you using the column directly,
so you must use var_dump seeing what's in $data.
We'll see:

array(1) { [0]=> array(1) { ["LEFT(`name`, 2)"]=> string(6) "セタ" } }

You see that is not what we expect.
If we use SQL directly,the key string will be "LEFT(`name`,2)" ,
but CI generated an additional space between comma and 2.
So if you want your arrays can be used easily,
you may need this code:

Code:
$num=$temp->num_rows();
$data1=array();
for($i=0;$i<$num;++$i)
{
  $data1[]=array('name'=>$data[$i]["LEFT(`name`, 2)"]);
}

You can also add:
Code:
echo $data1[0]['name'];
to see if it returns you セタ. (first 2 words)




Theme © iAndrew 2016 - Forum software by © MyBB