My method or function recurseMenu
(in: UssdNode.php
) normally contains the list of menus (WHICH I LOOK TO DISPLAY) contained in the variable $title=$node->getTitle()
:
function recurseMenu($items,$bufferLimit) {
$objectString="<strong>". $this->getTitle() . "</strong>" . PHP_EOL;
$lastMenu=false;
var_dump($items); //DEBUG "items"
if(count($items)>0) {
for ($i = $this->index; $i <= $bufferLimit; $i ) {
if ($items[$i]) {
$item = $items[$i];
$num = $i 1;
// get node by name
$userSessions = $_SESSION['userSessions'];
$currUserSession = $userSessions[$this->address];
$node = $currUserSession->getNode($item);
$title = $node->getTitle();
$objectString .= PHP_EOL . $num . '. ' . $title;
}
}
} else {
$objectString=$objectString.PHP_EOL . 'NO DATA AVAILABLE, TRY AGAIN LATER';
}
$lastMenu=$bufferLimit==count($items);
$objectString=$objectString . PHP_EOL . PHP_EOL . "<strong>0. Exit</strong>";
if($this->getParent() != '0'){
$objectString=$objectString . PHP_EOL . "<strong>#. Back</strong>";
}
if($lastMenu===false){
$rem=count($items)-$this->index;
$objectString=$objectString . PHP_EOL . "<strong>99. Next (".$rem.")</strong>";
}
return $objectString;
}
The getNode
method of the UssdTree.php
file:
function getNode($name){
$node=$this->treeMenu[$name];
return $node;
}
And when I try the debug of $this->recurseMenu($items,$bufferLimit)
in my toString
method or function which displays the list of $title
as strings:
function toString(){
$objectString='';
$items=$this->children;
$bufferLimit=(count($items)==0)?1:$this->getBufferLimit() 1;
echo "<pre>";
print_r($this->recurseMenu($items,$bufferLimit));
echo "</pre>";
do{
$bufferLimit-=1;
$objectString=$this->recurseMenu($items,$bufferLimit);
}while(strlen($objectString>160));
$this->index=$bufferLimit;
return $objectString;
}
So, the problem is that nothing is displayed. And I get the following error:
- Line 86 of the error I get and put below is from the
UssdNode.php
file which contains the statement bytrue
of the variableif ($items[$i])
in therecurseMenu
method below:
Notice: Undefined offset: 4 in C:\laragon\www\ussd\ussdmenu-server-php\UssdNode.php on line 86
When I the debug of var_dump($items);
at the line 3 in my recurseMenu
method or function above, I get this return:
array(4) {
[0]=>
string(5) "umeme"
[1]=>
string(4) "nwsc"
[2]=>
string(5) "paytv"
[3]=>
string(10) "paycurrent"
}
And when I edit $bufferLimit
variable by removing the addition 1
at $this->getBufferLimit() 1
in the declaration of the variable $bufferLimit=(count($items)==0)?1:$this->getBufferLimit() 1;
of the toString
function, the menus are displayed fine but it creates another lag when pressing or issuing the request for the 99. Next
key.
How to correctly display the list of menus represented by the variable $title=$node->getTitle();
in the recurseMenu
method below ?
Help me fix this error.
CodePudding user response:
The displayed error says the following:
The array $items
has four items, accessible by the keys 0, 1, 2 and 3
. But you are asking for key 4
. This key is not there. I don't know what to do, so I am giving you an error message.
You now have two possible ways to go.
- Make sure that the index (
$i
) is never greater than the highest index of the array.
Maybe your logic with calculating $bufferLimit
is wrong, or maybe the loop is not correct. Maybe $i <= $bufferLimit
needs to be $i < $bufferLimit
.
- Check first to see if the key you are using is actually present in the array https://www.php.net/manual/en/function.array-key-exists.php
if ($items[$i])
would need to change to if (array_key_exists($i, $items))
That way you could keep your current logic of calculating $this->index
and $bufferLimit
, but if the asked key is not there, the if-statement will end up being false and not throw an error.