/ Gists / Move in array
On gists

Move in array

PHP

list.php Raw #

<?php

$states = ['Přijatá', 'Nová', 'K zaplacení', 'Zaplaceno', 'Expedováno', 'Doručeno'];
$currentState = isset($_GET['state']) ? $_GET['state'] : 'K zaplacení';

$totalStates = count($states);
$currentIndex = array_search($currentState, $states);
$prev = null;
$next = null;
$prevAll = [];
$nextAll = [];

foreach ($states as $index => $state) {

    if ($index < $currentIndex) {
        $prev = $state;
        $prevAll[] = $state;
    }

    if ($index > $currentIndex && $next === null) {
        $next = $state;
    }

    if ($index > $currentIndex) {
        $nextAll[] = $state;
    }
}

$meta = [
    'prev' => $prev, // predchozi stav vuci aktualnimu ...
    'prevAll' => $prevAll, // vsechny predchozi vuci aktualnimu  ...
    'next' => $next, // nasledujici stav vuci aktualnimu
    'nextAll' => $nextAll, // vsechny nasledujici po aktualnim
    'currentStateNumber' => $currentIndex, // aktualni cislo stavu, K zaplaceni => 2, Doručeno => 6 atd.
    'totalStates' => $totalStates, // celkovy pocet stavu, tj. 6
];


echo '<pre>';
print_r($meta);

listByGpt.php Raw #

<?php 

$states = ['Přijatá', 'Nová', 'K zaplacení', 'Zaplaceno', 'Expedováno', 'Doručeno'];
$currentState = $_GET['state'] ?? 'K zaplacení';

$currentIndex = array_search($currentState, $states);
$prevAll = array_slice($states, 0, $currentIndex);
$prev = end($prevAll) ?: null;
$nextAll = array_slice($states, $currentIndex + 1);
$next = $nextAll[0] ?? null;

$meta = [
    'prev' => $prev,
    'prevAll' => $prevAll,
    'next' => $next,
    'nextAll' => $nextAll,
    'currentStateNumber' => $currentIndex + 1,
    'totalStates' => count($states),
];

echo '<pre>';
print_r($meta);