How to assign a value only for the first time

In my web system, I have 3 files: database.php, functions.php, dashboard.php

This is what my dashboard.php file looks like

<?php $i=NULL; if(isset($_POST['next'])) { $i=getQuizes($i); } ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input name="next" value="Next" type="submit"> </form> 

functions.php as below

 function getQuizes($quizNo) { if($quizNo==NULL) { $quizNo=0; } include_once('database.php'); $sql = "SELECT * FROM quiz LIMIT ".$quizNo.",1"; $result = $conn->query($sql); while($row=$result->fetch_assoc()) { echo $row['question'],$quizNo; } $quizNo++; return $quizNo; } 

when I clicked on the submit button data, go to the functions.php file and go back to dashboard.php, then again $ i will become NULL. I can only assign NULL for the first time. If so, how can I do this.

+7
php
source share
1 answer

save $i in the session and load it in each request, if it is not set to $_SESSION set $i to NULL

 <?php $i = isset($_SESSION['next']) ? $_SESSION['next'] : NULL; if(isset($_POST['next'])) { $i = getQuizes($i); $_SESSION['next'] = $i; }?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input name="next" value="Next" type="submit"> </form> 
+3
source share

All Articles