Portál AbcLinuxu, 8. května 2025 15:45
mám proměnné pole=array['0položka','1položka','2položka','3položka','4položka','5položka','6položka','7položka','8položka','9položka'] $aktualni_ID=0 $max_polozek_na_stranku=3 $celkem_polozek=10 for(i=$aktualni_ID;i=$max_polozek_na_stranku; i++ ) if($aktualni_ID==i) echo pole[i].'<<<' else echo pole[i] Kroky směrem dolů 0položka<<< 1položka 2položka 0položka 1položka<<< 2položka 0položka 1položka 2položka<<< 1položka 2položka 3položka<<< 2položka 3položka 4položka<<< 3položka 4položka 5položka<<< 4položka 5položka 6položka<<< 5položka 6položka 7položka<<< 6položka 7položka 8položka<<< 7položka 8položka 9položka<<< Kroky zpět (směrem nahoru) 7položka 8položka 9položka<<< 7položka 8položka<<< 9položka 7položka<<< 8položka 9položka 6položka<<< 7položka 8položka 5položka<<< 6položka 7položka 4položka<<< 5položka 6položka 3položka<<< 4položka 5položka 2položka<<< 3položka 4položka 1položka<<< 2položka 3položka 0položka<<< 1položka 2položka
<?php // (C) 2022, Dušan Kreheľ // Licence: MIT class MenuBox { private $items=array(); private $items_count=array(); private $max_items=10; private $position=0; /* index */ private $box_first=0; /* index */ function __construct($items, $draw_max_items) { $this->items=$items; $this->max_items=$draw_max_items; $this->items_count=count($items); } function down() { if($this->position == ($this->items_count-1)) return; $this->position++; if(abs($this->position-$this->box_first) == $this->max_items) $this->box_first++; } function up() { if($this->position == 0) return; if($this->position == $this->box_first) $this->box_first--; $this->position--; } function draw() { $count_steps=$this->max_items + $this->box_first; for($i=$this->box_first;$i<$count_steps;$i++) if($i == $this->position) echo " \033[35m".$this->items[$i]."\033[0m\n"; else echo " ".$this->items[$i]."\n"; echo "\n"; } } $items=array("A","B","C","D","E","F","G"); $box= new MenuBox($items, 4); $box->draw(); $box->down(); $box->draw(); $box->down(); $box->draw(); $box->down(); $box->draw(); $box->down(); $box->draw(); $box->down(); $box->draw(); $box->down(); $box->draw(); echo "hore dalej\n"; $box->up(); $box->draw(); $box->up(); $box->draw(); $box->up(); $box->draw(); $box->up(); $box->draw(); $box->up(); $box->draw(); $box->up(); $box->draw();
<?php // (C) 2022, Dušan Kreheľ // Licence: MIT class MenuBox { private $items=array(); private $items_count=-1; private $max_items=10; private $position=0; private $box_first=0; function __construct($items, $draw_max_items) { $this->items=$items; $this->max_items=$draw_max_items; $this->items_count=count($items); } function down() { if($this->position == ($this->items_count-1)) return; $this->position++; if(abs($this->position-$this->box_first) == $this->max_items) $this->box_first++; } function up() { if($this->position == 0) return; if($this->position == $this->box_first) $this->box_first--; $this->position--; } function draw() { $count_steps=$this->max_items + $this->box_first; for($i=$this->box_first;$i<$count_steps;$i++) if($i == $this->position) echo " \033[35m".$this->items[$i]."\033[0m\n"; else echo " ".$this->items[$i]."\n"; echo "\n"; } function set_position($index_position, $index_box_first_line) { $this->position=$index_position; $this->box_first=$index_box_first_line; } } $items=array("A","B","C","D","E","F","G"); $box= new MenuBox($items, 4); $box->set_position(2, 1); $box->draw(); $box->down(); $box->draw();
$box->set_position(2, 1);tak tu hodnotu 1 musíš vypočítat ručně, ne? Myslel jsem spíš volat jen s jednou hodnotou a tu druhou by si vypočítal sám.
$box->set_position(2);Omlouvám se, ale nevím jestli se vyjadřuji k pochopení.
first = count - lines if sel < first: first = sel - (lines // 2) if first < 0: first = 0kde first je index první položky menu, count je počet položek menu, lines je počet řádek menu
#!/usr/bin/env python3 def draw(sel, lines): menu = ('a', 'b', 'c', 'd', 'e', 'f') count = len(menu) first = count - lines if sel < first: first = sel - (lines // 2) if first < 0: first = 0 print('-------------') for line in range(lines): item = line + first print(menu[item], '<<<' if item == sel else '') for sel in range(7): draw(sel, 3)výstup je:
$ python3 menu.py ------------- a <<< b c ------------- a b <<< c ------------- b c <<< d ------------- d <<< e f ------------- d e <<< f ------------- d e f <<< ------------- d e f
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.