Collect the months in HTML and PHP from a select

1

I am trying to develop a function to be able to collect the months in HTML and PHP from select . At the moment, what I have is:

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
	<?php

	$mesos = [ "gener" => "31", "febrer" => "28 dies, si és any de traspàs 29",
	"març" => "31", "abril" => "30", "maig" => "31", "juny" => "30",
	"juliol" => "31", "agost" => "31", "septembre" => "30",
	"octubre" => "31", "novembre" => "30", "desembre" => "31",  ];
	//Sino se clica el boton submit entonces...
	if (!isset($_POST['submit'])) {
		?>
		<!--Aquí he de crear la funcion que recoja el mes y mediante el select lo selecciones y te printee el mes con los años que tiene-->
		<form method="post" action="ex05-4.php">
			<p>Escull el mes :</p>
			<select name="mes">
				<!--Aquí he de implementar el select bien pero no se como -->
			</select> <p />
			<input type="submit" name="submit" value="Ves">
		</form>
		<?php
	//Si se envia bien el formulario entonces	
	} else {
		$mesos = $_POST['mesos'];
	}
	?>
</body>
</html>
    
asked by InfoTipsconJavitoBCN 23.10.2018 в 15:22
source

1 answer

1

Well, if I understood correctly now ...

INDEX.PHP

    <?php 
//Esta funcion añade los options en el select recibiendo el array de los meses
function afegirOption($nomMesos){
    foreach($nomMesos as $nom){ 
        echo "<option name='mes' value='".$nom."'>".$nom."</option>";
    }
}
?>


<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <?php
    $nomMesos = [1=>"Gener", 2=>"Febrer", 3=>"Març", 4=>"Abril", 5=>"Maig", 6=>"Juny", 7=>"Juliol", 8=>"Agost", 9=>"Septembre", 10=>"Octubre", 11=>"Novembre", 12=>"Desembre"];



    //Sino se clica el boton submit entonces...
    if (!isset($_POST['submit'])) {
        ?>
        <!--Aquí he de crear la funcion que recoja el mes y mediante el select lo selecciones y te printee el mes con los años que tiene-->
        <form method="post" action="ex05-4.php">
            <p>Escull el mes :</p>
            <select name="mes">
                <!-- llamamos a la función y le enviamos el array -->
                <?php afegirOption($nomMesos); ?>
                <!--Aquí he de implementar el select bien pero no se como -->
            </select> <p />
            <input type="submit" name="submit" value="Ves">
        </form>
        <?php
    //Si se envia bien el formulario entonces   
    } else {
        $mesos = $_POST['mesos'];
    }
    ?>
</body>
</html>

ex05-4.php

<html>
<body>

<?php calcularData() ?>

</body>
</html>

<?php
function calcularData(){
    $nomMesos = [1=>"Gener", 2=>"Febrer", 3=>"Març", 4=>"Abril", 5=>"Maig", 6=>"Juny", 7=>"Juliol", 8=>"Agost", 
                                9=>"Septembre", 10=>"Octubre", 11=>"Novembre", 12=>"Desembre"];
    $any = date("Y");
    $calcularData = $any."-".array_search($_POST["mes"], $nomMesos)."-01";
    echo "El mes ".$_POST["mes"]." te ".date('t',strtotime($calcularData)). " dies."; 
}
?>

There are several ways, but I think the simplest is this, in addition to this mode, you do not have to calculate traspàs or not ... since it does so automatically. The variable $ mesos I have not used it at all, but you can adapt it to $ nomMesos for your exercise.

EDITO: I add functions in php, remember that the functions can be inside the html tags or outside them, depending on what you want to do, in this case, I have left you out. Now I hope I have helped you!

Option, all in one file:

<?php 
    $nomMesos = [1=>"Gener", 2=>"Febrer", 3=>"Març", 4=>"Abril", 5=>"Maig", 6=>"Juny", 7=>"Juliol", 8=>"Agost", 
                                9=>"Septembre", 10=>"Octubre", 11=>"Novembre", 12=>"Desembre"];
    //Esta funcion añade los options en el select recibiendo el array de los meses
    function afegirOption($nomMesos){
        foreach($nomMesos as $nom){ 
            echo "<option name='mes' value='".$nom."'>".$nom."</option>";
        }
    }
?>


<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
        <!--Aquí he de crear la funcion que recoja el mes y mediante el select lo selecciones y te printee el mes con los años que tiene-->
        <form method="post" action="">
            <p>Escull el mes :</p>
            <select name="mes">
                <!-- llamamos a la función y le enviamos el array -->
                <?php afegirOption($nomMesos); ?>
                <!--Aquí he de implementar el select bien pero no se como -->
            </select> <p />
            <input type="submit" name="submit" value="Ves">
        </form>
</body>
</html>
<?php 
    if (!empty($_POST)) {
        calcularData();
    }


    function calcularData(){
        $nomMesos = [1=>"Gener", 2=>"Febrer", 3=>"Març", 4=>"Abril", 5=>"Maig", 6=>"Juny", 7=>"Juliol", 8=>"Agost", 
                                9=>"Septembre", 10=>"Octubre", 11=>"Novembre", 12=>"Desembre"];
        $any = date("Y");
        $calcularData = $any."-".array_search($_POST["mes"], $nomMesos)."-01";
        echo "El mes ".$_POST["mes"]." te ".date('t',strtotime($calcularData)). " dies.";
    }
?>

If you have any doubt about the reasoning of the exercise, you can ask me and I can explain step by step why the whole code, do not hesitate to write!

Optional To not initialize $ nomMesos twice, above you can put:

global $nomMesos;
    $nomMesos = [1=>"Gener", 2=>"Febrer", 3=>"Març", 4=>"Abril", 5=>"Maig", 6=>"Juny", 7=>"Juliol", 8=>"Agost", 
                                9=>"Septembre", 10=>"Octubre", 11=>"Novembre", 12=>"Desembre"];

and then just below function calcularData(){ add global $nomMesos; and so you do not have to re-add $ nomMesos = [****].

    
answered by 23.10.2018 / 15:36
source