MichCAn
MichCAn

Reputation: 23

selecting a month in php and create a query

This is my situation:

i need to select a month from a list, then i need some results to show up based on that month. this is my code:

    <?php    
...    
...    
...    
     
        
    
echo "<form action='#' method='post'> ";    
echo "<select name='mes'>";     
echo "<option value='enero'>Enero</option> ";     
echo "<option value='febrero'>Febrero</option> ";     
echo "<option value='marzo'>Marzo</option> ";    
echo "<option value='abril'>Abril</option> ";
echo "<option value='mayo'>Mayo</option> ";
echo "<option value='junio'>Junio</option> ";
echo "<option value='julio'>Julio</option> ";
echo "<option value='agosto'>Agosto</option> ";
echo "<option value='septiembre'>Septiembre</option> ";
echo "<option value='octubre'>Octubre</option> ";
echo "<option value='noviembre'>Noviembre</option> ";
echo "<option value='diciembre'>Diciembre</option> ";
echo "</select>";
echo "<input type='submit' name='submit3' value='Seleccionar Mes' />";
echo "</form>";
//echo "<br>";
if(isset($_POST['submit3'])){
    $messeleccionado = $_POST['mes'];
    echo " El mes seleccionado fue: " .$messeleccionado;
    if($messeleccionado == "enero"){

        $result9 = mysqli_query($con,"SELECT *FROM equipos, clientes, marcas WHERE equipos.ncliente = clientes.ncliente AND equipos.marca = marcas.cat_id AND MONTH(equipos.fecha) = 1'");

    }
    if($messeleccionado == "febrero")
    {

        $result9 = mysqli_query($con,"SELECT *FROM equipos, clientes, marcas WHERE equipos.ncliente = clientes.ncliente AND equipos.marca = marcas.cat_id AND MONTH (equipos.fecha) = 2'");

    }
    echo "<br>";
    echo "<table border='1'>
<tr>
<th>Cliente</th>
<th>Marca</th>
<th>Modelo</th>
<th>IMEI</th>
<th>Plan</th>
<th>Fecha</th>
</tr>";

    while($row = mysqli_fetch_array($result9))
    {
        echo "<tr>";
        echo "<td>" . $row['nempresa'] . "</td>";
        echo "<td>" . $row['marca'] . "</td>";
        echo "<td>" . $row['modelo'] . "</td>";
        echo "<td>" . $row['IMEI'] . "</td>";
        echo "<td>" . $row['plan'] . "</td>";
        echo "<td>" . $row['fecha'] . "</td>";
        echo "</tr>";
        $contador = $contador+1;
    }
    echo "</table>";
}
echo "<br>";

...
...
...
?>

the problem if i ask the $resutl9 is null can anyone point me in the right direction? Also sorry for the code format but i couldnt quite get it, it is my first time asking a question here! Thanks!

Upvotes: 0

Views: 137

Answers (2)

Terry Carter
Terry Carter

Reputation: 300

Based on @Asperons answer, with the code to finish out the query, as well as a check to verify the posted month is indeed an array key in the months array.

The original code would have worked, so long as the single quotes issue was fixed, but this is a cleaner method which should answer the question as well.

$mesnumeros = array(
  "enero" => 1,
  "febrero" => 2,
  "marzo" => 3,
  "abril" => 4,
  "mayo" => 5,
  "junio" => 6,
  "julio" => 7,
  "agosto" => 8,
  "septiembre" => 9,
  "octubre" => 10,
  "noviembre" => 11,
  "dicembre" => 12
);

if(isset($_POST['submit3']) && array_key_exists($_POST['mes'], $mesnumeros)){
    $messeleccionado = $_POST['mes'];
    echo "El mes seleccionado fue: " .$messeleccionado;

    $query = mysqli_query($con,"SELECT e.*, c.*, m.* FROM equipos AS e, clientes AS c, marcas AS m WHERE e.ncliente = c.ncliente AND e.marca = m.cat_id AND MONTH(e.fecha) = {$mesnumeros[$messeleccionado]}");
    $results9 = mysqli_fetch_array($query);

    echo "<br>";
    echo "<table border='1'>
    <tr>
    <th>Cliente</th>
    <th>Marca</th>
    <th>Modelo</th>
    <th>IMEI</th>
    <th>Plan</th>
    <th>Fecha</th>
    </tr>
    ";

    foreach($results9 AS $result){
        echo "<tr><td>".$result['cliente']."</td><td>".$result['marca']."</td><td>".$result['modelo']."</td><td>".$result['imei']."</td><td>".$result['plan']."</td><td>".$result['fecha']."</td></tr>";    
    }

    echo "</table>";

}

Upvotes: 1

Architect Nate
Architect Nate

Reputation: 714

The bug fix @Terry mentioned is here as well, the main answer to your question is that you are missing a quote (or have added one).


Additional Details To Simplify Your Code

Following what @Dagon said, if you use the number of the month in the form instead of the name, you can then drop it into the query and not have an if statement for each query. However since you are using the name of the month in text other places you can also setup an array to handle this as well.

$mesnumeros = array(
  "enero" => 1,
  "febrero" => 2,
  "marzo" => 3,
  "abril" => 4,
  "mayo" => 5,
  "junio" => 6,
  "julio" => 7,
  "agosto" => 8,
  "septiembre" => 9,
  "octubre" => 10,
  "noviembre" => 11,
  "dicembre" => 12
);

if(isset($_POST['submit3'])){
    $messeleccionado = $_POST['mes'];
    echo " El mes seleccionado fue: " .$messeleccionado;

    $result9 = mysqli_query($con,"SELECT *FROM equipos, clientes, marcas WHERE equipos.ncliente = clientes.ncliente AND equipos.marca = marcas.cat_id AND MONTH(equipos.fecha) = {$mesnumeros[$messeleccionado]}");

    echo "<br>";
    echo "<table border='1'>
    <tr>
    <th>Cliente</th>
    <th>Marca</th>
    <th>Modelo</th>
    <th>IMEI</th>
    <th>Plan</th>
    <th>Fecha</th>
    </tr>";

    ...
    ...

Upvotes: 1

Related Questions