Select First, Next, Prev, Last Array using buttons

<html>
<head>
	<title>First, Next, Prev, Last Array buttons</title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	<style>
		body { font-size: 24px; font-weight: bold; }
		font-family: Arial Rounded MT Bold; font-weight: bold; Font-size: 95%; 
		form { margin-top: 10px; margin-left: -90px; font-size: 14px; font-weight: bold; }
		.buttons tr td { margin-top: -40px; vertical-align: top; padding: 2px; }
		.total { position: absolute; top: 250px; margin-left: 49%; font-size: 24px; border: 2px solid red; padding: 3px;}
		.descr { position: absolute; top: 300px; margin-left: 38%; font-size: 18px; border: 2px solid red; padding: 3px;}
		.probl { position: absolute; top: 350px; margin-left: 32%; font-size: 18px; border: 2px solid red; padding: 3px;}
	</style>
	<?php
		//----------------------------------- Create Array ------------------------------------
		$Lots = array('3', '11', '80', '110', '122', '144', '180', '240', '280', '364', '413');
		
		if(isset($_POST['first'])){ $nextrec= current($Lots); } 
		if(isset($_POST['next'])) { $nextrec = next($Lots); } 	
		if(isset($_POST['prev'])) { $nextrec = prev($Lots); }
		if(isset($_POST['last'])) { $nextrec = end($Lots); } 
		echo '<span class="total">'.'&nbsp;&nbsp;'.$nextrec.'&nbsp;&nbsp;'.'</span><br><br>';
		echo '<span class="descr">'.'ARRAY = 3  11 80  110  122  144 180  240  280 364  413'."</span>";
		echo '<span class="probl">'.'Why does the Next only get the second item and the Prev gets nothing?'."</span>";
	?>
</head>
<body>
<center>
<h1>First, Next, Prev, Last Array buttons</h1><br>
<form class="no-print" name="Lots" method="post">	
	<table class="buttons"><tr>
	<td>		
		<input type="hidden"  name="nextrec" value="<?php echo $next;?>"/>	
		<span class="icon-input-btn">
		<input class="btn btn-primary " style="font-size : 20px; font-weight: bold;" name="first" value="&nbsp;&nbsp;First&nbsp;&nbsp;" align="center" type="submit">
		</span> 
		<span class="icon-input-btn">
		<input class="btn btn-primary" style="font-size : 20px; font-weight: bold;" name="prev" value="&nbsp;&nbsp;Prev&nbsp;&nbsp;" align="center" type="submit">
		</span> 
		<span class="icon-input-btnr">
		<input class="btn btn-primary" style="font-size : 20px; font-weight: bold;" name="next" value="&nbsp;&nbsp;Next&nbsp;&nbsp;" align="center" type="submit">
		</span> 
		<span class="icon-input-btnr">
		<input class="btn btn-primary" style="font-size : 20px; font-weight: bold;" name="last" value="&nbsp;&nbsp;Last&nbsp;&nbsp;" align="center" type="submit">
	</td>
	</td></table>
</form>
</center>
</body>
</html>

Web servers are ‘stateless’. The only thing a web server knows on any request for a page are the inputs it receives with that request.

The array pointer that the current(), next(), and prev() functions use, starts over on each page request.

You are essentially doing pagination with one data item per page. The array index would be one less than the page number ( index = (requested page number - 1) * items per page ). The pagination links, which can be buttons, should submit the page number that you want that link to correspond to. The ‘first’ page link should always submit page=1. The ‘last’ page link should always submit page=count($Lots) (technically this would be ceil(count($Lots)/items per page) ). The ‘prev’ and ‘next’ links should submit one less than and one more than the current page number, except when you are on the first page there is no ‘prev’ link, and on the last page there is no ‘next’ link.

Sponsor our Newsletter | Privacy Policy | Terms of Service