I have a two-dimensional array with which I want to create a drop-down select in which, when choosing one of the different options, it returns a table with the data given by the chosen option, like this:
Code:
<?php
$arrayResultados = array
('Libre'=> array('No Presentados'=>12,'Suspenso'=>10,'Aprobado'=>6,'Notable'=>3,'Sobresaliente'=>1),
'On-line'=> array('Suspenso'=>26,'Aprobado'=>25,'Notable'=>22,'Sobresaliente'=>27),
'Presencial'=>array ('Suspenso'=>30,'Aprobado'=>25,'Notable'=>37,'Sobresaliente'=>28,'Matrícula de Honor'=>9)
);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form method="POST">
<select name="desplegable">
<option value="todas" selected="selected">Todas</option>
<?php
foreach ($arrayResultados as $indice => $valor)
{
echo "<option value='".$indice."'>".$indice."</option>";
}
?>
</select>
<input type="submit" name="envio" value="Aceptar">
</form>
<?php
if(isset($_POST["envio"]))
{
$listaAlum=[];
foreach ($arrayResultados as $indice => $valor)
{
foreach ($valor as $detalles => $numero)
{
if(isset($indice[$_POST["desplegable"]]))
{
$listaAlum[$detalles] = $numero;
}
}
}
echo "<h3>Seleccion: ".$_POST["desplegable"]."</h3>";
echo "<pre>";
print_r ($listaAlum);
echo "</pre>";
}
?>
</body>
</html>
This part of code: "if(isset($indice[$_POST["desplegable"]]))" doesn't work for me and if I don't remove it from the code it doesn't save anything in the array.
I can't get it to show me all the values when I choose the "all" option, I get several errors (Undefined index:) and it adds the numerical values more times than count. What I do in the following code is an if where if I choose the "all" option, add the sum of the numeric values to the array $listaAlum.
if($_POST["desplegable"]=="todas") //cuando elija la opcion todas
{
foreach ($arrayResultados as $indice => $valor)
{
foreach ($valor as $detalles => $numero)
{
$listaAlum[$detalles] = $numero;
}
}
}
How should I put it?
When I give all of them this is the result it gives me, I don't understand why it gives me that result (they are the values of the last value of the array) nor why it doesn't save the value "Not presented".
These kinds of things should always be resolved on the client . The function of the server is to give you the data that resides there, but once with the data, the dynamic changes must be worked on in the client.
What is the reason for this? That if you reflect the dynamic changes on the server, every time there is a change you will have to return to the server to reflect those changes without any need , and going to the server has a very high cost.
Imagine it like this, so that it is understood: the data is on the server (in Australia) and you are in Paris (the client). Every time the
select
change in the client (in Paris) you pay for a Paris-Australia plane ticket to go to Australia to compare data that you already have on hand in Paris. Isn't that absurd? If you have the data in Paris, don't go to Australia to check the data you already have! You're going to end up broke!Another thing is that some of these data changes, having a different value on the server... then you should go back to the server to look for the new data. But this is not the case here,
$arrayResultados
it has some fixed data that, once received in the client, you can work with.This code, contrary to what you may think, does not burden anyone, but fairly distributes responsibilities. Imagine an application with millions of users, all those millions of users generating Rest of World-Australia air traffic and calculating in Australia everything that should be calculated in the rest of the world. Anyway... a disaster.
Apart from that, the code works much faster being distributed. The Australia server could end up going down with so much traffic.
Look at the proposed solution, working the logic from the client. Here, the variable
mData
would represent the data that would come in$arrayResultados
, when the server provides you with that data it will enter rest, and it will be up to each client to work. This is how modern apps work.I update with the complete solution: I have replaced the part of the isset so that when choosing an option it checks if the chosen option is in the array, if it is I add to the array "$listaAlum" all the values of the chosen option.
For the "all" option, I simply go through the array and save the categories in another array and then go through it and accumulate all the students in each category.