Welcome Guest, Not a member yet? Register   Sign In
SELEC LEFT
#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)


Messages In This Thread
SELEC LEFT - by El Forum - 09-27-2011, 07:59 AM
SELEC LEFT - by El Forum - 11-18-2012, 07:42 PM



Theme © iAndrew 2016 - Forum software by © MyBB