Home > OS >  How to correct the following error in my PHP code to display "$title" list contains?
How to correct the following error in my PHP code to display "$title" list contains?

Time:08-17

  • Line 83 of the error I get and put below is from the UssdNode.php file which contains the declaration of the variable $item=$items[$i] in the recurseMenu method below.

  • Line 45 of the same error in the UssdTree.php file contains the declaration of the variable $node=$this->treeMenu[$name]; in the getNode method below.

  • Line 94 of the same error in the UssdNode.php file contains the declaration of the variable $title=$node->getTitle(); in the recurseMenu method below.

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;
    if(count($items)>0) {
        for($i=$this->index;$i<$bufferLimit;$i  ){
            $item=$items[$i];

            /* echo "<pre>";
            print_r($item);
            echo "</pre>"; */

            $num=$i 1;
            //get node by name
            $userSessions=$_SESSION['userSessions'];
            $currUserSession=$userSessions[$this->address];
            $node=$currUserSession->getNode($item);
            $title=$node->getTitle();
            $objectString=$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;
}

The problem is that nothing is displayed. And I get the following error:

Notice: Undefined offset: 3 in C:\laragon\www\ussd\ussdmenu-server-php\UssdNode.php on line 83

Notice: Undefined index: in C:\laragon\www\ussd\ussdmenu-server-php\UssdTree.php on line 45

Fatal error: Uncaught Error: Call to a member function getTitle() on null in C:\laragon\www\ussd\ussdmenu-server-php\UssdNode.php:94

Stack trace:

#0 C:\laragon\www\ussd\ussdmenu-server-php\UssdNode.php(59): UssdNode->recurseMenu(Array, 4)
#1 C:\laragon\www\ussd\ussdmenu-server-php\UssdUserSession.php(77): UssdNode->toString()
#2 C:\laragon\www\ussd\ussdmenu-server-php\UssdReceiver.php(51): UssdUserSession->fetchDisplay()
#3 C:\laragon\www\ussd\ussdmenu-server-php\UssdReceiver.php(43): UssdReceiver->handleChildBearingNode('paybill', '0772247408', '1234567')
#4 C:\laragon\www\ussd\ussdmenu-server-php\UssdReceiver.php(15): UssdReceiver->handleContinuingRequests('0772247408', '4', '1234567')
#5 C:\laragon\www\ussd\receiver.php(28): UssdReceiver->onMessage(Array)
#6 C:\laragon\www\ussd\receiver.php(36): MyUssdReceiver::process(Array)
#7 {main} thrown in C:\laragon\www\ussd\ussdmenu-server-php\UssdNode.php on line 94

URL : http://localhost/ussd/receiver.php MSISDN :

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:

Just check for the index before accessing it?

 for ($i = $this->index; $i < $bufferLimit; $i  ) {
            if(isset($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;
            }
        }

or loop the items and check if there's more than the buffer or not

foreach ($items as $i => $item) {
            if ($i >= $bufferLimit){
                break;
            }
            $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;
        }

you're trying to access a key that doesn't exist. If your items array has 3 entries and the buffer limit is something like 10, you'll get errors starting from number 3 because they don't exist in the array.

CodePudding user response:

This work fine:

foreach ($items as $i => $item) {
            if ($i >= $bufferLimit){
                break;
            }
            $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;
        }

Thanks

  •  Tags:  
  • php
  • Related