1

I have a Circular Queue I've made using PHP with all of the generic functions you'd see like dequeue, enqueue, peek etc in a file queue.php

I am trying to submit form data using AJAX that has been pre-sanitised to a file save.php with the queue.php included in the file.

queue.php

#prior queue code omitted as it is not relevant to the question
public function enqueue(string $element) { 
      $this->queue[$this->rear] = $element; 
      $this->rear = ($this->rear + 1) % $this->limit; 
  } 
} 

save.php

include("queue.php");
$queue = new CircularQueue(5);

if (isset($_POST['data'])){
    try{
         $queue->enqueue($_POST['data']); 
         echo $queue->peek();
         echo print_r($queue);
    } catch (Exception $e){
         echo $e->getMessage();
    }
}

It successfully manages to enqueue one POST data but any successive AJAX will reset the array and keep storing it as the first index.

e.g. First AJAX data: 20
[0] ==> 20
Second AJAX data: 285
[0] ==> 285

I have checked my queue and it runs as intended when enqueueing in separate lines so the issue is in the save.php file.

Aim: I want any data sent to this save.php file using AJAX to be enqueued accordingly.

e.g. First AJAX data: 20
[0] ==> 20
Second AJAX data: 285
[0] ==> 20 [1] ==> 285

3
  • Because the data is not resident in memory, the data requested by the two Ajax will not be stored in memory. It is recommended to use redis list to implement queue Commented Mar 18, 2021 at 3:55
  • 1
    @Davis Would there not be an option without using Redis then as I'm unfamiliar with it? Commented Mar 18, 2021 at 4:07
  • redis | redis for php It's simple and important! Commented Mar 18, 2021 at 6:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.