mysql_fetch_length equivalent function in CodeIgniter ? - codeigniter

I want to count how many characters are generated by a database query.
In PHP I'm using mysql_fetch_length. Is there an equivalent function in CodeIgniter?

"In Codeigniter" is to php as "with jquery" is to javascript, these days. It's just php.
No, CI doesn't have this. Return a row_array() and then:
$row_array = ['test', 'this'];
$new_total = array_sum(array_map(function($value){$total = strlen($value);return $total;}, $row_array));
var_dump($new_total);

Related

Codeigniter 4 Current/Active Controller

In Codeigniter 3 is possible to get current active class and method with this code:
$active_controller = $this->router->fetch_class();
$active_function = $this->router->fetch_method();
Are there such functions in Codeigniter 4?
In CodeIgniter 4
$router = service('router');
$controller = $router->controllerName();
$router = service('router');
$method = $router->methodName();
Its worth saying those classes were never officially part of CI3 (https://codeigniter.com/user_guide/installation/upgrade_300.html?highlight=fetch_class). Bearing in mind CI4 is a lot more flexible and that routes are defined more variably I would look at the routing side of things and extract it from there (https://codeigniter4.github.io/userguide/incoming/incomingrequest.html#the-request-url).
You can use PHP constant or functions that provide you the same:
Get Current Function name:
__FUNCTION__
Get Current Class name:
__CLASS__
OR
get_class()
Codeigniter 4: All the above is working well otherwise use the same code that #mathan answered:
$router = service('router');
echo $router->controllerName();

select_max not working in CI

This is not working in my CI app:
$query = $this->db->select_max('order')->get('posts');
print_r($query);
Why is that?
I have a column in my DB called order (int, where the highest value is currently 6) and the table is called posts
Why nothing is outputted instead a number 6 ?
This just runs the query, you need to use ->row() to get the result from it.
$this->db->select_max('order', 'max_order');
$query = $this->db->get('posts');
echo $query->row()->max_order;
DOCS:
https://www.codeigniter.com/userguide2/database/active_record.html
https://www.codeigniter.com/userguide2/database/results.html

Returning values from php file to ajax

I have written ajax engine to connect to php file. In php file i am accesing data from database. I want to return the array fetched from database back to ajax engine.. How to return the values..
what you can do is join the array with something in your php let's say
$return_string=join('||||', $array);
Then in the javascript split the response you get
var response_array=response.split('||||');
with JSON :)
$json_str = json_encode($arr);
echo $json_str;
I assume you are wanting to use the array in another language such as JavaScript.
Decoding in JavaScript is easy: var arr = JSON.parse(request.responseText);
Hope this helps..

Short table name for SQL Query with Codeigniter database library

I'm using CodeIgniter, I want to using short table name for my SQL Query like that:
select pr.name from product pr where pr.id=12;
by using db class, I supposed to do:
$this->db->select('pr.name')->from('product pr')->where('pr.id', 12)->get();
This works perfect on CI 2.1.3. Don't forget to use result().
Example works for me:
function test(){
$this->load->database();
$sql = $this->db->select('pr.order_id')->from('items_table pr')->where('pr.order_id', 2)->get();
foreach($sql->result() as $item){
echo $item->order_id;
}
}
You can do it in this form:
$this->db->select('pr.name');
$this->db->from('product as pr');
$this->db->where('pr.id', 4);
return $this->db->get()->row_array();
with row_array() you will get one row, with result_array() you will get result of array.
There are so many solutions.. I prefer HMVC, but for demo purpose to explain "how it works", solutions inside controller (terrible,sucks!!!!)
Answer for using shorts table as alias, pls read rtfm or use simple query and generating results
For join methods,you can use too. Happy coding

CodeIgniter: current_url shows question mark

Say my page is on:
http://localhost/app1/profile/index/123/
The result of current_url() is this:
http://localhost/app1/?profile/index/123
There is a ? that shouldn't be there. The question mark seems to be caused by the following config setting:
$config['enable_query_strings'] = TRUE;
I need query strings enabled in my application. Any ideas what I need to do?
EDIT 1:
Also, in the case when the URL does have a query string, I need current_url to also return that. I'm hoping Phil Sturgeon's solution here CodeIgniter current_url doesn't show query strings will help me.
I'm using CI 2.1.0.
As mentioned, $config['enable_query_strings'] is sort of a "legacy" setting in Codeigniter from back when there was no support $_GET (really, none).
http://codeigniter.com/user_guide/general/urls.html
Enabling Query Strings
In some cases you might prefer to use query strings URLs:
index.php?c=products&m=view&id=345
c === Your controller name
m === Your method name
The rest is the method arguments. It's a very misleading description, and there's no mention of the other setting or query strings at all in the rest of the URL docs. I've never heard of anyone actually using this. CI comes with $config['allow_get_array']= TRUE; by default, which is what you want.
You can modify the current_url() function for query string support, just create application/helpers/MY_url_helper.php and use this:
function current_url($query_string = FALSE)
{
$CI =& get_instance();
$current_url = $CI->config->site_url($CI->uri->uri_string());
// BEGIN MODIFICATION
if ($query_string === TRUE)
{
// Use your preferred method of fetching the query string
$current_url .= '?'.http_build_query($_GET);
}
// END MODIFICATION
return $current_url;
}
Then call it like current_url(TRUE) to include the query string.
Don't use: $config['enable_query_strings'] = TRUE;
Use this instead: $config['allow_get_array']= TRUE;
enable_query_strings is not what you think and is not used much.
To build your own query strings, use one of these two:
$query_string = http_build_query($this->input->get());
$query_string = $this->input->server('QUERY_STRING');
along with this:
$lastseg_with_query = $lastseg.'?'.$query_string;
Please consult this SO Q&A for more information: URI Segment with Question Mark

Resources