I am a PHP beginner and I am stuck on an assignment for school. I have to create 6 different drop down menus (num1 , num2 , num3 , num4 , rows , columns) each with the numbers 1 through 10 in it. I then have make sure the form is submitted, and only when it is submitted all perfectly, then I have to use the variables to create a table based on even and odd numbers.
<form method="post" action="prelab2.php">
<select name="num1">
<option>Pick one</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>
Above is the drop-down menu, now this is where I am really stuck.
<?php
$_POST['submit'];
$num_1=$_POST['num1'];
$num_2=$_POST['num2'];
$num_3=$_POST['num3'];
$num_4=$_POST['num4'];
$rows=$_POST['rows'];
$cols=$_POST['cols'];
?>
I figured out how to $_POST the variables, but how can I take these variables (only on submit) and turn it into a table?
Welcome to SO. $_POST['submit']
is not neccesary. In your PHP script, create the HTML for the table, and then <?php echo $num_1; ?>
(or the others) where needed. The HTML must be outside the PHP code.
A line like $_POST['submit'];
has no side effect... That is, it does nothing. You're just reading the variable value and not storing it anywhere.
What you're searching for seems to be echo
. It writes content to the output, in your case the HTML page.
In a template-style, you can use it this way:
<?php
// initial PHP code (reading form value, etc.)
$title = 'Foo';
// after the closing tag, starts the output
?>
<html>
<title><?php echo $title; ?></title>
<!-- remaining of your HTML -->
</html>
Notice the PHP echo
statement embeded into the HTML markup.
So now, you just have to compose your HTML page as intended, using your own variables...
Start here: http://www.w3schools.com/tags/tag_option.asp
<select>
<option value="n1">Volvo</option>
<option value="n2">Saab</option>
<option value="n3">Opel</option>
<option value="n...">Audi</option>
</select>
<?php $n1 = $_POST['n1']; ?>
if(isset($_POST['submit']))
{
//check if form is submitted
echo '<table style="border:colapse">'; //css trick to make nicer borders
//get post vars
$num_1=$_POST['num1'];
$num_2=$_POST['num2'];
$num_3=$_POST['num3'];
$num_4=$_POST['num4'];
//put number values in array
$nums=array($num_1,$num_2,$num_3,$num_4);
//sort array -> we will need it for outpu
sort($nums);
$rows=$_POST['rows'];
$cols=$_POST['cols'];
for($i=0;$i<$rows;$i++)
{
//print rows in loop
echo '<tr>';
for($j=1;$j<=$cols;$j++)
//now print colums -> starting from one!
{
echo '<td style="border:1px solid black;width:100px;height:20px">';
//modulo operator, to check if we are inside even or odd column
if($j % 2 == 0) {
foreach($nums as $num)
// loop thorough array of numbers
{
//if number is even -> print it
if($num % 2==0) {
echo $num;
}
}
}
else {
//if we are in odd row
foreach($nums as $num)
{
//print odd numbers
if($num % 2 !=0) {
echo $num;
}
}
}
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
}
P.S. I know that this could get many down votes, but... :)