Slice array

Discussion in 'PHP' started by squishi, Feb 22, 2008.

  1. #1
    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?
     
    squishi, Feb 22, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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
     
    zerxer, Feb 22, 2008 IP
  3. squishi

    squishi Peon

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The error I get is that the array_slice function returns:
    So I figure array_slice cannot be used on this array.
     
    squishi, Feb 22, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    $_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...?
     
    nico_swd, Feb 22, 2008 IP
  5. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    zerxer, Feb 22, 2008 IP
  6. squishi

    squishi Peon

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    squishi, Feb 22, 2008 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    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
     
    nico_swd, Feb 22, 2008 IP
  8. squishi

    squishi Peon

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Okay. Thank you for the help.
    That was new information for me. I think passing the array to the function should work.
     
    squishi, Feb 22, 2008 IP
  9. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    zerxer, Feb 22, 2008 IP