I need help creating tables in php. Here is the code that I have right now. I have the correct html code I just need help with my php code.
<html>
<head/>
<body>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>" >
<table border="1">
<tr><td>Number of Rows:</td><td><input type="text" name="rows" /></td></tr>
<tr><td>Number of Columns:</td><td><select name="columns">
<option value="1">1</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
<option value="16">16</option>
</select>
</td></tr>
Operation:Multiplication
Addition
<?php
if(isset($_POST["submit"])){
//check to see if num of rows is numeric
if ( isset($_POST["rows"]) && is_numeric($_POST["rows"])){
//check to see if rows is a positive number
if($_POST["rows"] > 0){
//if the multiplication button is checked do this
if(isset($_POST) && $_POST["operation"] == "multiplication"){
//start the table and post what type of table it is
echo 'This is a '. $_POST["rows"] . ' x ' . $_POST["columns"] .'multiplication table';
echo "<table border=1";
echo'<tr>';
//post the first row
for($b = 0; $b <= $_POST["columns"];$b++){
echo '<th>'.$b.'</th>';}
echo '</tr>';
//nested for loops to finish the table
for($r = 1; $r <= $_POST["rows"]; $r++){
echo'<tr><th>'.$r.'</th>';
for($c = 1; $c <= $_POST["columns"]; $c++){
echo '<td>' .$c*$r. '</td>';
You have to run the query using a function like mysqli::query($query);. This function returns some content which you can then fetch/display with some other functions. But remember to connect to the database first and that stuff...
I'd recommend you to read some guides about mysqli first ;) Then it's quite self-explanating.
Related
I have this loop coded to create a dropdown with the numbers 0-30.
<td style="text-align:left;">
<select id="iPhone12Case" name="iPhone12Case">
<?php
for ($i=0; $i<=30; $i++) {
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
</select>
</td>
I cannot seem to figure out how to get the value of I to work in my calculations on an order processing form.
I assign the variable using
$iPhone12 = htmlspecialchars($_POST['iPhone12Case']);
but if I try to output $iPhone12 * 15 for example the answer is always zero.
Make sure you select is wrapped in a form element with method="POST". You should also use isset to make sure it exists.
<form action="" method="post">
<td style="text-align:left;"><select id="iPhone12Case" name="iPhone12Case">
<?php
for ($i = 0; $i <= 30; $i++) {
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
?>
</select></td>
<button>submit</button>
</form>
<?php
if (isset($_POST['iPhone12Case'])) {
$iPhone12 = htmlspecialchars($_POST['iPhone12Case']);
printf($iPhone12 * 15);
}
I'm trying to create an html form to provide information about the salaries table in my database, the user should be able to pick year between 1986-1996 and choose if she wants to see the total salary of that year or the average salary of that year.
I have no idea how I link up these scripts and I can't find much online.
html file:
<html>
<body>
<fieldset>
<form id="frmName" method=post action="Oppgave4.php" onsubmit="">
<h1>Oppgave 4</h1>
Choose year:
<select id="frmName" onChange="">
<option selected disabled hidden>----</option>
<option name="1986">1986</option>
<option name="1987">1987</option>
<option name="1988">1988</option>
<option name="1989">1989</option>
<option name="1990">1990</option>
<option name="1991">1991</option>
<option name="1992">1992</option>
<option name="1993">1993</option>
<option name="1994">1994</option>
<option name="1995">1995</option>
<option name="1996">1996</option>
</select>
Total or average salary:
<select id="frmName" onChange="">
<option selected disabled hidden>----</option>
<option name="Total">Total salary</option>
<option name="Average">Average salary</option>
</select>
<input type="submit" value="Submit" id="submit">
</p>
</form>
</fieldset>
</body>
</html>
php file:
<?php
$year = ($_POST['1986'], $_POST['1987'], $_POST['1988'], $_POST['1989'], $_POST['1990'],
$_POST['1991'], $_POST['1992'], $_POST['1993'], $_POST['1994'], $_POST['1995'],
$_POST['1996'], $_POST['Total']);
$average = $_POST['Average'];
$conn = mysqli_connect("localhost", "root", "", "employees");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sqlavg = "SELECT AVG(salaries.salary) AS average FROM salaries
WHERE from_date = '$year'";
$result = $conn->query($sql);
if (!empty($result) && $result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["average"] ."</td></tr>";
}
echo "</table>";
$sqlsum = "SELECT SUM(salaries.salary) AS total FROM salaries
WHERE from_date = '$year'";
$result = $conn->query($sql);
if (!empty($result) && $result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["total"] ."</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
What you're trying to do is literally PHP/MySQL 101 and there is a lot online on how to do this. Having said that you are making some mistakes in your code. First, you should name the <select>
<select name="year">
Then you should give each option a value:
<option value="1994">1994</option>
...// do each one like this
This way, when the form is submitted to the PHP you can find it in the POST array:
$year = $_POST['year'];
That is just a start. You have a second drop-down that also needs a name and each option should have a value attribute.
<select name="calculation_type">
<option>----</option>
<option value="Total">Total salary</option>
<option value="Average">Average salary</option>
</select>
Which will be found like this in the POST array:
$average = $_POST['calculation_type'];
Your form needs a name and does not need the onsubmit The action should be the name of the PHP script which will perform the calculations:
<form name="form_name" method=post action="Oppgave4.php">
Warning
Little Bobby says your script is at risk for SQL Injection Attacks. Learn about prepared statements for MySQLi. Even escaping the string is not safe!
Suggestion
You should go work through some basic PHP tutorials like those offered by https://www.learn-php.org/ (a free, interactive website) or other services
I am trying to create a selected value of the previous output.
(the user fills in a form , submits, returns to the form) And I want the selected value to be the value the user previously used.
The vraagNummer and answer are given in the url parameter and the dropdown menu of items available in the list are created in a for loop.
Now I got stuck on that part.. How do I create an option selected value if it is created in a for loop?
Usually I would just put in option value = $vraagNummer selected> <?php echo $vraagNummer ?></option but in this case that wouldn't work. I suppose?
Anyone knows how to fix this?
Same with the A/B/C/D. How do I put in the selected value of answer into the value previously selected while still keeping the others as an option.
With kind regards,
if(!empty($_GET['vraagnummer'])){
$vraagNummer = $_GET['vraagnummer'];
if(!empty($_GET['answer'])){
$answer = $_GET['answer'];
}
}
echo $vraagNummer;
echo $answer;
?>
<form id="start" method="post" action="index.php?test=true">
<select name="question">
<?php
for($i= 1; $i < $nRows+1 ; $i++){
?><option value="<?php echo $i ?>">Question <?php echo $i?></option>
<?php
}
?>
</select>
<select name="result">
<option value="A">Answer A</option>
<option value="B">Answer B</option>
<option value="C">Answer C</option>
<option value="D">Answer D</option>
<input type="submit" id="submit" value="Submit" />
</form>
You simply test the $_GET['question'] against the current $i value in the loop using a ternary if
<form id="start" method="post" action="index.php?test=true">
<select name="question">
<?php
for($i= 1; $i < $nRows+1 ; $i++){
$sel = (isset($_GET['question']) && $_GET['question'] == $i) ? 'selected="selected"' : '';
echo '<option ' . $sel . ' value="' . $i .">Question ' . $i . '</option>';
}
?>
</select>
Warning:
Also be careful with the use of empty() when the variables can contain a zero as zero is considered as empty by the empty() function
Actually I would do your other dropdown like this
<select name="result">
foreach (range('A', 'D') as $char) {
$sel = (isset($_GET['result']) && $char == $_GET['result']) ? 'selected="selected"' : '';
echo "<option $sel value='$char'>Answer $char</option>";
}
</select>
I have a sticky select form, which gets the data for the options from MySQL. The form should display "Select Type" if no form was submitted (this works). Then I tried to make it sticky. My idea was to compare the $_POST['type'] with the data from MySQL and if its the same it should echo selected.
The $_POST['type'] was working perfectly when I tried to echo it, also the options from the MySQL DB are working.
I feel like I'm close to the solution, but I'm missing something. Any ideas?
<select type="text" name="type" id="type" class="form-control input-lg">
<option value="" disabled <?php if(!isset($_POST['submit'])){ echo "selected";} ?> >Select type</option>
<?php
$result = mysql_query("select * from type");
while ($row = mysql_fetch_assoc($result))
{
$type[] = $row;
}
$count = count($type);
for ($i = 0; $i < $count; $i++)
{
$selected = $_POST['type'];
echo "<option";
if($selected === $type[$i] ){
echo "selected";
}
echo ">";
echo $type[$i]['type'];
echo '</option>';
}
?>
</select>
Change the line echo "selected"; to echo " selected ";
It will currently echo <optionselected> on selected option which is malformed.
I am having trouble creating an addition table for my php program. I created my multiplication table just fine (it creates the right output), but when I try and make an addition one the numbers arent inside the table and I cant figure out what I am doing wrong. I will post what my output looks like when I create an addition table, and what it is supposed to look like and also the code that I have wrote. I feel like I am very close to completing this program, but right now I am stuck, thanks for the help in advance.
Here is the output that I am supposed to get for a 4x5 addition table.
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
Here is my output .
11112222333344445555
0 1 2 3 4
1
2
3
4
5
And here is my code. Any help is appreciated.
<html>
<head/>
<body>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>" >
<table border="1">
<tr><td>Number of Rows:</td><td><input type="text" name="rows" /></td></tr>
<tr><td>Number of Columns:</td><td><select name="columns">
<option value="1">1</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
<option value="16">16</option>
</select>
</td></tr>
<tr><td>Operation:</td><td><input type="radio" name="operation" value="multiplication" checked="yes">Multiplication</input><br/>
<input type="radio" name="operation" value="addition">Addition</input>
</td></tr>
</tr><td colspan="2" align="center"><input type="submit" name="submit" value="Generate" /></td></tr>
</table>
</form>
<?php
if(isset($_POST["submit"])){
//check to see if num of rows is numberic
if ( isset($_POST["rows"]) && is_numeric($_POST["rows"])){
//check to see if rows is a positive number
if($_POST["rows"] > 0){
if(isset($_POST) && $_POST["operation"] == "multiplication"){
echo 'This is a '. $_POST["rows"] . ' x ' . $_POST["columns"] .'multiplication table';
echo "<table border=1";
echo'<tr>';
for($b = 0; $b <= $_POST["columns"];$b++){
echo '<th>'.$b.'</th>';}
echo '</tr>';
for($r = 1; $r <= $_POST["rows"]; $r++){
echo'<tr><th>'.$r.'</th>';
for($c = 1; $c <= $_POST["columns"]; $c++){
echo '<td>' .$c*$r. '</td>';
}
echo '</tr>';
}
echo "</table>";
}
else if (isset($_POST) && $_POST["operation"] == "addition")
{
echo 'This is a '. $_POST["rows"]. ' x ' . $_POST["columns"] .' addition table';
echo "<table border = 1";
echo'<tr>';
for($a = 0; $a <= $_POST["columns"];$a++){
echo '<th>'.$a.'</th>';}
echo '</tr>';
for($r = 1; $r <= $_POST["rows"]; $r++){
echo '<tr><th>'.$r.'</th>';
for($c = 1; $c <= $_POST["columns"]; $c++)
{
echo '<td>' .$c+$r. '</td>';
}
echo '</tr>';
}
echo "</table>";
}
else{
echo 'Invalid rows columns parameters';
exit();
}
}
exit();
}}
?>
</body>
</html>
try this.
Note: i only changed elseif statement which you used for additioin, so you can just copy and paste this as i changes nothing else.
so i divided the solution in three part and leave comment in bold for you to know which part i changed. thanks.
and post the comment what should you do next. In case it did not give you desired out come.
<html>
<head/>
<body>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>" >
<table border="1">
<tr><td>Number of Rows:</td><td><input type="text" name="rows" /></td></tr>
<tr><td>Number of Columns:</td><td><select name="columns">
<option value="1">1</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
<option value="16">16</option>
</select>
</td></tr>
<tr><td>Operation:</td><td><input type="radio" name="operation" value="multiplication" checked="yes">Multiplication</input><br/>
<input type="radio" name="operation" value="addition">Addition</input>
</td></tr>
</tr><td colspan="2" align="center"><input type="submit" name="submit" value="Generate" /></td></tr>
</table>
</form>
<?php
if(isset($_POST["submit"])){
//check to see if num of rows is numberic
if ( isset($_POST["rows"]) && is_numeric($_POST["rows"])){
//check to see if rows is a positive number
if($_POST["rows"] > 0){
if(isset($_POST) && $_POST["operation"] == "multiplication"){
echo 'This is a '. $_POST["rows"] . ' x ' . $_POST["columns"] .'multiplication table';
echo "<table border=1";
echo'<tr>';
for($b = 0; $b <= $_POST["columns"];$b++){
echo '<th>'.$b.'</th>';}
echo '</tr>';
for($r = 1; $r <= $_POST["rows"]; $r++){
echo'<tr><th>'.$r.'</th>';
// for($c = 1; $c <= $_POST["columns"]; $c++){
// echo '<td>' .$c*$r. '</td>';
//}
echo '</tr>';
}
echo "</table>";
}
From here i change the code In case it does not give you desired outcomes. you only have to change for loop part just change $_post['rows'] to $_post['columns'] in the first foreach loop. And in the second for loop chnage $_post['columns'] to $_post['rows']. Thanks
else if (isset($_POST) && $_POST["operation"] == "addition")
{
for($x=0; $x<$_POST['rows']+1; $x++)
{
$i=0;
$y= $i+$x;
echo '<table border 1px solid black>';
echo '<tr>';
//$value[]=$x;
echo '<td>'.$y.'</td>';
for($value=1;$value<$_POST['columns']+1;$value++){
$c=$y+$value;
// $c=$y+$value;
echo '<td>'.$c;
echo '</td>';
}
echo '</tr>';
echo '</table>';
}
My changes finished Every thing is your code as it is.
echo "</table>";
}
else{
echo 'Invalid rows columns parameters';
exit();
}
}
exit();
}}
?>
</body>
</html>
It is actually a simple bug!
There is no error in your logic. The bug is in your echo statement for addition.
You just can't echo the addition directly. Use a bracket to surround them instead. It will work :-)
Like this;
echo '<td>'.($c+$r).'</td>';
Please let us know if it solved your problem.
Thanks,