I have an HTML table that gets information fetched from a DB. Using a while loop, all the rows are created depending on the number of user records on the DB. The problem is that only the last row of the table shows the select box.
<table id="UserTable">
<tr bgcolor="#2ECCFA">
<th>UserID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Approval</th>
</tr>
<?php
while ($course = mysqli_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$course['user_id']."</td>";
echo "<td>".$course['first_name']."</td>";
echo "<td>".$course['last_name']."</td>";
echo "<td>".$course['email']."</td>";
}
?>
<td>
<select>
<option value="Approved">Approved</option>
<option value="Dissaproved">Dissaproved</option>
</select>
</td>
</table>
Fix your html as:
<?php
while ($course = mysqli_fetch_assoc($records)){?>
<tr>
<td><?php echo $course['user_id']?></td>
<td><?php echo $course['first_name']?></td>
<td><?php echo $course['last_name']?></td>
<td><?php echo $course['email']?></td>
<td>
<select>
<option value="Approved">Approved</option>
<option value="Dissaproved">Dissaproved</option>
</select>
</td>
</tr>
<?php
}?>
Related
In this code, I have 2 dropdowns in the table (one is the options from MySQL and I checked the condition for options) and I want to pass their values for each row to the next page when I push the button "call" in that row.
So, how can I do this?
<table class="table-info">
<thead>
<tr>
<th>HN number</th>
<th>name</th>
<th>surname</th>
<th>gender</th>
<th>date of birth</th>
<th>claim</th>
<th>department</th>
<th>unit</th>
<th>tel</th>
<th>time</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row["hn"] ?></td>
<td><?php echo $row["first_name"] ?></td>
<td><?php echo $row["last_name"] ?></td>
<td><?php echo $row["gender"] ?></td>
<td><?php echo $row["DOB"] ?></td>
<td><?php echo $row["claim"] ?></td>
<td><?php echo $row["dept"] ?></td>
<td><?php echo $row["unit"] ?></td>
<td><?php echo $row["tel"] ?></td>
<td><?php echo $row["q_time"] ?></td>
<td>
<div action = "manage_db.php" method="post">
<select class="select" name="doc_drop" <?= ( $row["doc"] == 'general' ? '' : 'disabled' ) ?> require>
<option selected disabled>doctor</option>
<?php foreach($result2 as $doc_select){?>
<option value="<?php echo $doc_selectp["doc"];?>"
<?= ( trim( $row["doc"] ) == $doc_select["doc"] ? 'selected' : '' ) ?> >
<?php echo $doc_select["doc"];?>
</option>
<?php } ?>
</select>
<select class="select" name="room" require>
<option selected disabled> room </option>
<option value="1">room 1</option>
<option value="2">room 2</option>
<option value="3">room 3</option>
<option value="4">room 4</option>
<option value="5">room 5</option>
</select>
<a href="manage_db.php?hn_patient=<?php echo $row["hn"];?>" class = btn-call >call</a>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
My website
Based on the user that is chosen within the combo box, I want the table that is displaying user data from the database to only show the data corresponding to the user selected in the combo box.
I mainly tried using an array to store values but I couldn't get that working.
Combo Box that displays the name to pick
<select>
<?php
$res = mysqli_query($db, "SELECT * FROM shifts");
while ($row = mysqli_fetch_array($res))
{
?>
<option><?php echo $row ["name"]; ?></option>
<?php
}
?>
<button class="btn-primary rounded">Find</button>
</select>
</form>
Table that shows the data from the database.
<table class="table table-hover">
<thead class="thead-dark"></thead>
<tr>
<th scope="col">Shift ID</th>
<th scope="col">Name</th>
<th scope="col">Origin</th>
<th scope="col">Destination</th>
<th scope="col">Date</th>
</tr>
</thead>
<?php
global $result;
//Fetch Data form database
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tbody>
<tr>
<td><?php echo $row['shift_id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['origin']; ?></td>
<td><?php echo $row['destination']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
</tbody>
</table>
I'm wondering if by using the form and doing a function that on pressing the Find button it looks up the user and displays only it's data. Thanks
Check my code here, there are some thing you must add, like a WHERE statement to your query, when fetching data to only show results with the selected name in the form
<!-- Create a form with a select for all the names -->
<form method="POST" action="">
<select name="name">
<?php
$res = mysqli_query($db, "SELECT * FROM shifts");
while ($row = mysqli_fetch_array($res))
{
?>
<option><?php echo $row ["name"]; ?></option>
<?php
}
?>
<button class="btn-primary rounded" name="find_info">Find</button>
</select>
</form>
<?php
if(isset($_POST['find_info'])){ //If find button is pressed, show this:
?>
<table class="table table-hover">
<thead class="thead-dark"></thead>
<tr>
<th scope="col">Shift ID</th>
<th scope="col">Name</th>
<th scope="col">Origin</th>
<th scope="col">Destination</th>
<th scope="col">Date</th>
</tr>
</thead>
<?php
global $result;
$nameSelected = strip_tags(mysqli_real_escape_string(htmlspecialchars($_POST['name'])));
// Use WHERE in your mysqli query to fetch data where name in database is equal to $nameSelected
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tbody>
<tr>
<td><?php echo $row['shift_id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['origin']; ?></td>
<td><?php echo $row['destination']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
I have a table that is populated using a foreach loop. One of my columns is SKU Group, which is a column of dropdown boxes. However, whenever the foreach loop runs, only the one corresponding value for that row is displayed inside the dropdown box.
How can I get it so that I can run the foreach loop and it correctly populates the table, while the dropdowns are also populated with every SKU Group name and not just the single value that corresponds with each row?
Normally, I would just run this foreach($var->fetchAll() as $var1) for the dropdowns to populate them, but I dont think that it can be ran properly inside another loop that is already running. So that is why I am having this issue.
HTML Table:
<table id="skuTable" cellspacing="5" class="ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header">
<th style="display: none">Product ID</th>
<th class="skuRow">Major Category</th>
<th class="skuRow">Minor Category</th>
<th class="skuRow">Report Code</th>
<th class="skuRow">SKU</th>
<th class="skuRow">SKU Description</th>
<th class="skuRow">SKU Status</th>
<th class="skuRow">Create Date</th>
<th class="skuRow">SKU Group</th>
<th class="skuRow">Edit</th>
</tr>
</thead>
<tbody>
<?php foreach ($dbh->query($query) as $row) {?>
<tr>
<td style="display: none" class="prod_id" id="product_id-<?php echo intval ($row['Product_ID'])?>"><?php echo $row['Product_ID']?></td>
<td class="major_cat" id="major_cat-<?php echo intval ($row['Major Category'])?>"><?php echo $row['Major Category']?></td>
<td class="minor_cat" id="minor_cat-<?php echo intval ($row['Minor Category'])?>"><?php echo $row['Minor Category']?></td>
<td class="rep_code" id="rep_code-<?php echo intval ($row['Product Report Code'])?>" align="center"><?php echo $row['Product Report Code']?></td>
<td class="sku" id="sku-<?php echo intval ($row['SKU'])?>" align="center"><?php echo $row['SKU']?></td>
<td class="sku_desc" id="sku_desc-<?php echo intval ($row['SKU Description'])?>"><?php echo $row['SKU Description']?></td>
<td class="sku_status" id="sku_status-<?php echo intval ($row['SKU Status'])?>" align="center"><?php echo $row['SKU Status']?></td>
<td class="create_date" id="create_date-<?php echo intval ($row['Date'])?>" align="center"><?php echo $row['Date']?></td>
<td class="sku_group" id="sku_group-<?php echo intval ($row['SKU Group'])?>" align="center">
<select id="sku_group_dropdown">
<option
value=""
data-name="<?php echo $row ['SKU Group'];?>"
>
<?php echo $row ['SKU Group'];?>
</option>
</select>
</td>
<td><input type="button" class="edit" name="edit" value="Edit"></td>
</tr>
<?php } ?>
</tbody>
</table>
first need to populate your dropdown list using loop
<?php
$i = 0;
$content = '';
$names = array('Test1','Test2');
foreach ($names as $row) {
if($i== 0){
$names = array('Test1','Test2');// replace it echo $row ['SKU Group'];
foreach($names as $key)
{
$content .= '<option value="'.$key.'">'.$key.'</option>';
}
}
$i++;
?>
<tr>
<td>sadasdsa</td>
<td class="sku_group" id="sku_group-<?php echo intval ($row['SKU Group'])?>" align="center">
<select id="sku_group_dropdown">
<?php echo $content?>
</select>
</td>
<td><input type="button" class="edit" name="edit" value="Edit"> </td>
</tr>
<?php } ?>
in this code replace your array data or if you want to populate dropdown data outside your loop then first get your group data for you can use this code
I'm a beginner and I try to make an agriculture application, to gain some experience, and to try something advanced. But I can't figure out how to make searches within my application. I have:
- 4 select html elements
- 5 search boxes
For space reasons, I'll write just 3/14 columns for the html elements.
HTML code:
<form>
<!-- makes the global filter based on all the data from db -->
<select id="select1" name="select1" onchange="this.form.submit()">
<option value="">Select</option>
<option value="code">Code</option>
<option value="land">Land</option>
<option value="name">Name</option>
</select><br/>
<!-- makes a filter for a html column, based on a db column -->
<select id="select2" name="select2" onchange="this.form.submit()">
<option value="">All parcels</option>
<option value="P1">P1</option>
<option value="P2">P2</option>
<option value="P3">P3</option>
</select>
<!-- search boxes to filter 5 html columns -->
<input type="search" id="search1" name="search2">
<input type="search" id="search2" name="search2">
<input type="search" id="search3" name="search3">
<input type="search" id="search4" name="search4">
<!-- same as the previous select menu, just this is for another html column -->
<select id="select3" name="select3" onchange="this.form.submit()">
<option value="">Select</option>
<option value="MO">MO</option>
<option value="MA">MA</option>
<option value="ME">ME</option>
</select>
<!-- the 5th search box -->
<input type="search" id="search5" name="search5">
<!-- renders the data from db according to the selections made by the user or search terms -->
<table id="table" class="display" cellspacing="0" width="100%">
<tr>
<th id="code">Code</th>
<th id="land">Land</th>
<th id="name">Name</th>
</tr>
<tr>
<?php
require 'config.php'; # login to database
# ---- QUERY ----
?>
</tr>
</table>
<!-- this displays in the page as many rows as the user selects -->
<select name="select4" id="select4" onchange="this.form.submit()">
<option value="">Select</option>
<option value="1">1</option>
<option value="5">5</option>
<option value="10">10</option>
</select>
</form>
PHP code: I just put this block of code instead of "# ---- QUERY ----" from above, to populate the html columns within the page:
if(isset($_POST['select1'])){
$select1 = $_POST['select1'];
switch($select1){
case 'code':
$stmt = $db->query("SELECT * FROM users WHERE `code`='code'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
break; # some more cases follows
} # end switch($select1)
} # end if(isset($_POST['select1']))
elseif(isset($_POST['select2'])){
$select2 = $_POST['select2'];
switch($select2){
case P1:
$stmt = $db->query("SELECT * FROM users WHERE `parcel` LIKE '%P1%'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
break; # some more cases here
} # end switch($select2)
} # end if(isset$_POST['select2'])
elseif(isset($_POST['search1'])){
$search1 = $_POST['search1'];
$stmt = $db->query("SELECT * FROM users WHERE `name` LIKE '%search1%'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
} # end elseif(isset($_POST['search1']))
elseif(isset($_POST['search2'])){
$search2 = $_POST['search2'];
$stmt = $db->query("SELECT * FROM users WHERE `cont_ref_a` LIKE '%search2%'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
} # end elseif(isset($_POST['search2']))
elseif(isset($_POST['search3'])){
$search3 = $_POST['search3'];
$stmt = $db->query("SELECT * FROM users WHERE `owner` LIKE '%search3%'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
} # end elseif(isset($_POST['search3']))
elseif(isset($_POST['search4'])){
$search4 = $_POST['search4'];
$stmt = $db->query("SELECT * FROM users WHERE `block` LIKE '%search4%'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
} # end elseif(isset($_POST['search4']))
elseif(isset($_POST['select3'])){
$select3 = $_POST['select3'];
switch($select3){
case 'T1':
$stmt = $db->query("SELECT * FROM users WHERE `zone`='T1'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr> <?php
}
break; # end case 'T1', more cases follows
} # end switch($select3)
} # end elseif(isset($_POST['select3']))
elseif (isset($_POST['search5'])){
$search5 = $_POST['search5'];
$stmt = $db->query("SELECT * FROM users WHERE `s_a` LIKE '%search5%'");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr><?php
}
} # end elseif(isset($_POST['search5']))
elseif(isset($_POST['select1'])){
$select1 = $_POST['select1'];
switch($select1){
case 'code':
$stmt = $db->query("SELECT * FROM users");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
</tr><?php
}
break; # end case 'code', more cases follows
} # end switch($select1)
} # end elseif(isset($_POST[select1]))
elseif(isset($_POST['select4'])){
$select4 = $_POST['select4'];
switch($select4){
case 1:
$stmt = $db->query("SELECT * FROM users LIMIT 1");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr><?php
}
break; # end case 1, more cases follows
} # end switch($select4)
} # end elseif(isset($_POST['select4'])))
else { # if noone of the select menus or search boxes where filled, then just render all the data from db to the page
$stmt = $db->query("SELECT * FROM users");
while($row = $stmt->fetch_assoc()){ ?>
<tr class="action">
<td id="td"><?php echo $row['code'];?></td>
<td id="td"><?php echo $row['land'];?></td>
<td id="td2"><?php echo $row['name'];?></td>
</tr><?php
}
} # end php script
Sorry for this post beeing so long, but I tried to express as better as I can. The problem is that when I select one option from a select menu, or I enter a term to search, nothing is displayed. I don't care for now if something is missing in my code when comes about security, I just want to make this app work. Any help? Thanks a lot!
LE: I finally found the fix for my problem (and I will write it here so that any other persons that will search for this type of problems, to have an answer (I'll write a trivial example)):
For search-boxes:
<form action="" method="post">
<input type="search" name="search1" onchange="this.form.submit()">
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr><?php
if(isset($_POST['search1'])){
$search1 = $_POST['search1'];
if(!empty($_POST['search1'])){
$sql = "SELECT * FROM tblName WHERE fname LIKE '%$search1%'";
$stmt = $db->query($sql);
while($row = $stmt->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
</tr><?php
}
}
}
else {
$sql = "SELECT * FROM tblName";
$stmt = $db->query($sql);
while($row = $stmt->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
</tr><?php
}
} ?>
</table>
</form>
For select-menus:
<form action="" method="post">
<select name="select1" onchange="this.form.submit()">
<option value="">Select</option>
<option value="fname">First Name</option>
<option value="lname">Last Name</option>
</select>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr><?php
if(isset($_POST['select1'])){
$select1 = $_POST['select1'];
if(!empty($_POST['select1'])){
switch($select1){
case 'fname':
$stmt = $db->query("SELECT * FROM users");
while($row = $stmt->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['fname'];?></td>
</tr><?php
}
break;
case 'lname':
$stmt = $db->query("SELECT * FROM users");
while($row = $stmt->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['lname'];?></td>
</tr><?php
}
break;
}
}
}
else {
$stmt = $db->query("SELECT * FROM users");
while($row = $stmt->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
</tr><?php
}
}?>
</table>
</form>
Your sample code is incomplete. The way it is written, many of your search options won't work. I would try selecting select2 and P1. That should work.
But there is potentially a bug in all of your IF statements. You shouldn't just test if a $_POST variable is set, you should make sure it's not NULL or zero length (strlen($var) > 0). Post variables can be "set" but still not have been submitted with actual data.
I would removed everything in your example that isn't related to select2 and see if you can get it to work in a much simpler format.
And of course smashing all of your SQL and PHP code in with a bunch of HTML is a bad idea, but that's not part of the question.
Good day. Im trying to have some functionality on my project, but im stuck with this.
I want to display my database depending on what the user select
Here is my html code:
<table>
<tr><td>Select Request Status:</td>
<td><select class="form-control" name="status" style="width:200px;">
<option value="1">Pending Request</option>
<option value ="2">Requests On-Process</option>
<option value="3">Unassigned Requests</option>
<option>All Requests</option>
</select></td>
</tr>
</table>
And here is my php code:
if ($status = $_SESSION['status'] == 'Pending Request')
{
$query = "select * from tblrequest where status='Pending Request'";
$request = mysql_query($query)or die(mysql_error());?>
<table class="table">
<tr>
<th>Request ID</th>
<th>Requestor</th>
<th>Department</th>
<th>Category</th>
<th>Assigned to</th>
<th>Status</th>
<th>Created Date</th>
<th>Created Time</th>
</tr>
<?php while($row = mysql_fetch_array($request)){
?>
<tbody class="table table-hover">
<tr>
<td style="width:100px;"><?php echo $row['RequestNumber']; ?></td>
<td style="width:100px;"><?php echo $row['Requestor']; ?></td>
<td style="width:100px;"><?php echo $row['Department']; ?></td>
<td style="width:50px;"><?php echo $row['category']; ?></td>
<td style="width:100px;"><?php echo $row['AssignedTo']; ?></td>
<td style="width:100px;"><?php echo $row['status']; ?></td>
<td style="width:100px;"><?php echo $row['DateRequested']; ?></td>
<td style="width:100px;"><?php echo $row['TimeRequested']; ?></td>
</tr>
</tbody>
<?php
}
}?>
Im a newbie to php so please help me.
Can you try this,
HTML:
<form method="post">
<table>
<tr><td>Select Request Status:</td>
<td>
<?php $Statuses = array("0"=>"All Requests", "1"=>"Pending Request", "2"=>"Requests On-Process", "3"=>"Unassigned Requests" );?>
<select class="form-control" name="status" style="width:200px;" onchange="this.form.submit();">
<?php foreach($Statuses as $key=>$status):?>
<?php
$selected ="";
$statusPost = $_POST['status'];
if($key == $statusPost): $selected =" selected"; endif;
?>
<option value="<?php echo $key;?>" <?php echo $selected;?>><?php echo $status;?></option>
<?php endforeach;?>
</select></td>
</tr>
</table>
</form>
PHP:
if (isset($_POST['status'])){
$status= $_POST['status'];
$Where ='';
if($status!='0'){
$Where =" where status='".$Statuses[$status]."' ;
}
}
$query = "select * from tblrequest $Where";
$request = mysql_query($query)or die(mysql_error());?>
<table class="table">
<tr>
<th>Request ID</th>
<th>Requestor</th>
<th>Department</th>
<th>Category</th>
<th>Assigned to</th>
<th>Status</th>
<th>Created Date</th>
<th>Created Time</th>
</tr>
<?php while($row = mysql_fetch_array($request)){
?>
<tbody class="table table-hover">
<tr>
<td style="width:100px;"><?php echo $row['RequestNumber']; ?></td>
<td style="width:100px;"><?php echo $row['Requestor']; ?></td>
<td style="width:100px;"><?php echo $row['Department']; ?></td>
<td style="width:50px;"><?php echo $row['category']; ?></td>
<td style="width:100px;"><?php echo $row['AssignedTo']; ?></td>
<td style="width:100px;"><?php echo $row['status']; ?></td>
<td style="width:100px;"><?php echo $row['DateRequested']; ?></td>
<td style="width:100px;"><?php echo $row['TimeRequested']; ?></td>
</tr>
</tbody>
<?php
}
?>
put select dropdown in a form with id,action,method etc.
give id to a select element(say "mySelect")
Now, in jquery,
$("#mySelect").on("change",function(){
if($("#mySelect").val() != ""))
{
$("#form1").submit();
}
})