SOLUTION WITH AN EXAMPLE:

// get doc_typs
$i=0;
$j='';
foreach ($types->result() as $row)
{
$j.="WHEN d.doc_type = '".$row->type."' THEN ".$i++.' ';
}
  $when=$j;
// end get type