Move array pointer?

Hi guys

I need to move an array pointer from another method.

Here are my codes

class Language extends MY_Controller
    public $fruits = array();

    function __construct()
        $this->fruits = array('apple', 'orange', 'mango', 'banana', 'guyabano');

	//show current array pointer position/value
    public function idx()
        $position = current($this->fruits); // $mode = 'foot';
        echo 'Current index ===> '. $position .'<br>';
	//Move array pointer one step forward
    public function move_idx()

I have a button called “Next”.
When this button was pressed it will call move_idx() method to move the pointer one step forward.

The problemis when I tested this it did not work.
The pointer did not move one step forward…

Would anyone like to help me?
What’s the proper way to do this?

Thanks in advance.

In PHP, there’s no such thing as redirect. However, if you are referring that you want to redirect a user, use the header() commands.

the redirect() is a codeigniter built-in method.

that’s not my problem.

How do I make the array pointer move one step forward every time I click the “Next” button I created.

The problem is much more complex than just moving array pointers around.

To get your code to work, you have to understand the nature of web-servers in general and the way how PHP works.

The moment you start pressing any buttons in the browser, there are no more array pointers, nor arrays, nor whole php instance ever exists. PHP-based web-server is stateless. Means PHP script, that has been used to display your page, is already died. While when pressing a button after that, you are invoking a completely different PHP instance, with its own arrays, pointers and stuff, that knows absolutely nothing of any previous calls.

Therefore, if you want to keep any state with you, you have to pass all the information to the browser. In your case - send the array position in the browser along with request and then send it back to PHP.

thus you have to get the current array position (initially it’s 0), and attach it to the button. Having received it through GET request you may add 1 to it and get the next position, like

$index = $_GET['index'] + 1;
$item = $this->fruits[$index];

thanks, but I think I already figured this out.
I will save the index into session.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.