Php“继续”点击

I have some values in an array. First, I want to show my first two values of that array in my view and next I want to show remaining values after certain button click action with incrementing array index by 1. for example: one click shows 3rd value then another click shows 4th .

How can I achieve that ??

$array=[1,2,3,4,5,6,7,8,9];
foreach($array as $a)
{ 
if($a>2)
continue;
echo $a.'<br/>';
}

for the first time i want the view like

1
2

and after that once button is clicked i need to show 3 then 4 then 5 . . .

That is not how PHP works.

A PHP script is runned on the server to build your web page. Once you have it, you would need to rebuild your page, according to the new parameters (recived by GET or POST), by asking the server again.

It would look like that:

<?php

    $array=array(1,2,3,4,5,6,7,8,9); //That is how you declare an array in PHP

    if(empty($_GET["count"])){ //It's the first time on the page, show the first two elements
        echo $array[0] . ' ' . $array[1];
        echo '<br><a href="page.php?count=1">Next</a>'; //Send to the same page, but now send the count by GET
    }

    else{ //It's not the first time on the page, keep track of how many times the link was clicked
        $count = $_GET["count"];
        echo $array[$count] . ' ' . $array[$count+1];
        $count++;
        echo '<br><a href="page.php?count=' . $count . '">Next</a>';
    }

?>

But you should not be doing stuff like this. If you want to interact with the user, changing your page dinamically after it's contructed you need to look for Javascript.

As Jack and Y U NO WORK mentioned, you can use AJAX.

With Ajax, you will have to differ between two cases:

  1. Your page including your button and view is rendered the first time
  2. Your AJAX-Code will get a list of values that will replace the old list of values in the view.

Therefore, you will end up with two URLs:

  1. www.mywebpage.com/app/index.php --> This will deliver the full page
  2. www.mywebpage.com/app/index.php?action=getNewValues&start=3&to=7 --> This will be treated as AJAX call and deliver, maybe, a JSON with values from 3 to 7.

Now you know how to organize your PHP command handler. Something like

...
$action = $GET['action'];
...
if ($action == 'getNewValues') {
    ...
    ...
    echo $resultJson;
    exit;

} else if ... {

    ...

} else {

   $myPageRenderer->renderFullPage();

}

You will find many explanations for jQuery.ajax()-calls, just go on :-) Here is a good start http://api.jquery.com/jQuery.ajax/

Remember to disable your next button when you are at the end of the list and vice versa.

There is a second approach, namely to get the full list of values while rendering the page the first time. Then, the javascript button click will just choose the new value from the existing list.

Therefore, you can write with PHP in the HTML head part:

<script type=text/javascript>
   myDataIsland = {
     myListOfValues : <?php echo $myList; ?>
   };
</script>

Like this, you will get your list onto your page in the first place. Having all values available, the Javascript event code can just choose the next values.

Here, a good approach is to write a string first that represents a JSON, and on $(document).ready() to render the JSON list to a JSON object. Like this, your page will not crash if PHP delivers some rubbish.