I'm trying to update all selected fields in the database using check box. This is the code that I've done, but it only updates the last record I choose from the checkbox list.
<tbody>
<?php while($row = mysqli_fetch_array($queryEmail)):?>
<tr>
<td><input type="checkbox" name="formDoor[]" value=<?php echo $row['id'];?>>
</td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['hour'];?></td>
</tr>
<?php endwhile;?>
</tbody>
<?php
$hor = $_POST['hours']; //input value i want to update in selected id's
foreach ($_POST['formDoor'] as $entry){
$sql = "UPDATE hora SET hour='$hor' WHERE id = $entry";
}
?>
Try this You are forget to execute mysqli_query() on loop
<tbody>
<?php while($row = mysqli_fetch_array($queryEmail)):?>
<tr>
<td>
<input type="checkbox" name="formDoor[]" value=<?php echo $row['id'];?>>
</td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['hour'];?></td>
</tr>
<?php endwhile;?>
</tbody>
<?php
$hor = $_POST['hours']; //input value i want to update in selected id's
foreach ($_POST['formDoor'] as $entry){
$sql = mysqli_query($con,"UPDATE hora SET hour='$hor' WHERE id = $entry");
}
?>
Related
I want to filter data in the table use dropdown menu (e.g. filter by name so i can see all data with name 'Jane'). I don't want to move to another page (use ajax or anything else if can). Any idea what must i do ?
This is the dropdown menu and table code :
<!-- Dropdown menu -->
<div class="col-md-2">
<select class="form-control selectpicker">
<option value="">Name</option>
<?php
// print all name value from $administratorProvider
foreach($administratorProvider as $administrator){
?>
<option value="<?php $administrator->first_name ?>"><?php echo $administrator->first_name; ?></option>
<?php
}
?>
</select>
</div>
<table>
<!-- Table heading -->
<thead>
<tr>
<th class="center">No.</th>
<th>Name</th>
<th>Email</th>
<th>Join</th>
<th>Last Login</th>
</tr>
</thead>
<!-- Table body -->
<tbody>
<?php
$i=1;
foreach ($dataProvider as $data){
?>
<tr>
<div>
<td class="center"><?php echo $i; ?></td>
<td><?php echo $data->name; ?></td>
<td><?php echo $data->email; ?></td>
<td><?php echo $data->join; ?></td>
<td><?php echo $data->last_login; ?></td>
</div>
</tr>
<?php $i++; } ?>
</tbody>
<!-- // Table body END -->
</table>
Thanks for any advice.
Regards
You can use jQuery to achieve this effect quite easily. Make two files:
1) One that includes the table.
2) One that has the select tag that will reload the first file upon change of the <select> tag.
Let's call the first file select.php
<script>
// Load the div with the contents of the table.php file with no GET parameter
$('div').load('table.php');
$('select').change(function() {
var name = $(this).val();
var data = 'name='+ name;
// Make sure that the table's contents don't change if the first option tag
// is selected.
if(name != '') {
$('div').load('table.php', data);
}
});
</script>
<div class="col-md-2">
<select class="form-control selectpicker">
<option value="">Name</option>
<?php
// print all name value from $administratorProvider
foreach($administratorProvider as $administrator){
?>
<option value="<?php $administrator->first_name ?>"><?php echo $administrator->first_name; ?></option>
<?php
}
?>
</select>
</div>
File two can be called table.php
$sql = "SELECT * FROM table WHERE name = '".$name."'";
$query = mysql_query($sql)or die(mysql_error());
$num = mysql_num_rows($query);
$i = 0;
while($row = mysql_fetch_array($query)) {
// Save your info as variables
$name[$i] = $row['name'];
$email[$i] = $row['email'];
$join[$i] = $row['join'];
$login[$i] = $row['last_login'];
}
?>
<div>
<table>
<thead>
<tr>
<th class="center">No.</th>
<th>Name</th>
<th>Email</th>
<th>Join</th>
<th>Last Login</th>
</tr>
</thead>
<tbody>
<?php
for($i=0;$i<$num;$i++) {
?>
<tr>
<div>
<td class="center"><?php echo $i; ?></td>
<td><?php echo $name[$i]; ?></td>
<td><?php echo $email[$i]; ?></td>
<td><?php echo $join[$i]; ?></td>
<td><?php echo $login[$i]; ?></td>
</div>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
Upon change of the select tag, the second file will be loaded with the value of the selected option tag sent to that file as a GET variable. You might wanna take the SQL part with a grain of salt as I'm not sure how you're fetching your relevant data.
I working with a web page which displays all the records of students [id, name, subject name, attendance status] that is displaypage.php
$query="SELECT * FROM tbl_stu_rec WHERE Degree_name='".$degree."' ";
$result=mysql_query($query);
$num= mysql_num_rows($result);
<?php
$i=0;
while ($i< $num)
{
$f1=mysql_result($result,$i,"Reg_no");
$f2=mysql_result($result,$i,"First_name");
$f3=mysql_result($result,$i,"Degree_name");
<form action="adattendanDb.php" method="GET">
<tr align="center">
<td><?php echo $f1; ?> </td>
<td><?php echo $f2; ?> </td>
<td><?php echo $f3; ?> </td>
<td><select id='attend' name='attendancestatus' >
<option>p</option>
<option>a</option>
</select>
</td>
</tr>
<?php
$my_array[$i]['id'] = $f1;
$my_array[$i]['fn'] = $f2;
$my_array[$i]['deg'] = $f3;
$i++;
}
?>
<input type="submit" value="Add Attendance">
</form>
Now i want to store the values (note there are multiple equal record against each value which is being fetched from db) in variable in a way that i will be able get them on next page where i can save all the record against their values in database including their attendance status ? in adattendanDb.php
I need help with that thanks.
<form action="adattendanDb.php" method="GET">
<?php
$my_array = array();
while ($row = mysql_fetch_array($result))
{
$f1 = $row["Reg_no"];
$f2 = $row["First_name"];
$f3 = $row["Degree_name"];
?>
<tr align="center">
<td><?php echo $f1; ?> </td>
<td><?php echo $f2; ?> </td>
<td><?php echo $f3; ?> </td>
<td><select id='attend' name='attendancestatus' >
<option>p</option>
<option>a</option>
</select>
</td>
</tr>
<?php
$my_array[]['id'] = $f1;
$my_array[]['fn'] = $f2;
$my_array[]['deg'] = $f3;
}
?>
<input type="submit" value="Add Attendance">
</form>
Evening all, I have the form, which is populated from a sql database.
<table class="sortable" border="2" cellspacing="2" cellpadding="2">
<tr>
<th> Serial Number </th>
<th> Date </th>
<th> Time </th>
<th> Color </th>
<th> Design </th>
<th> Result </th>
</tr>
<?php
$i = 0;
while ($i < $num)
{
$serial = mysql_result($results, $i, 'serial_number');
$date = mysql_result($results, $i, 'date');
$time = mysql_result($results, $i, 'time');
$airport = mysql_result($results, $i, 'color');
$terminal = mysql_result($results, $i, 'design');
$result = mysql_result($results, $i, 'result');
?>
<tr>
<td> <?php echo $serial; ?> </a></td>
<td> <?php echo $date; ?> </td>
<td> <?php echo $time; ?> </td>
<td> <?php echo $color; ?> </td>
<td> <?php echo $design; ?> </td>
<td> <?php echo $result; ?> </td>
</tr>
<?php
$i++;
}
?>
What I would like to do is have each row of the table clickable. When each row is clicked, one cell of data from that row (the first cell) is sent via (POST) to the next page. Can a form be integrated into each tr??
You specify jQuery in your tags, so I assume you can use that.
// when any row is clicked
$('table').on('click', 'tr', function () {
// get the value
var value = $(this).find('td:first').text();
// redirect the user with the value as a GET variable
window.location = nextPage + '?data=' + value;
});
Where nextPage is the URL of the page you want to redirect to.
The short answer: yes, a form can be integrated into each tr.
The long answer - use just as you did before:
<tr>
<td>
<form method="post" target="details.php">
<input type="submit" name="more" value="<?php echo $serial; ?>"
</form>
<?php echo $serial; ?>
</td>
<td> <?php echo $date; ?> </td>
<td> <?php echo $time; ?> </td>
<td> <?php echo $color; ?> </td>
<td> <?php echo $design; ?> </td>
<td> <?php echo $result; ?> </td>
</tr>
But GET is easier, make a series of links that go details.php?number=123, or whichever:
<td>
<a href="details.php?number=<?php echo $serial; ?>"
<?php echo $serial; ?>
</a>
</td>
Although get can use a form, the data send is not user-customised, so the form data can be generated to use like a link.
Try with that code in echo when create your table:
<td><?php echo(''.$serial.'');?></td>
For each data that you have!
OTHER SOLUTION WITHOUT ACTION
<td><?php echo(''.$serial.'');?></td>
my_page.php - where to send the data
?[variable_name] - where is stored the data
I have a table which show data from mysql database in HTML table like this:
then I want to show like this with checkboxes:
after that user select some rows and submit then the selected rows shown in the next page so how could it be done in php with HTML.
This will get you started:
<table>
<?php
while($r = mysql_fetch_array($q)) {
?>
<tr>
<td><?php echo $r['s_no']; ?></td>
<td><?php echo $r['name']; ?></td>
<td><input type="checkbox" name="selected_s_no[]" value="<?php echo $r['s_no']; ?>" /></td>
</tr>
<?php
}
?>
</table>
On the submitted page there will be an array you can access with the selected s_no values
print_r($_REQUEST['selected_s_no'])
<table>
<?php
while($r = mysql_fetch_array($q)) {
?>
<tr>
<td><?php echo $r['s_no']; ?></td>
<td><?php echo $r['name']; ?></td>
<td><input type="checkbox" name="selected_s_no[]" value="<?php echo $r['s_no']; ?>" /></td>
</tr>
<?php
}
?>
</table>
on submitting page you can use foreach with selected values
foreach($_POST['selected_s_no'] as $value)
{
//show data according to selected checkbox
}
I've got an inventory table that needs to have certain in each row, and then in the last column in each row, i want to loop out each item that was used per a specific inventory update i.e.
one row would have one column for the customername, one column for the date of the inventory transaction, one for the type of transaction, the specific technician and the last column for the certain products that were used in the update. what i've got so far loops out the first 4 columns fine, but the last column only gets generated for the first row. code:
<table style="width:92%;">
<tr style="font-size:14px;">
<th style="width:150px;">Customer</th>
<th style="width:110px;">Date</th>
<th style="width:140px;">Tech</th>
<th style="width:50px;text-align:center;">Type</th>
<th>Equipment</th>
</tr>
<?php
$pd_name = array();
$compArray = array();
$prevCompany = '';
$count = 0;
$select = mysql_query("SELECT pd_name, pd_col_name, company FROM item_inventory ORDER BY company ");
while($row = mysql_fetch_array($select)){
$pd_name[$row['pd_col_name']] = $row['pd_name'];
$compArray[$row['pd_col_name']] = $row['company'];
}
$select2 = mysql_query("SELECT * FROM tech_inventory WHERE date >= '$date%' ORDER BY date DESC ");
while($row2 = mysql_fetch_array($select2)){
$count = 0;
$prevCompany = '';
?>
<tr>
<td><?php echo htmlspecialchars($row2['customerName']); ?></td>
<td><?php echo $row2['date']; ?></td>
<td><?php echo $row2['tech']; ?></td>
<td style="text-align:center;"><?php echo $row2['type']; ?></td>
<td>
<table width="200">
<?php
while (list($key, $val) = each($pd_name)){
if($row2[$key] != 0){
if($prevCompany != $compArray[$key]){
?>
<tr>
<td style="text-align:center;"><?php echo $compArray[$key]; ?></td>
</tr>
<?php
$prevCompany = $compArray[$key];
}
?>
<tr>
<td><?php echo $val ?></td>
<td><?php echo $row2[$key]; ?></td>
</tr>
<?php
$count++;
}
}
?>
</table>
</td>
</tr>
<tr><td><?php echo $count; ?></td></tr>
<?php } ?>
</table>
This is what you have to do:
reset($pd_name);
while (list($key, $val) = each($pd_name))
The problem was that each steps through the array and once it reaches the end, it won't go any further. Therefore, you have to reset the array pointer to the beginning every time.