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

rxm
  • 71
  • 2
  • 11
  • 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 – Davis Mar 18 '21 at 03:55
  • 1
    @Davis Would there not be an option **without** using Redis then as I'm unfamiliar with it? – rxm Mar 18 '21 at 04:07
  • [redis](https://redislabs.com/ebook/part-1-getting-started/chapter-1-getting-to-know-redis/1-2-what-redis-data-structures-look-like/1-2-2-lists-in-redis/) | [redis for php](https://www.programmersought.com/article/25402722288/) It's simple and important! – Davis Mar 18 '21 at 06:24

0 Answers0