Welcome Guest, Not a member yet? Register   Sign In
get the previous and next array key with a keyword
#1

[eluser]Bartolo![/eluser]
Hi guys,

i have a list of customers. which can be all customers, or a list from the search.
when you open a customer i want navigate previous and next.

first of all i have a function that saves all the id's found to a session:
Code:
$_SESSION['customer_ids'] = $this->customers_model->get_allcustomer_ids($search);

print_r($_SESSION['customer_ids']);

Code:
Array
(
    [0] => Array
        (
            [customer_id] => 1165
        )

    [1] => Array
        (
            [customer_id] => 1166
        )

    [2] => Array
        (
            [customer_id] => 786
        )

    [3] => Array
        (
            [customer_id] => 787
        )

    [4] => Array
        (
            [customer_id] => 788
        )

    [5] => Array
        (
            [customer_id] => 789
        )

    [6] => Array
        (
            [customer_id] => 790
        )

etc...

how to get the previous / current / next array key...???

what i want is a link (when i'm in customer 1166) a link to customer 786. and off course the previous one.

The current customer_id is:
Code:
$customer['customer_id'];

i was already trying with array_search and stuff but couldn't get it working.

Hopefully someone can help me out....

Thanks in advance!!!!

regards,

Bart


EDIT:

php.net told me that array_search returns an array key.
but when i use:

Code:
foreach($_SESSION['customer_ids'] as $array) {
                
    $key = array_search($customer['customer_id'], $array);
    echo $key;

}

it shows the word "customer_id". so how do i get the array key corresponding with my current record??
#2

[eluser]jmadsen[/eluser]
next() is pretty cool :-)

http://php.net/manual/en/function.next.php

next(), prev(), end() are explained here.

Do you need the value, or the key?

---

Google search words: "php array next element"
#3

[eluser]Bartolo![/eluser]
Hi,

what i need is the next and previous customer_id in the array.

i already found the next functions but i think i'm using the multidimensional array's the wrong way...

this:

Code:
foreach($_SESSION['customer_ids'] as $array) {
    
    if(array_search($customer['customer_id'], $array) == null){
        
        //nothing
        
    }
    else {
        
        echo 'current: '.current($array);
        echo '<br/>';
        echo 'next:    '.next($array);
    }
    
}


current returns: 1165

next returns.....nothing....
#4

[eluser]jmadsen[/eluser]
right, so read up on the stuff I posted:


$my_array = $_SESSION[‘customer_ids’];

$my_customer = next($my_array);

$my_customer[1] is your value, I think.

Maybe I'm not clear what you are after, but it seems like php array coding from here...
#5

[eluser]Bartolo![/eluser]
Thanks for your help...it was easier than i thought...

This is the result:

Code:
foreach ($_SESSION['customer_ids'] as $key => $value)
{

    if (array_search($customer['customer_id'], $value) == null)
    {
        //the customerid is not found in this array
    }
    else
    {
        // customer_id is found, now lets create the buttons
        
        // First check if the previous key exists
        if(array_key_exists($key-1, $_SESSION['customer_ids']))
        {
            //yes...key exists so button can be made
            $data['prev_button'] = '<div style="float: left;" id="view_navigate_button">
                                        <a href="'.site_url().'/customers/view/'.$_SESSION['customer_ids'][$key-1]['customer_id'].'">Previous</a>
                                    </div>';
        }
        else
        {
            $data['prev_button'] = '';
        }                    
        
        // First check if the previous key exists
        if(array_key_exists($key+1, $_SESSION['customer_ids']))
        {
            //yes...key exists so button can be made
            $data['next_button'] = '<div style="float: right;" id="view_navigate_button">
                                        <a href="'.site_url().'/customers/view/'.$_SESSION['customer_ids'][$key+1]['customer_id'].'">Next</a>
                                    </div>';
        }
        else
        {
            $data['next_button'] = '';
        }                                      

                            
    }




Theme © iAndrew 2016 - Forum software by © MyBB