0

I don't really know how to approach this, thing is, I'm developing a web application and in a section I need to assign projects to other developers, every assignment/project will have a priority of how important it is. Priority 1 is the highest (more important), and priority 5 is lowest (less important).

What the system has to do is, when I add a new priority 1 (or any other priority), if there are other priorities and a priority 1, move the others down (P1 = P2, P2 = P3, P3 = P4) and add the new one as P1.

I made a little piece of code (making everything manually that will only work once but is just for you to see what I want)

//PHP CODE
//prioridad = "P1" from a button
$prioridad = validacion::limpiar_cadena($_POST['prioridad']);
$estado = "";
$pes = array();

//I get all the priorities from my user and save them in this array
//Saving an array of the user's priorities
while ($row = $resultado->fetch_assoc()){
    $pes[] = $row["prioridad"];
}

//Replace current priorities with their new one (just once)
if (in_array($prioridad, $pes)){
    if (in_array("P5", $pes)){
        $estado = "lleno";
    }
    
    //Make priority 4 = priority 5 and same for all
    //This user just had the first 3 priorities,so this one did nothing but the others updated succesfully just for this example
    if (in_array("P4", $pes)){
        $upd= $conexion->prepare("UPDATE asignarproyectousuario SET prioridad = 'P5' WHERE idUsuario = 1 AND idProyecto = 32");
        $upd->execute();
    }

    if (in_array("P3", $pes)){
        $upd= $conexion->prepare("UPDATE asignarproyectousuario SET prioridad = 'P4' WHERE idUsuario = 1 AND idProyecto = 2");
        $upd->execute();
    }

    if (in_array("P2", $pes)){
        $upd = $conexion->prepare("UPDATE asignarproyectousuario SET prioridad = 'P3' WHERE idUsuario = 1 AND idProyecto = 1");
        $upd->execute();
    }

    if (in_array("P1", $pes)){
        $upd= $conexion->prepare("UPDATE asignarproyectousuario SET prioridad = 'P2' WHERE idUsuario = 1 AND idProyecto = 3");
        $upd->execute();
    }

    $insert = $conexion->prepare("INSERT asignarproyectousuario(idProyecto, idUsuario, prioridad) VALUES(4, 1, ?)");
    $insert->bind_param("s", $prioridad);
    $insert->execute();
}

I tried using arrays and adding a value of one to the current priority but I don't know how to make it work, separate the array and assign every value to a row in the database.

I also found queues that make exactly that "movement" of adding one priority and move the others in order, but I haven't found much documentation about it.

This is the example I saw:

$queue = new SplQueue();
$queue->enqueue('prioridad1');
$queue->enqueue('Prioridad2');
$queue->enqueue('Prioridad3');

$queue->unshift('prioridad1');
    
$queue->rewind(); // always rewind the queue/stack, so PHP can start from the beginning.
    
while($queue->valid()){
     echo $queue->current()."\n"; // Show the first one
     $queue->next(); // move the cursor to the next element
}
echo "\n"."\n"."\n";
var_dump($queue);

If you could give me an idea of how to do it or a different example would be very helpful. Thanks in advance and if my english is not good enough I can try to explain it better.

marco
  • 1
  • 1

1 Answers1

0

technique is simple > you need to know how JSON works add an Extra column (text) in mysql Table > store JSON array in that column Like > [{"TaskName":"XYX","AssignDate":"AnyDate","Priority":"High","PriorityCount":"50"}]

add extra array in JSON / Update array whenever require

itarate the array that finds ("Priority":"High" AND "PriorityCount": "" // highest)

this might help > Getting max value(s) in JSON array

Shubham Dange
  • 173
  • 13
  • Thank you for your answer, I'll check it out and post later. – marco May 04 '21 at 05:31
  • I had the same problem once, don't use extra columns and updates, it will make more confusion in code upvote it and another useful link https://stackoverflow.com/questions/16184047/how-to-add-item-to-the-json-file-formatted-array – Shubham Dange May 04 '21 at 05:37