Hi. I want to use the smarty pagination function. Here is the code: function get_db_results() { SmartyPaginate::setTotal(count($_data)); return array_slice($_data, SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit()); } PHP: My $_data array looks like this: $_data = array ( "0" => array("id"=>"1", "url"=>"url1", "description"=>"test"), "1" => array("id"=>"2", "url"=>"url2", "description"=>"test") ); PHP: How do I slice this array so that the pagination works?
I'm not sure what the problem is here. Just use the array_slice function as well? It's not a SmartyPaginate function or anything, it's a PHP one. Explain more what you're trying to do.. http://us.php.net/array_slice
The error I get is that the array_slice function returns: So I figure array_slice cannot be used on this array.
$_data is not defined in the scope of the function, that means it is not an array. I'm not sure where it's supposed to come from...?
Oh, yea, didn't notice he was using it in a function. If you're trying to call array_slice in a function, and you didn't just define the $_data array in that function, be sure to put global $_data; at the top of the function.
I don't think this is the problem. I used this and still get the error: function get_db_results() { $_data = $myarray; SmartyPaginate::setTotal(count($myarray)); return array_slice($_data, SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit()); } PHP: where $myarray is the array above, defined in the php file. Still got the error.
But not in the function. Each function has its own scope. Only super-global variables are accessible there, and variables that have been defined inside the function, or variables that have been imported using the global construct. (Or variables that have been passed as argument). EDIT: Read about the basics here: www.php.net/functions
Okay. Thank you for the help. That was new information for me. I think passing the array to the function should work.
You don't have to pass it though if you're going to use the same array every time. You can just global it in the function. function get_db_results() { global $myarray; $_data = $myarray; SmartyPaginate::setTotal(count($myarray)); return array_slice($_data, SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit()); } PHP: By putting that first line, it will make it accessible from there and editing it in there will also edit the main one.