Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign a value only for first time

Tags:

php

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

This is how 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 is like 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 the submit button data go to the functions.php file and comes back to the dashboard.php then again $i becomes NULL. can I assign NULL only for first time.if yes,How can I do that.

like image 542
isuruAb Avatar asked Jan 24 '16 16:01

isuruAb


1 Answers

store $i in session and load it in every request, if it's not set in $_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>
like image 116
Farnabaz Avatar answered Oct 30 '22 06:10

Farnabaz