The undernoted form gives a series of options to choose using a select form. When the values are retrieved from the $_POST array although the correct id is being passed the value for season simply seems to be the last value in the form.
I have checked my code and league is not being updated anywhere else as far as I can see.
I'm sure I'm missing something obvious but can't figure it out!
<form method = 'post' action = 'index.php?choice=matcht&stage=1'><table width = '50%' border = '1'><thead><tr>
<th>Select</th>
<th>League</th>
<th>Season</th>
</tr></thead>
<tr><td><input type = 'radio' name = 'id' value = '1' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21' readonly></td>
<td><input type = 'text' name = 'season' value = '12' readonly></td>
</tr>
<tr><td><input type = 'radio' name = 'id' value = '25' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21' readonly></td>
<td><input type = 'text' name = 'season' value = '13' readonly></td>
</tr>
<tr><td><input type = 'radio' name = 'id' value = '49' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21' readonly></td>
<td><input type = 'text' name = 'season' value = '14' readonly></td>
</tr>
<tr><td><input type = 'radio' name = 'id' value = '73' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21' readonly></td>
<td><input type = 'text' name = 'season' value = '15' readonly></td>
</tr>
<tr><td><input type = 'radio' name = 'id' value = '97' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21' readonly></td>
<td><input type = 'text' name = 'season' value = '16' readonly></td>
</tr>
<tr><td colspan = '3' align = 'center'><input type = 'submit' name = 'go' value = 'GO'></td></tr>
</table>
</form>
Here is the PHP generating the HTML:
$sql='SELECT `TeamID`, `League`,`Season` FROM teams WHERE `FranchiseID` = 0 GROUP BY `League`,`Season` ORDER BY `League` ASC ,`Season` ASC ';
$unmatched = $db2->get_results($sql);
if ($db2->num_rows>0) {
echo "<form method = 'post' action = 'index.php?choice=matcht&stage=1'>";
echo "<table width = '50%' border = '1'>";
echo "<thead><tr>\n";
echo "<th>Select</th>\n";
echo "<th>League</th>\n";
echo "<th>Season</th>\n";
echo "</tr></thead>\n";
foreach ( $unmatched as $unmatch )
{
// Access data using object syntax
echo "<tr>";
echo "<td><input type = 'radio' name = 'id' value = '$unmatch->TeamID' ></td>\n";
echo "<td><input type = 'text' name = 'league' value = '".$unmatch->League."' readonly></td>\n";
echo "<td><input type = 'text' name = 'season' value = '".$unmatch->Season."' ></td>\n";
echo "</tr>\n";
}
echo "<tr><td colspan = '3' align = 'center'><input type = 'submit' name = 'go' value = 'GO'></td></tr>\n";
echo "</table>\n";
echo "</form>\n";
All of the text inputs have the same name and the last is overwriting the others. Use an array and set the index to the id so you know which ones to get:
<tr><td><input type = 'radio' name = 'id' value = '1' ></td>
<td><input type = 'text' name = 'league[1]' value = 'MLB21' readonly></td>
<td><input type = 'text' name = 'season[1]' value = '12' readonly></td>
Then you can get it like:
$id = $_POST['id'];
$league = $_POST['league'][$id];
$season = $_POST['season'][$id];
Related
I have searched for this and keep getting errors no matter what I try.
The main error I get is:
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, string given
I have created a page showing all records. The user then selects one record to edit and the id is passed on to this page which would show all the fields in a form/table. a few of the fields are readonly so can not be modified.
What I am trying to achieve is to show the results in a table so that they can be edited and then sent to the database and updated. I was thinking of sending it to another file to update and then send them back to view the results page.
<form action = 'modify2.php' method = 'post'>
<table border="1">
<tr style="border-bottom:1px solid #000000;">
<strong>User ID</strong>
<strong>Username</strong>
<strong>Password</strong>
<strong>Email</strong>
<strong>Name</strong>
<strong>Submitted By</strong>
<strong>Memeber Since</strong>
</tr>
<?PHP
//error check to see what results are obtained
echo '<br />sql code = '.$sql.'<br/>'; // this works bot the $result
shows nothing
//die();
//fetch object array and put into a row
//while($row = mysqli_fetch_assoc($result))
while($row = mysqli_fetch_assoc($sql))
{
?>
<tr><td><label>User ID :</label></td>
<td><input type = 'text' name = 'id' id = 'id' value='<?PHP echo $row["id"];?>' readonly/></input><br /></td></tr>
<tr><td><strong>Username</strong></td>
<td><input type = 'text' name = 'username' id = 'username' value='<?PHP echo $row["username"];?>'/></input><br /></td></tr>
<tr><td><label>password :</label></td>
<td><input type = 'text' name = 'password' id = 'password' value='<?PHP echo $row["password"];?>'/></input><br /></td></tr>
<tr><td><label>email :</label></td>
<td><input type = 'text' name = 'email' id = 'email' value='<?PHP echo $row["email"];?>'/></input><br /></td></tr>
<tr><td><label>Real Name :</label></td>
<td><input type = 'text' name = 'Name' id = 'Name' value='<?PHP echo $row["Name"];?>'/></input><br /></td></tr>
<tr><td><label>submitted by :</label></td>
<td><input type = 'text' name = 'submittedby' id = 'submittedby' value='<?PHP echo $row["submittedby"];?>' readonly/></input><br /></td></tr>
<tr><td><label>trn_date :</label></td>
<td><input type = 'text' name = 'trn_date' id = 'trn_date' value='<?PHP echo $row["trn_date"];?>' readonly/></input></td></tr>
<?PHP
}
//close table here
?>
<tr><td colspan='2'><div align='center'><input type = 'submit' value ='Modify Record' name = 'submit'/></td></tr>
</form></div>
</table>
You need to put $result=mysqli_query($con,$sql); first to you can use
// Associative array
$row=mysqli_fetch_assoc($result);
I have a table in MySql table with many rows of records (existingbankproducts table):
The code I use to select is from the database is below:
$stmt2 = $DB_con->prepare("SELECT * FROM applicantpersonaldetails apd "
. "INNER JOIN existingbankproducts ext ON apd.ApplicantID = ext.ApplicantID "
. "WHERE apd.AccountID ='{$accountId}' AND apd.applicantType ='main';");
$stmt2->execute();
if ($stmt2->rowCount() > 0) {
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
?>
<?php
}
} else {
?>
<div class="">
<div class="alert alert-warning">
<span class="glyphicon glyphicon-info-sign"></span> No Data Found ...
</div>
</div>
<?php
}
I want to select them and insert to my HTML table, the code is below:
<table>
<tr>
<th>Financial Institution</th>
<th>Product Type</th>
<th>Balance</th>
<th>Monthly Commitment</th>
</tr>
<tr>
<td><input type = "text" name = "finanIns1" id = "finanIns1" value = ""readonly></td>
<td>
<input list = "proTypeList" name = "proType1" id = "proType1"readonly >
</td>
<td id = "balance"><input type = "number" name = "balance1" id = "balance1" value = "" min = "0"readonly></td>
<td id = "MonthyComm"><input type = "number" name = "monthlyComm1" id = "monthlyComm1" value = "" min = "0"readonly></td>
</tr>
<tr>
<td><input type = "text" name = "finanIns2" id = "finanIns2" value = ""readonly></td>
<td>
<input list = "proTypeList" name = "proType2" id = "proType2" readonly>
</td>
<td id = "balance"><input type = "number" name = "balance2" id = "balance2" value = "" min = "0"readonly></td>
<td id = "MonthyComm"><input type = "number" name = "monthlyComm2" id = "monthlyComm2" value = "" min = "0"readonly></td>
</tr>
</table>
Actually, there are more rows, this is an example.
Also, I put value="<?php echo $row['Financialinstitution'] " ?> as an example, However, all the records are coming out.
Is there any way to display the result according to the HTML table in order.
1st : You need to loop the record set like this
2nd : your input value should filled with right column like this
<input type = "text" name = "finanIns1" id = "finanIns1" value="<?php echo $row['Financialinstitution']; ?>" readonly>
Note : you need to echo the each column desired td input . i have echo only one column
3rd : using prepared statement is good . as well you need to use bindparam . like this
$stmt2 = $DB_con->prepare("SELECT * FROM applicantpersonaldetails apd "
. "INNER JOIN existingbankproducts ext ON apd.ApplicantID = ext.ApplicantID "
. "WHERE apd.AccountID =:accountId AND apd.applicantType ='main';");
$stmt2->bindParam(':accountId', $accountId, PDO::PARAM_INT);
//if account id data type is varchar change the last parameter to `PDO::PARAM_str`
$stmt2->execute();
PHP :
if ($stmt2->rowCount() > 0) {
?>
<table>
<tr>
<th>Financial Institution</th>
<th>Product Type</th>
<th>Balance</th>
<th>Monthly Commitment</th>
</tr>
<?php
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
?>
<tr>
<td><input type = "text" name = "finanIns1" id = "finanIns1" value="<?php echo $row['Financialinstitution']; ?>" readonly></td>
// like above td you need to echo all your data for following td
<td>
<input list = "proTypeList" name = "proType1" id = "proType1" readonly >
</td>
<td id = "balance"><input type = "number" name = "balance1" id = "balance1" value = "" min = "0"readonly></td>
<td id = "MonthyComm"><input type = "number" name = "monthlyComm1" id = "monthlyComm1" value = "" min = "0"readonly></td>
</tr>
<?php
}
} else {
?>
<div class="">
<div class="alert alert-warning">
<span class="glyphicon glyphicon-info-sign"></span> No Data Found ...
</div>
</div>
<?php
}
Whatever i understand from your question you can look like below:
if ($stmt2->rowCount() > 0) {
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
?>
<tr>
<td><input type = "text" name = "finanIns1" id = "finanIns1" value = "<?php $row['columnName']?>" readonly></td>
<td>
<input list = "proTypeList" name = "proType1" id = "proType1"readonly >
</td>
<td id = "balance"><input type = "number" name = "balance1" id = "balance1" value = "<?php $row['columnName']?>" min = "0"readonly></td>
<td id = "MonthyComm"><input type = "number" name = "monthlyComm1" id = "monthlyComm1" value = "" min = "0"readonly></td>
</tr>
<tr>
<td><input type = "text" name = "finanIns2" id = "finanIns2" value = "<?php $row['columnName']?>" readonly></td>
<td>
<input list = "proTypeList" name = "proType2" id = "proType2" readonly>
</td>
<td id = "balance"><input type = "number" name = "balance2" id = "balance2" value = "<?php $row['columnName']?>" min = "0"readonly></td>
<td id = "MonthyComm"><input type = "number" name = "monthlyComm2" id = "monthlyComm2" value = "<?php $row['columnName']?>" min = "0"readonly></td>
</tr>
<?php
?>
<?php
}
I already found a question called "Check if at least 1 checkbox is ticked in PHP", but that did not meet my needs because I am an organized person and that didn't seem like an organized way of doing it. I have this checkbox code:
<h1>_________</h1>
<p>Which records would you like to see?<br />
Enter as little or as many criteria as you want and then press Submit.</p>
<form method = "get" action = "http://www.its-a-secret.com/validation.php">
<div>
<span>
<label for = "lastname" id = "txtlabel">Last Name</label>
<input type = "text" name = "lastname" />
</span>
<span>
<label for = "firstname" id = "txtlabel">First Name</label>
<input type = "text" name = "firstname" />
</span>
<span>
<label for = "company" id = "txtlabel">Company</label>
<?php
session_start();
mysql_connect("______","_______","________");
mysql_select_db("____");
$sql = "SELECT DISTINCT company FROM _____";
$result = mysql_query($sql);
print "<select name = 'company' id = 'company'> \n";
print " <option></option> \n";
while ($row = mysql_fetch_array($result)) {
print " <option value='" . $row['company'] . "'>" . $row['company'] . "</option> \n";
}
print " </select> \n";
?>
</span>
<span id = "spanaccttype">
<label for = "accttype" id = "txtlabel">Account type</label>
<?php
mysql_connect("_____","_____","_______");
mysql_select_db("_____");
$sql = "SELECT DISTINCT accttype FROM _____";
$result = mysql_query($sql);
print "<select name = 'accttype' id = 'accttype'> \n";
print " <option></option> \n";
while ($row = mysql_fetch_array($result)) {
print " <option value='" . $row['accttype'] . "'>" . $row['accttype'] . "</option> \n";
}
print " </select> \n";
?>
</span>
<span id = "spanproductname">
<label for = "productname" id = "txtlabel">Product Name</label>
<?php
mysql_connect("______","______","______");
mysql_select_db("_____");
$sql = "SELECT DISTINCT productname FROM ______";
$result = mysql_query($sql);
print "<select name = 'productname' id = 'productname'> \n";
print " <option></option> \n";
while ($row = mysql_fetch_array($result)) {
print " <option value='" . $row['productname'] . "'>" . $row['productname'] . "</option> \n";
}
print " </select> \n";
?>
</span>
<span id = "spansharetype">
<label for = "sharetype" id = "txtlabel">Share Type</label>
<?php
mysql_connect("_____","_____","______");
mysql_select_db("_____");
$sql = "SELECT DISTINCT sharetype FROM _____";
$result = mysql_query($sql);
print "<select name = 'sharetype' id = 'sharetype'> \n";
print " <option></option> \n";
while ($row = mysql_fetch_array($result)) {
print " <option value='" . $row['sharetype'] . "'>" . $row['sharetype'] . "</option> \n";
}
print " </select> \n";
?>
</span>
<span id = "spanclosed">
<label for = "closed" id = "txtlabel">Closed?</label>
<?php
mysql_connect("_________","______","__");
mysql_select_db("____");
$sql = "SELECT DISTINCT closed FROM _____";
$result = mysql_query($sql);
print "<select name = 'closed' id = 'closed'> \n";
print " <option></option> \n";
while ($row = mysql_fetch_array($result)) {
print " <option value='" . $row['closed'] . "'>" . $row['closed'] . "</option> \n";
}
print " </select> \n";
?>
</span>
<span id = "spanvaluegreaterthan">
<label for = "valuegreaterthan" id = "txtlabel">Value Greater Than $</label>
<input type = "number" name = "valuegreaterthan" />
</span>
<br />
<span id = "spanwhatcolumnstoshowquestion">What columns would you like to show? (Note if none are checked none of the below will be shown.)</span>
<table id = "showwhichcolumns">
<tr>
<td><input type = "checkbox" id = "showcolumncompany" name = "showcolumncompany" value = "showcolumncompany" title = "Show column company" /><label for = "showcolumncompany" id = "check">Company</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumnaccountnumber" name = "showcolumnaccountnumber" value = "showcolumnaccountnumber" title = "Show column account number" /><label for = "showcolumnaccountnumber" id = "check">Account #</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumncontractdate" name = "showcolumncontractdate" value = "showcolumncontractdate" title = "Show column contract date" /><label for = "showcolumncontractdate" id = "check">Contract Date</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumnclosed" name = "showcolumnclosed" value = "showcolumnclosed" title = "Show column close" /><label for = "showcolumnclosed" id = "check">Closed?</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumndateclosed" name = "showcolumndateclosed" value = "showcolumndateclosed" /><label for = "showcolumndateclosed" id = "check">Date Closed</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumndob" name = "showcolumndob" value = "showcolumndob" /><label for = "showcolumndob" id = "check">DOB</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumnproductname" name = "showcolumnproductname" value = "showcolumnproductname" /><label for = "showcolumnproductname" id = "check">Product Name</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumncommissionpercent" name = "showcolumncommissionpercent" value = "showcolumncommissionpercent" /><label for = "showcolumncommissionpercent" id = "check">Commission %</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumncommissionpayoutpercent" name = "showcolumncommissionpayoutpercent" value = "showcolumncommissionpayoutpercent" /><label for = "showcolumncommissionpayoutpercent" id = "check">Commission Payout (%)</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumncommissionpayoutdollars" name = "showcolumncommissionpayoutdollars" value = "showcolumncommissionpayoutdollars" /><label for = "showcolumncommissionpayoutdollars" id = "check">Commission Payout ($)</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumntrailcomm" name = "showcolumntrailcomm" value = "showcolumntrailcomm" /><label for = "showcolumntrailcomm" id = "check">Trail Comm?</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumnguaranteepercent" name = "showcolumnguaranteepercent" value = "showcolumnguaranteepercent" /><label for = "showcolumnguaranteepercent" id = "check">Guarantee %</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumnaccounttype" name = "showcolumnaccounttype" value = "showcolumnaccounttype" title = "Show column account type" /><label for = "showcolumnaccounttype" id = "check">Account Type</label></td>
</table>
<p>
<input type = "submit" value = "Submit" id = "gobutton" />
</p>
The form submits to a page called validation.php and this is the code for that page (I hardly have anything yet):
<?php
$lastname = $_REQUEST["lastname"];
$firstname = $_REQUEST["firstname"];
$company = $_REQUEST["company"];
$accttype = $_REQUEST["accttype"];
$productname = $_REQUEST["productname"];
$sharetype = $_REQUEST["sharetype"];
$closed = $_REQUEST["closed"];
$valuegreaterthan = $_REQUEST["valuegreaterthan"];
header("Location: http://www.its-a-secret/?lastname=" . htmlspecialchars($lastname) . "&firstname=" . htmlspecialchars($firstname) . "&company=" . htmlspecialchars($company));
?>
I would like to delete the HTML special characters, instead of changing them, I would also want the First Name and Last Name to only have one capital letter by just telling the user, and if they don't I want the SQL to be able to validate with the First Name or Last Name being all capital or something crazy like that. I also want the First Name and Last Name to have all there space(s), if there are any. I want there to be at least one checkbox checked. I also want there to be a positive number that is a number in the number textbox.
I'm not sure what you are trying to do, but here is my guess code:
if(isset($_GET['showcolumnxx']) || isset($_GET['showcolumnxx']) || isset($_GET['showcolumnxx'] || etc.)) {
$newUrl = "http://something.site.end/otherpageshere/[screenAfterValidation].php";
header('Location: ' . $newUrl);
} else {
print "<p style = "color: red;">You must at least check one checkbox.</p>\n";
}
This may help. I am just echoing in the same page. You can change it according to your need.
<?php
if(!empty($_GET['check'])) {
foreach($_GET['check'] as $check) {
echo $check."<br />";
}
}
?>
<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>">
<span id = "spanwhatcolumnstoshowquestion">What columns would you like to show? (Note if none are checked none of the below will be shown.)</span>
<table id = "showwhichcolumns">
<tr>
<td><input type = "checkbox" id = "showcolumncompany" name = "check[]" value = "showcolumncompany" title = "Show column company" /><label for = "showcolumncompany" id = "check">Company</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumnaccountnumber" name = "check[]" value = "showcolumnaccountnumber" title = "Show column account number" /><label for = "showcolumnaccountnumber" id = "check">Account #</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumncontractdate" name = "check[]" value = "showcolumncontractdate" title = "Show column contract date" /><label for = "showcolumncontractdate" id = "check">Contract Date</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumnclosed" name = "check[]" value = "showcolumnclosed" title = "Show column close" /><label for = "showcolumnclosed" id = "check">Closed?</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumndateclosed" name = "check[]" value = "showcolumndateclosed" /><label for = "showcolumndateclosed" id = "check">Date Closed</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumndob" name = "check[]" value = "showcolumndob" /><label for = "showcolumndob" id = "check">DOB</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumnproductname" name = "check[]" value = "showcolumnproductname" /><label for = "showcolumnproductname" id = "check">Product Name</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumncommissionpercent" name = "check[]" value = "showcolumncommissionpercent" /><label for = "showcolumncommissionpercent" id = "check">Commission %</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumncommissionpayoutpercent" name = "check[]" value = "showcolumncommissionpayoutpercent" /><label for = "showcolumncommissionpayoutpercent" id = "check">Commission Payout (%)</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumncommissionpayoutdollars" name = "check[]" value = "showcolumncommissionpayoutdollars" /><label for = "showcolumncommissionpayoutdollars" id = "check">Commission Payout ($)</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumntrailcomm" name = "check[]" value = "showcolumntrailcomm" /><label for = "showcolumntrailcomm" id = "check">Trail Comm?</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumnguaranteepercent" name = "check[]" value = "showcolumnguaranteepercent" /><label for = "showcolumnguaranteepercent" id = "check">Guarantee %</label></td>
</tr>
<tr>
<td><input type = "checkbox" id = "showcolumnaccounttype" name = "check[]" value = "showcolumnaccounttype" title = "Show column account type" /><label for = "showcolumnaccounttype" id = "check">Account Type</label></td>
</table>
<p>
<input type = "submit" value = "Submit" id = "gobutton" />
</p>
</form>
HI I NEED YOUR HELP..
I have this VIEW which show all items according to its company
<?php foreach($item_list as $item2):?>
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="<?php echo $item2->item_id ?>" ></td>
<td><?php echo $item2->item_desc?></td>
<td><?php echo $item2->serial_num ?></td>
<td><input type = "checkbox" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "checkbox" name = "JobStatus[]" value = "job order" ></td>
</tr>
<?php endforeach; ?>
---------------------------------------- html output ---------------------------------------
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="146" ></td>
<td>sample item 01</td>
<td>123</td>
<td><input type = "radio" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "radio" name = "JobStatus[]" value = "job order" ></td>
</tr>
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="147" ></td>
<td>sample item 02</td>
<td>21344</td>
<td><input type = "radio" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "radio" name = "JobStatus[]" value = "job order" ></td>
</tr>
My problem is that I want to insert this to new value (status, item_id, item_desc, serial_num, JobStatus) to quotation table in mysql. If Click I on radio item_id 146 row it will insert all values under item_id 146 likewise to item_id 147 it will insert all item values under 147 item_id
So you want the radio button to be checked according to it's job status?????
If so, try something along these lines ( i don't know how your your database is set up so you will have to alter the following):
<?php foreach($query as $item):?>
<td><input type = "text" name = "item_id" value ="<?php echo $item->item_id ?>" ></td>
<td><?php echo $item->item_desc?></td>
<td><?php echo $item->serial_num ?></td>
<td><input type="radio" name="JobStatus" value="quotation" <?php $item->job_status == 'quotation' ? echo "checked" : null; ?> ></td>
<td><input type="radio" name="JobStatus" value="job order" <?php $item->job_status == 'job order' ? echo "checked" : null; ?> ></td>
</tr>
<?php endforeach; ?>
Enclose the whole loop inside a form tag with action="index.php/controller/method"
In your controller,
public function method() {
$items = $this->input->post('item_id', TRUE);
$job_status = $this->input->post('JobStatus', TRUE);
$stati = $this->input->post('status', TRUE);
foreach($items as $key => $row) {
$this->your_model->insert_new($row, $job_status[$key], $stati[$key]);
}
}
In your_model, write the database query to insert these data wherever you need. Of course you need to change the names - method, controller and your_model to suit your application.
I have a page that pulls 5 random rows from a table. It looks similar to the below.
<table>
<tr>
<td><input type = 'radio' name='bills[1]' value = 'y'><label for='1'>Yes</label> </td>
<td><input type = 'radio' name='bills[1]' value = 'n'><label for='1'>No</label> </td>
</tr>
<tr>
<td><input type = 'radio' name='bills[8]' value = 'y'><label for='8'>Yes</label> </td>
<td><input type = 'radio' name='bills[8]' value = 'n'><label for='8'>No</label> </td>
</tr>
<tr>
<td><input type = 'radio' name='bills[2]' value = 'y'><label for='2'>Yes</label> </td>
<td><input type = 'radio' name='bills[2]' value = 'n'><label for='2'>No</label> </td>
</tr>
<tr>
<td><input type = 'radio' name='bills[6]' value = 'y'><label for='6'>Yes</label> </td>
<td><input type = 'radio' name='bills[6]' value = 'n'><label for='6'>No</label> </td>
</tr>
<tr>
<td><input type = 'radio' name='bills[3]' value = 'y'><label for='3'>Yes</label> </td>
<td><input type = 'radio' name='bills[3]' value = 'n'><label for='3'>No</label> </td>
</tr>
</table>
This returns an array that looks like the following,
Array
(
[bills] => Array
(
[6] => y
[2] => n
[5] => n
[1] => y
[8] => y
)
)
By using a foreach($_POST['bills'] as $bill) statement I can loop through that array, but how do I get the value of the id, and its respective answer? In the above case, 6, 2, 5, 1, 8.
Include the key on your foreach construct like this
foreach($_POST['bills'] as $bill=>$answer)
{
echo "The value of $bill is $answer\n"; $bill will be 6,2,5,1,8 and $answer will be y,n,n,y,y
}
OUTPUT :
The value of 6 is y
The value of 2 is n
....
You can use key():
<?php
$array = array(
"one" => 1,
"two" => 2,
"three" => 3,
"four" => 4
);
while($element = current($array)) {
echo key($array)."\n";
next($array);
}
?>
OR
foreach($_POST['bills'] as $bill=>$answer)
{
echo "$bill and $answer\n";
}