I have a db where read all information I read a optino and the stato 1 is confirmed and 0 not... I create a table where anyone can update, check or remove check and after the submit I update all data but in the submit I see only the "checked"... the other not..
$c_row = current($row);
if ($y > 1) {
echo "<form name=salvo method=post action='dettaglio.php?tipo=1'>";
$id = substr($c_row,0,strpos($c_row, '|'));
$stato = substr($c_row,strpos($c_row, '|')+1,1);
echo "<td class='tg-dett' align=center>";
if ($stato == 1) {
echo "<input type='checkbox' name='chkColor[]' value='$c_row' checked>";
} else {
echo "<input type='checkbox' name='chkColor[]' value='$c_row' >";
}
echo "</td>";
for($i = 0; $i < count($_POST["chkColor"]); $i++)
{
if(trim($_POST["chkColor"][$i]) != "") {
echo "chkColor $i = ".$_POST["chkColor"][$i]."<br>";
}
}
}
the output is only checked, If anyone remove a check don't appear to output
You can either check on the values after posting it on being empty or not set. Or you can use the hidden input trick like so:
<input type='hidden' name='chkColor[' . $row["id"] . ']' value='0'><input type='checkbox' name='chkColor[' . $row["id"] . ']' checked>
This way it will post all boxes but will add a different value to determine which is on and which is not on.
Related
I have a form, populated by an uploaded CSV, and upon submit it saves to my database staging/temp table. However, it's only saving the first 4 of 7 rows.
The form:
$file = $_FILES["file"]["tmp_name"];
$handle = fopen($file, "r");
$maxPreviewRows = PHP_INT_MAX; // this will be ~2 billion on 32-bit system, or ~9 quintillion on 64-bit system
$hasHeaderRow = true;
echo "<form method='post' action='/form-submit' >";
echo '<table>';
/*WE WILL NEED TO QA CONDITIONS AND HIGHLIGHT IN RED HERE. ALSO NEED BORDER STYLINGS*/
if ($hasHeaderRow) {
$headerRow = fgetcsv($handle);
echo '<thead><tr>';
foreach($headerRow as $value) {
echo "<th>$value</th>";
}
echo '</tr></thead>';
}
echo '<tbody>';
$rowCount = 0;
while ($row = fgetcsv($handle)) {
$colCount = 0;
echo '<tr>';
foreach($row as $value) {
echo "<td><input name='row[".$rowCount."][".$colCount."]' type='text' value='$value' /></td>";
$colCount++;
}
echo '</tr>';
if (++$rowCount > $maxPreviewRows) {
break;
}
}
echo '</tbody></table>';
echo "<input type='submit' name='confirm' value='confirm'>";
echo '</form>';
}
The submission:
foreach ($_POST['row'] as $rowValue)
{
if( is_array($rowValue) && count($rowValue) > 0 )
{
$sqlFull = "INSERT INTO staging VALUES( DEFAULT, ". substr( str_repeat ( '?, ', count($rowValue) ), 0, -2 ) .")";
if( !$stmt = $connect->prepare($sqlFull) )
{
echo "Prepare failed: (" . $connect->errno . ") " . $connect->error;
}
$stmt->bind_param( str_repeat ( 's', count($rowValue) ), ...$rowValue );
$stmt->execute();
}
}
So it's definitely working but for some reason it's stopping on the fourth row every time. I edit the first field of each row each time and when I check my file dashboard I'll see only the first 4 from each submission.
ANy ideas why this is ending on the 4th row?
UPDATE:
I am working on a project to pull data from a sql database via PHP and create an HTML table from it. It is part of a form I am creating and the table will be a listing of what I am selling, costs, weights, etc. Anyway, I need to do this with PHP. So far I have the following in this section of my code to make this table:
<?php
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB: " .
mysqli_connect_error());
$query="SELECT fruit_item_no, fruit_name, fruit_price, fruit_weight FROM fruit_$
$result=mysqli_query($db, $query);
if (!$result) {
print "Error - The query could not be executed!" .
mysqli_error();
}
print "<table class = 'main'><caption> <h2> Fruit Purchasing Form </h2> </c$
print "<tr align = 'center'>";
$num_rows=mysqli_num_rows($result);
if ($num_rows > 0) {
$row=mysqli_fetch_assoc($result);
$num_fields=mysqli_num_fields($result);
$keys=array_keys($row);
for ($index = 0; $index < $num_fields; $index++)
print "<th class='a'>" . $keys[$index] . "</th>";
print "</tr>";
for ($row_num = 0; $row_num < $num_rows; $row_num++) {
print "<tr>";
$values = array_values($row);
for ($index = 0; $index < $num_fields; $row_num++) {
$value=htmlspecialchars($values[$index]);
print "<td class = 'b'>" . $value . "</td>";
}
print "</tr>";
$row=mysqli_fetch_assoc($result);
}
}
else {
print "There were no such rows in the table <br />";
}
print "</table>";
?>
The problem I am running into is when I attempt the view the entire page in Chrome, all I see is:
I think the error is somewhere around this section of code:
for ($row_num = 0; $row_num < $num_rows; $row_num++) {
print "<tr>";
$values = array_values($row);
for ($index = 0; $index < $num_fields; $row_num++) {
$value=htmlspecialchars($values[$index]);
print "<td class = 'b'>" . $value . "</td>";
If anyone can give me a little direction on what I might be missing, I would greatly appreciate it. I have been researching a while now and no clear fix has come up that i can see.
UPDATE:
Thought it would be best to just update my post instead of new question or comment somewhere. Thanks to all your suggestions I was able to correct my code to properly display my table and worked out all validation errors. I apologize for the code reading nightmare... I am still fairly new at this and I need to work more at it.
I did run into one issue I still cannot resolve though. I am trying to add an input box on the end of each row in the table I created. Problem is no matter what I try the boxes stay in a line above the table (pretty much at the top of the browser screen.
I placed this line in the sql query block to try to get the input box:
echo ("<input name='item[]' type='text' .=''/>");
and I also tried:
echo ("<input type='text' name ='item'/>");
Both did the same thing in giving me the line of boxes along the top. Any direction you could provide in getting these to be at the end of each row? I do not need it to put data back in the database or add to a table there. I just am trying to get input fields that will allow input that will submit with my form.
You can simplify the code by running and while loop on the result and iterating through the rows. And then using foreach loops since each row is an array of key value pairs. On the first iteration you could print the header.
$num_rows=mysqli_num_rows($result);
if ($num_rows > 0) {
$i = 0;
// loop through each row in the result
while ($row=mysqli_fetch_assoc($result)) {
// if first then print the header
if ($i == 0) {
print "<tr>";
foreach ($row as $field => $value) {
print "<th class='a'>".$field."</th>";
}
print "</tr>";
}
// print the row
print "<tr>";
foreach ($row as $value) {
print "<td class='b'>".$value."</td>";
}
print "</tr>";
$i++;
}
}
else {
print "<tr></td>There were no such rows in the table</td></tr>";
}
As suggested in the comments it would be a good idea to separate your view from your code.
In your second for loop, you have this:
for($index=0; $index<$num_fields; $row_num++)
Well, the value of $index will remain 0 throughput your execution. Try changing it to
for($index=0; $index<$num_fields; $index++)
{
$row_num++;
//rest of code
}
Hope it helps.
This is unnecessarily complicated:
$row=mysqli_fetch_assoc($result);
$num_fields=mysqli_num_fields($result);
$keys=array_keys($row);
for ($index = 0; $index < $num_fields; $index++)
print "<th class='a'>" . $keys[$index] . "</th>";
print "</tr>";
for ($row_num = 0; $row_num < $num_rows; $row_num++) {
print "<tr>";
$values = array_values($row);
for ($index = 0; $index < $num_fields; $row_num++) {
$value=htmlspecialchars($values[$index]);
print "<td class = 'b'>" . $value . "</td>";
}
print "</tr>";
$row=mysqli_fetch_assoc($result);
}
Something like this would be easier to read and should solve your problem, which was caused by incrementing the wrong index on one of the for loops:
$headers = false;
while($row=mysqli_fetch_assoc($result)) {
if (!$headers) {
echo "<thead><tr>";
foreach($row as $k=>$v) {
echo "<th>$k</th>";
}
echo "</tr></thead>";
$headers = true;
}
echo "<tr>";
foreach ($row as $k=>$v) {
$v=htmlspecialchars($v);
print "<td class='b'>$v</td>";
}
echo "</tr>";
}
But I would strongly recommend a) using a database abstraction layer like PDO, and b) separating your logic from your presentation. Debugging problems with HTML is made 100 times harder when it's jumbled all together with your PHP like this.
I managed to get the checkboxes to POST the data and displays each on the checkout page but the problem I have is when it gets to 4th column it stops when it hits a certain char limit also how would I turn this into a table on the checkout page.
Database snippet code:
print '<td><input type="checkbox" name="check_list[]"value='. $getColumn[0]. $getColumn[1]. $getColumn[2]. $getColumn[3]. $getColumn[4]. $getColumn[5].$getColumn[6].$getColumn[7].$getColumn[8].$getColumn[9].'</td>';
for ($column = 1; $column < pg_num_fields($res); $column++)
{
print "<td>" . $getColumn[$column] . "</td>";
}
}
print '</table>'
Checkout page
<?php
echo "<hr />\n";
$res = pg_query ($con, "select count(ref) from music");
$a = pg_fetch_row($res);
echo "<p>Total " . $a[0] . " music in database.</p>";
echo "<table border='1'>\n<thead>\n<tr>\n";
echo "<th>Artist</th><th>Composer</th><th>Genre</th><th>Title</th><th>Album</th><th>Label</th> <th>Price</th><th>Description</th>\n";
echo "</tr>\n</thead>\n<tbody>\n";
$res=pg_query($con, "SELECT * from music ORDER BY ref");
while ($a = pg_fetch_array ($res))
{
echo "<tr>";
for ($j = 0; $j < pg_num_fields($res); $j++) {
// htmlspecialchars converts things like & to HTML entity codes
echo "<td>" . htmlspecialchars($a[$j], ENT_QUOTES) . "</td>";
}
echo "</tr>\n";
}
echo "</tbody>\n</table>";
?>
I am sure, you are tried to do the following:
print '<table>';
for ($column = 1; $column < pg_num_fields($res); $column++) {
echo '<tr>';
print '<td><input type="checkbox" name="check_list[]" value="'.$getColumn[$column] .'" /></td>';
print "<td>" . $getColumn[$column] . "</td>";
echo '</tr>';
}
print '</table>';
I used the JSON to create an array multidimensional. Now my problem is how to make groups of checkboxes (into tables or in anything to make possible separating them, by horizontal bars for example).
This is a example of the lines of the file:
{"id": "v0", "namegroup": "Table rules create OR insert", "rule": "All classes give origin to a table.", "value": 0}
And this is how I create the array:
<?php
$file = fopen("rules.txt", "r") or exit("Unable to open file!");
$arrayRules = array();
$i = 0;
while(!feof($file))
{
$line = fgets($file);
$content = json_decode(utf8_encode($line), true);
$arrayRules[$i]['id'] = $content["id"];
$arrayRules[$i][0] = utf8_decode($content["rule"]);
$arrayRules[$i]['namegroup'] = $content["namegroup"];
$arrayRules[$i][1] = $content["value"];
$i = $i + 1;
}
fclose($file);
?>
And this is how I create the checkboxes:
echo "<input name=\"regra[]\" value=\"" . $arrayRules[$i]["id"] . "\" type=\"checkbox\" /> " . $arrayRules[$i][0] . "<br/> " ;
Remember that the user can edit the name of the group and all the other points.
As you can notice, my problem is not how to echo checkbox, but how will be the mechanism with the purpose of creating and organize the checkboxes by groups.
UPDATE1
For now I have this:
for($i=0; $i < count($arrayRules); $i++)
{
if ($arrayRules[$i]['idgroup'] == 1)
{
if ($arrayRules[$i][1] == 1)
echo "<input name=\"regra[]\" value=\"" . $arrayRules[$i]["id"] . "\" type=\"checkbox\" checked onclick=\"this.checked='checked'\"/> " . $arrayRules[$i][0] . "<br/> " ;
if ($arrayRules[$i][1] == 0)
echo "<input name=\"regra[]\" value=\"" . $arrayRules[$i]["id"] . "\" type=\"checkbox\" /> " . $arrayRules[$i][0] . "<br/> " ;
}
}
?>
the problem is that in if ($arrayRules[$i]['idgroup'] == 1) should not be the 1 but should be a variable or something that when every time it finds a new or a diferent name in the file on idgroup it add or create a new table/group of check-boxes.
Add group_id param to your json objects and resort all like
$arrayRules[$group_id][$i]
after, you can do something like:
foreach($arrayRules as $group_id=>$objects) {
// start group html
foreach($objects as $i=>$params) {
echo "<input name=\"regra[]\" value=\"" . $params[$i]["id"] . "\" type=\"checkbox\" /> " . $params[$i][0] . "<br/> " ;
}
// end group html
}
I'm trying to allow a user to edit a row of their data with POST from a displayed form. The query is working and the table properly displays everything except a name value in the form input field. I've tried numerous variations but the name value keeps coming up blank. The problem might be with this line:
echo $field_name;
Here is the code:
<form action="process.php" method="POST">
<?
$qry = "SELECT activity, site, date, FROM home WHERE user_id='$session->user_id' ORDER BY date";
$res = mysql_query($qry);
$field_name = mysql_field_name($res, 0);
function mysql_fetch_all($res) {
while($row=mysql_fetch_array($res)) {
$return[] = $row;
}
return $return;
}
function create_table($dataArr) {
echo "<form action=\"process.php\" method=\"POST\"><table><tr>";
for($j = 0; $j < 3; $j++) {
echo "<td><input type=\"text\" name=\"";
echo $field_name;
echo "\" maxlength=\"30\" value=" .$dataArr[$j]. "></td>";
}
echo "<td><input type=\"hidden\" name=\"subedit\" value=\"1\"><input type=\"submit\" value=\"Update\"></td></tr></table></form>";
}
$all = mysql_fetch_all($res);
echo "<table class='data_table'>";
echo "<tr><td colspan=\"3\"><h2>Current Profile</h2></td></tr>";
echo "<tr><small><td>Activity </td><td>Site </td><td>Date </td></small></tr>";
for($i = 0; $i < count($all); $i++) {
create_table($all[$i]);
}
echo "</table></form>";
$field_name is out of scope (check out PHP's variable scope page). Try changing your create_table function to accept the $field_name var like so:
function create_table($dataArr, $field_name) {
...
}
...
for($i = 0; $i < count($all); $i++) {
create_table($all[$i], $field_name);
}
Or using global (not as recommended)
function create_table($dataArr) {
global $field_name;
...
}
Try this code:
<form action="process.php" method="POST">
<?
$qry = "SELECT activity, site, date, FROM home WHERE user_id='$session->user_id' ORDER BY date";
$res = mysql_query($qry);
$field_name = mysql_field_name($res, 0);
function mysql_fetch_all($res) {
while($row=mysql_fetch_array($res)) {
$return[] = $row;
}
return $return;
}
function create_table($dataArr, $field_name) {
echo "<form action=\"process.php\" method=\"POST\"><table><tr>";
for($j = 0; $j < 3; $j++) {
echo "<td><input type=\"text\" name=\"";
echo $field_name;
echo "\" maxlength=\"30\" value=" .$dataArr[$j]. "></td>";
}
echo "<td><input type=\"hidden\" name=\"subedit\" value=\"1\"><input type=\"submit\" value=\"Update\"></td></tr></table></form>";
}
$all = mysql_fetch_all($res);
echo "<table class='data_table'>";
echo "<tr><td colspan=\"3\"><h2>Current Profile</h2></td></tr>";
echo "<tr><small><td>Activity </td><td>Site </td><td>Date </td></small></tr>";
for($i = 0; $i < count($all); $i++) {
create_table($all[$i], $field_name);
}
echo "</table></form>";
However, I do recommend that you try to create a Table class. It would be much more efficient and neater.