Lei-Lonnie
Lei-Lonnie

Reputation: 784

How do you get next() in a multidimentional PHP array?

I have an array like the one below:

$products = array();

$products["Archery"] = array(
    "name" => "Archery",
    "img" => "img/wire/100-Archery.jpg",
    "desc" => "Archer aiming to shoot",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);
$products["Artist"] = array(
    "name" => "Artist",
    "img" => "img/wire/101-Artist.jpg",
    "desc" => "Artist with palette & easel",
    "prices" => array($price3,$price6),
    "paypal" => $paypal5,
    "sizes" => array($size1, $size2)
);
$products["Badminton"] = array(
    "name" => "Badminton",
    "img" => "img/wire/102-Badminton.jpg",
    "desc" => "About to hit bird above head",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);
$products["Baseball-Bat-Stance"] = array(
    "name" => "BASEBALL -Bat - Stance",
    "img" => "img/wire/103a-Baseball-Stance.jpg",
    "desc" => "Waiting for pitch",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);
$products["Baseball-Bat-Swing"] = array(
    "name" => "BASEBALL - Bat - Swing",
    "img" => "img/wire/103b-Baseball-Swing.jpg",
    "desc" => "Just hit ball",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);

I have a page that loads a single product from this array, and I'm trying to make "prev" and "next" buttons that will link to the adjacent products in the array. My PHP skills are rudimentary, and I've had no luck trying to accomplish this with the prev() and next() functions. What's the easiest way to find the adjacent elements in the array? (If I'm on the "Artist" page how would I link to, "Archery" and, "Badminton.")

Upvotes: 1

Views: 69

Answers (5)

Kostas Mitsarakis
Kostas Mitsarakis

Reputation: 4747

Based on your array data you can use the following function. I used the array_keys() function to make an array with only the keys of the initial array and all the work is done using the new array.

function custom_array_pagination($data = array()) {

    $current_page = 'Baseball-Bat-Swing';    //$current_page = $_GET['product']; //Change to this when you have set up your final code
    $data_keys = array_keys($data);  //This is the array all the work is done

    $s = '';

    if (!in_array($current_page, $data_keys)) {  //If there is no such element as the $_GET element in the array
        return $s;
    }

    $is_first = false;
    $is_last = false;
    $found_prev = false;
    $found_next = false;

    $next_text = '';

    if ($current_page == $data_keys[0]) {
        $is_first = true;
    }
    if ($current_page == end($data_keys)) {
        $is_last = true;
    }

    $s .= '<ul class="pagination">';

    if ($is_first) {    //If it is the first element then show only text
        $s .= '<li class="first">First'.'</li>';
    } else {
        $s .= '<li class="first"><a href="'.$data_keys[0].'">First</a>'.'</li>';
    }

    foreach($data_keys as $key => $value) {

        if ($is_first && !$found_prev) {   //If it is the first element then show only text
            $found_prev = true;
            $s .= '<li class="prev">Prev'.'</li>';
        } else {
            if (!$found_prev) { //If prev has not been found yet
                $prev = $data_keys[array_search($current_page, $data_keys) - 1];
                $found_prev = true;
                $s .= '<li class="prev"><a href="'.$prev.'">Prev</a>'.'</li>';
            }
        }

        if ($current_page == $value) {
            $s .= '<li class="current">'.$data[$value]['name'].'</li>';
        } else {
            $s .= '<li class="current"><a href="'.$value.'">'.$data[$value]['name'].'</a>'.'</li>';
        }

        if ($is_last && !$found_next) {   //If it is the last element then show only text
            $found_next = true;
            $next_text = '<li class="next">Next'.'</li>';
        } else {
            if (!$found_next) { //If next has not been found yet
                if ($value == $data_keys[count($data_keys) - 1]) {    //If this value is the last value in the table
                    $found_next = true;
                    $next = $data_keys[array_search($current_page, $data_keys) + 1];
                    $next_text = '<li class="next"><a href="'.$next.'">Next</a>'.'</li>';
                }
            }
        }

    }

    $s .= $next_text;

    if ($is_last) { //If it is the last element then show only text
        $s .= '<li class="last">Last</li>';
    } else {
        $s .= '<li class="last"><a href="'.$data_keys[count($data_keys) - 1].'">Last</a>'.'</li>';
    }

    return $s;

}

You can use the function like this:

echo custom_array_pagination($products);

Upvotes: 1

Dennis St&#252;cken
Dennis St&#252;cken

Reputation: 1396

You cannot do next() and prev() on an associative array. What you need is another array structure, like this:

$products = array();

$products[0] = array(
    "name" => "Archery",
    "img" => "img/wire/100-Archery.jpg",
    "desc" => "Archer aiming to shoot",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);
$products[1] = array(
    "name" => "Artist",
    "img" => "img/wire/101-Artist.jpg",
    "desc" => "Artist with palette & easel",
    "prices" => array($price3,$price6),
    "paypal" => $paypal5,
    "sizes" => array($size1, $size2)
);
$products[2] = array(
    "name" => "Badminton",
    "img" => "img/wire/102-Badminton.jpg",
    "desc" => "About to hit bird above head",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);
$products[3] = array(
    "name" => "BASEBALL -Bat - Stance",
    "img" => "img/wire/103a-Baseball-Stance.jpg",
    "desc" => "Waiting for pitch",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);
$products[4] = array(
    "name" => "BASEBALL - Bat - Swing",
    "img" => "img/wire/103b-Baseball-Swing.jpg",
    "desc" => "Just hit ball",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);

You are still able to iterate through this structure and get the name of the current item from key "name".

Upvotes: 0

rawb
rawb

Reputation: 2426

You could traverse the array to find the current key, and then go one element more. A tested example:

$current_page = 'Artist'; // as an example

$prev = $next = false; // the keys you're trying to find

$last = false; // store the value of the last iteration in case the next element matches

// flag if we've found the current element so that we can store the key on the next iteration
// we can't just use $last here because if the element is found on the first iteration it'll still be false
$found = false;

foreach ($products as $key => $value) {

    // if we found the current key in the previous iteration
    if ($found) {
        $next = $key;
        break; // no need to continue
    }

    // this is the current key
    if ($key == $current_page) {
        $found = true;
        $prev = $last;
    }

    $last = $key; // store this iteration's key for possible use in the next iteration
}

At the end of this script, $prev and $next will either contain the key of the previous/next item or be false (if the current item is not found or we're at the very beginning/end of the array and no prev/next is available).

Upvotes: 1

IVAO CA-WM
IVAO CA-WM

Reputation: 73

Sounds like what you need is javascript. It will be able to do the switch the products on the client side, so you can store the variable from a for loop and when the button is clicked, you can call a function to switch it to the next one

Upvotes: 0

brute_force
brute_force

Reputation: 1171

array yp_next ( string $domain , string $map , string $key ) Returns the next key-value pair in the named map after the specified key.

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

Upvotes: 1

Related Questions