PHP dynamic table - php

I have to make a dynamic table n*n , the user first gives the number n and the program makes a 5*5 table with check box this part I have make it, the second part is the user checks same of the checkbox and clicks on submit and the program makes again a table 5*5 but in the place of check box which checks is colored. I have uploaded and image.
Sorry for my bad English, thanks for your time.
enter image description here
<form name="form" action="" method="get">
<input type="text" name="subject" id="subject" value="Give value">
</form>
<?php
$rows = $cols = $name = "";
if(isset($_GET['subject']))
$rows = $cols = $_GET['subject'];
if(isset($_POST['check_list']))
$name = $_POST['check_list'];
if(isset($_GET['subject'])){
echo "<form action='my.php' method='post'>";
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td><input type='checkbox' name='check_list[]' value='value ".$td."'></td>";
}
echo "<tr>";
}
echo "</table>";
echo "<input type='submit' />
</form>";
}
// this part of code is not make the third excecution the number 3 image
echo $cols;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
foreach($_POST['check_list'] as $value){
if($tr == $value[td])echo "<td bgcolor='#FF0000'></td>";
else
echo "<td> </td>";
}
echo "</tr>";
}
echo "</table>";
?>

Related

Handling Nested Forms in HTML/php

I try to figure out how to handle two nested forms. The first one sends data from select-option dropdown to another_file.php. The nested one should send data from check boxes to current file and should be handled by isset($_POST['...']). Here is my simplified code:
if (isset($_POST['a']))
{
// do something
}
if (isset($_POST['b']))
{
// do something else
}
echo "<form action='another_file.php' method='post'>"; // begin of first form
echo "<table class ='table table-hover table-condensed table-striped table-bordered'>";
echo "<thead>";
echo "<th>ID</th><th>Jídlo</th><th>Množství</th><th>por_cislo</th><th>Odebrat</th>";
echo "</thead>";
while($rows = $stmt->fetch()){
echo "<tr><td>" . $rows['id'] . "</td><td>".$rows['jidlo'];
echo "<select name =".$rows['id']."_".$rows['por_cislo']."> ";
$stmt2 = $db->query($q2);
echo "<option value ='nic'> (vyberte potravinu) </option>";
$max_por_cislo = $rows['por_cislo'];
while($rows2 = $stmt2->fetch())
{
echo '<option value="'.$rows2['id'].'">'.$rows2['jidlo'].'</option>';
};
echo "</select>";
echo "</td> <td>" . $rows['mnozstvi'] . "g <input name = '".$rows['por_cislo']."' type='text' value = '-'></td><td>".$rows['por_cislo']."</td>";
echo "<td><form action ='this_file.php' method = 'post'>"; // begin of nested form
echo "<input type='checkbox' id='atur_peg' name='idecko[]' value=".$rows['id']."*".$rows['por_cislo']." /></td></tr> ";
};
echo "<input type='hidden' name='pc' value=".$plan_cislo.">";
echo "<tr><td colspan='2'><input name = 'go' type='submit' value='OK'/></td><td colspan='2'><input type = 'submit' name ='a' value='ADD'/></td>";
echo "<td><input type = 'submit' name ='b' value='DELETE checked'/></form></td></tr>"; // end of nested form
echo "</table>";
echo "</form>"; // end of first form
Is
there any way to do this correctly?
Instead of nested forms, use jquery to detect checkbox being checked and then change the form action url. Something on this line.. You will have to modify the same to suit your needs.
$(document).ready(function(){
$("#formname").on("change", "input:checkbox", function(){
if( $(this).is(":checked") ) {
$('#formName').attr('action', 'this_file.php');
$("#formname").submit();
}
});
});

HTML button running out of alignment

My idea is very simple, I will have a search box and a submit button.
When user key in the keyword and click on the submit button, results will be shown below with an additional button. Now my problem is I have no idea on how to make the button to be located at bottom right of the table populated.
Please consider the below code for my situation:
<input type="text" name="criteriaInput" style="width: 300px;"> <input type="submit" name="submit" value="GO" />
<?php
if (isset($_POST['submit'])) {
if(isset($_POST['inquiryMethod'])){
error_reporting(0);
$sql = 'SELECT
*
FROM
table
WHERE
fullname REGEXP \''.$_POST['criteriaInput'].'\'' ;
$server = mysql_connect("localhost","root", "");
$db = mysql_select_db("mysql",$server);
$query = mysql_query($sql);
echo "<table class=\"striped\">";
echo "<tr class=\"header\">";
echo "<td>Full Name</td>";
echo "<td>ID</td>";
echo "<td>ID Type</td>";
echo "<td>Issuance Country</td>";
echo "<td>Class</td>";
echo "</tr>";
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row[fullname]."</td>";
echo "<td>".$row[id]."</td>";
echo "<td>".$row[id_type]."</td>";
echo "<td>".$row[issuance_country]."</td>";
echo "<td>{$row['class']}</td>";
echo "</tr>";
}
echo "<form method=\"post\" action=\"CIF_InquiryAction.php\">";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";
echo "</form>";
echo "</table>";
}else{
echo "Please select one of the criteria!";
}
}
?>
The submit button with value "Create" did successfully created on existence of data, however it's aligned on top left of the table.
Kindly advice Thank you.
You need to put your button into a table row and cell.
echo "<tr>";
echo "<td colspan=\"5\">"
echo "<form method=\"post\" action=\"CIF_InquiryAction.php\">";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";
echo "</form>";
echo "</td>"
echo "</tr>";
Also, your form should probably move to be outside your table.
Editing to show input outside of table:
echo "</table>";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";

display quantity field in table in php

I'm trying to get my table to look like this
http://gyazo.com/6a134b723c30e97fa99559158cde4b1e
but when I use my code looks like this
http://gyazo.com/1c08f83e744b3a20c99b55eee5313045
I can't get the quantity field or add field to work for the life of me; the quantity field is just blank. I would greatly appreciate some help because I can't get it to work.
<?php require_once("include/db_connect.php"); ?>
<html>
<head><title>Displaying Image files using PHP</title></head>
<body>
<h1>Displaying Images from an Image table using PHP</h1>
<?php
$db_link = db_connect("project");
// Retrieve table properties
$fields = mysql_list_fields("project", "bookstore");
$num_columns = mysql_num_fields($fields);
// Make a simple database query to select all columns and rows
$query = "SELECT * FROM bookstore";
$result = mysql_query($query) or die("SQL query failed");
// Display results as an HTML table. Note how mysql_field name
// uses the $fields object to extract the column names
echo '<table border="1" cellpadding = "5" >';
// Display the column names
echo "<tr>";
for ($i = 0; $i < $num_columns; $i++)
{
echo "<th>", mysql_field_name($fields, $i), "</th>";
}
echo "<th>"Quantity "</th>";
echo "</tr>";
// Loop over the rows of the table.
// $row contains the information for each row
// Refer to the names of the fields in the table
// Must ahow the path where the image files are held
while ($row = mysql_fetch_assoc($result))
{
echo "<tr>
<form action='somethingToHandleForm.php' method='post'>";
echo "<td>". $row['isbn']. "</td>";
echo "<td>". $row['title']."</td>";
echo "<td>". $row['author']."</td>";
echo "<td>". $row['pub']."</td>";
echo "<td>". $row['year']."</td>";
echo "<td>". $row['price']."</td>";
echo "<td><input type='text' name='quantity' value=".$row['quantity']."/></td>";
echo "<td><input type='submit' value='add></td>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
// Free the resources and close the connection
mysql_free_result($result);
mysql_close($db_link);
?>
</body>
</html>
One thing I noticed is you're missing a single quote in this line after value=add:
<input type='submit' value='add>
You have some formatting problems in your HTML, which look to be the source of your problems when rendering the table. Specifically the lines:
echo "<td><input type='text' name='quantity' value=".$row['quantity']."/></td>";
echo "<td><input type='submit' value='add></td>";
The value attribute for the first line is not properly enclosed in quotes, and there is no end single-quote for the value add in the second line (nor are you closing the input tag), try the following:
echo "<td><input type='text' name='quantity' value='".$row['quantity']."' /></td>";
echo "<td><input type='submit' value='add' /></td>";

how to show the correct result of the quiz

I m trying to make a quiz.It's working but not giving the right result. On a correct answer for example answer 1 variable rans should be incremented by one but it is incrementing after submitting the 2nd question, that's why the value of the 10th current answer is not including in the total correct answer.
<?php
require_once("global.inc.php");?>
<form name="test" method="post" action="test.php">
<?php
$qid=(!isset($_POST['q_id'])) ? 0 : $_POST['q_id'];
$rans=(!isset($_POST["rans"])) ? 0 : $_POST["rans"];
$totalquestion=(!isset($_POST["totalquestion"])) ?
0 : $_POST["totalquestion"];
echo $rans;
if(isset($_POST["submit"]))
{
echo "<table align='center' style='border:1px solid silver' width='80%'
bgcolor='green'>";
echo "<tr><td>Total Question Attempt</td><td>",$totalquestion,"</td><tr>";
echo "<tr><td>Correct Answer</td><td>",$rans,"</td></tr>";
echo "<tr><td>Wrong Answer</td><td>",$totalquestion-$rans,"</td></tr>";
echo "<tr><td>Correct Answer Percentage</td> <td>",$rans/$totalquestion*100,"%</td></tr>";
echo "<tr><td>Wrong Answer Percenntage</td><td>",($totalquestion-$rans)/$totalquestion*100,"%</td></tr>";
echo "</table><br><br>";
$query="select * from questions,answers
where questions.q_id=answers.q_id";
echo "<table cellpadding='5px' align='center' style='border:1px
solid silver'>";
echo "<tr><th colspan='4' id='heading'>Online Quiz Test
Question</td></tr>";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>",$row['q_id'],"</td><td colspan='2'>",$row['question'],"</td></tr><tr><td></td>";
echo "<td colspan='2'>A. ",$row['opt1'],"</td>";
echo "<td colspan='2'>B. ",$row['opt2'],"</td></tr>";
echo "<tr><td></td><td colspan='2'>C. ",$row['opt3'],"</td>";
echo "<td colspan='1'>D. ",$row['opt4'],"</td></tr>";
echo "<tr><td colspan='4' align='right'
style='color:orange'>Correct option is ",strtoupper($row['correct_ans']),"</td></tr>";
echo "<tr><td colspan='4' align='right'
style='color:orange'><hr></td></tr>";
}
echo "</table>";
echo "<p align='right'><a href='#' onclick='window.print()'>Print</a></p>";
echo "<div style='visibility:hidden;display:none'>";
}
?>
<form name="test" method="post" action="test.php">
<?php
if(!isset($a))
{
$a=0;
//unset($_SESSION['score']);
}
if(isset($_POST['next'])) {
$a=$_POST['a'];
$totalquestion=$_POST['totalquestion'];
if(isset($_POST['rans']))
$rans=$_POST['rans'];
}
$sql1="SELECT * FROM questions,answers
where questions.q_id=answers.q_id limit 1 offset $a";
$result=mysql_query($sql1);
$num = mysql_num_rows($result);
echo "<form method='post' action=''>";
if($result) {
while ($row = mysql_fetch_array($result))
{
$qid = $row["q_id"];
$questions = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$opt4 = $row["opt4"];
$correct = $row["correct_ans"];
echo $rans;
?>
<p >Q.<?php echo $qid ?> <?php echo $questions;?></p>
<input type="radio" value="<?php echo $opt1;?>" name="choice"/><?php echo $opt1;?> <br/>
<input type="radio" value="<?php echo $opt2;?>" name="choice"/><?php echo $opt2;?><br/>
<input type="radio" value="<?php echo $opt3;?>" name="choice"/><?php echo $opt3;?><br/>
<input type="radio" value="<?php echo $opt4;?>" name="choice"/><?php echo $opt4;?><br/>
<input type="hidden" value="$answer" name="rightanswer[$qid]"/>
<?php
$b=$a+1;
$sql2="SELECT * FROM questions where q_id=$qid-1 ";
$result2=mysql_query($sql2);
while ($row2 = mysql_fetch_array($result2)) {
$ans=$row2['correct_ans'];
}
if(isset($_POST['choice'])) {
if($ans==$_POST['choice']){
//echo "<input type='hidden' name='rans' value='".($rans+1). "'>";
$rans=$rans+1;
}
else {
//echo "<input type='hidden' name='rans' value='" . $rans . "'>";
$rans=$rans;
}
}
//$query="select correct_ans from questions where q_id='$qid'";
//$result=mysql_query($query);
//while ($row = mysql_fetch_array($result)) {
//echo $row['correct_ans'];
echo "<input type='hidden' value='$b' name='a'>";
echo "<input type='hidden' value='count' name='count'>";
echo "<input type='hidden' name=qid value='$qid'>";
echo "<input type='hidden' name='totalquestion' value='".$totalquestion+1)."'>";
echo "<input type='hidden' name='rans' value='" . $rans . "'>";
echo "<input type='submit' name='next' value='next'> ";
echo "<input type='submit' name='submit' value='submit'><br><br>";
echo "</form>";
}
}
?>
Okay, your code is a bit of a mess.
You have random tautologies (like the $rans=$rans; which does absolutely nothing. If the answerer clicks "next" you're assigning $totalquestion twice. Definitely take a good, hard look and refactor this page.
But the answer to your question is probably because you're checking to see if they entered in the right answer at the bottom of the code -- after you've presented the results or the next question.
You've utilized the scripting capabilities of PHP without touching on any functions so it will evaluate top to bottom.
I'd move everything around: Move the handler for "next" to the top, underneath your default variable assignments, then put the check for right answer underneath that, then do the presentation of the next question, then the "submit"handler.
I'd break up the different units into functions for readability and reusability, also. For example, make a function to print out the specified question, make another one to validate the user entered in the right answer.

How to create submit form in a row and relate input to that row?

Hey Guys, I have a question for you.
Imagine that I wanted to be able to keep track of how many miles I've ran every week, so that I could
compare it to the goals I've set for each week. So i've created this table by the use of mysql_fetch_row.
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><form><input method='post' type='number'></form></td>";
echo "</tr>";
}
echo "</table>";
This piece of code resultet in a table with 10 weeks with 10 goals - and a column for the actual number of miles. This column should include 10 input forms where the actual number of miles can be submitted. But how do I relate the input from the submit form to the row in which the submit form is positioned?
The primary key is the week - so this would be the one to relate to.
Hope you understand what my problem is:)
To do this you would use a hidden input field.
When you echo each row, and the form in that row, you would simply add an extra line:
`<input type="hidden" name="row_id" value="' . $row['id_column'] . '" />';
In full, your code would be:
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>
<form>
<input method='post' type='number'>
<input type='hidden' name='row_id' value='" . $row['id_column'] . "' />
</form>
</td>";
echo "</tr>";
}
echo "</table>";
I think there should be some modifications that has to be done in loop.
echo "<td><form method='post'><input type='number' value='".$rows['col_name']."'><input type='submit' ></form></td>";
This code adds a submit button to each row. But, this shouldn't be what I think.
It should be rather this way,
echo "<form method='post'> ";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><input type='number' value='".$rows['col_name']."'></td>";
echo "</tr>";
}
echo "<input type='submit' ></form>";
Or make the input field look like this.
'<input type="text" name="row['.$row['id_column'].'][miles]" />';
It will return you an array when you post it.
foreach($_POST['row'] as $key => $value){
// $key is your primary key
// $value['miles'] is your input value
}

Categories