I have a table with 4 columns that I want to sort via a select form.
For example, here I have a select to sort the Type column (column 3) with 3 possibilities:
- Site Sportif
- Site de bloc
- Terrain d’aventure
How do you do it?
<div class="stages">
<form id="form-sne-filtre" method="post">
<div>
<select name="type" id="select_1">
<option value=""><?php _e('Filtrer par type'); ?></option>
<option value="0"> <?php _e('Site Sportif'); ?></option>
<option value="1"><?php _e('Site de bloc'); ?></option>
<option value="2"><?php _e('Terrain d\'aventure'); ?></option>
</select>
</div>
</form>
<table id="myTable">
<thead>
<tr>
<th><?php _e('Nom'); ?></th>
<th><?php _e('Ville'); ?></th>
<th><?php _e('Type'); ?></th>
<th><?php _e('Niveau'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach($_POST as $key => $value){
if($value != "") $args[$key] = $value;
}
foreach($snes->items as $sne) { ?>
<tr>
<td><?php echo $sne->SNE_VILLE; ?> <?php echo $sne->SNE_NOM; ?></td>
<td><?php echo $sne->SNE_COMMUNES; ?></td>
<td><?php
$array = array(0 => __('Site sportif'),1 => __('Site de bloc'), 2 => __('Terrain d\'aventure'));
echo $array[$sne->SNE_TYPESITE];
?></td>
<td><?php _e("Nombre de voies : "); ?><?php echo $sne->SNE_NBRVOIES; ?> <?php _e('De'); ?> <?php echo $sne->SNE_NIVEAUVOIE_MIN; ?> <?php _e('à'); ?> <?php echo $sne->SNE_NIVEAUVOIE_MAX;; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
If you're using jQuery, you can submit the form whenever a user selects a sort option:
1. Bind events
Add the following tag below div.stages tag
<script>
$("#select_1").on("change", function(e) => {
$("#form-sne-filtre").submit();
})
</script>
2. Handle post param in php
<?php
$sort = $_POST["type"] ?? "";
$snes = sortSnes($snes, $sort); // you have to implement sortSnes function by yourself, don't ask me
?>
<tbody>
<?php foreach($snes as $sne): ?>
<tr>
// render table rows
</tr>
<?php endforeach;?>
</tbody>
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
I am having a problem about showing total in groups.
Here is my scenario, I have a report grouped by area and by product.
What I already have is the row for area group.
What I want to do is to show the total qty of product per area before the row for the next group. Currently, it shows the total after every row.
Here is my code.
<?php if (isset($summaryPerArea)): ?>
<div class="col-lg-12">
<table id="" class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>Area</th>
<th>Material</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php
$prevArea = '';
$total = 0;
$currentQty = 0;
?>
<?php foreach ($summaryPerArea as $key => $value): ?>
<?php $currentQty = $value['totalQty']; ?>
<?php $total += $value['totalQty']; ?>
<?php if ($value['area'] != $prevArea): ?>
<tr class="bg-info">
<?php if ($key != 0) {$total = $currentQty;} ?>
<td colspan="3"><?php echo $value['area']; ?></td>
<?php $prevArea = $value['area']; ?>
</tr>
<?php endif; ?>
<tr>
<td><?php echo $value['area']; ?></td>
<td><?php echo $value['material']; ?></td>
<td><?php echo $value['totalQty']; ?></td>
</tr>
<?php if ($value['area'] == $prevArea): ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
<?php endif; ?>
<?php endforeach ?>
</tbody>
</table>
</div>
<?php endif ?>
query:
SELECT d.SOffcNm as area,
c.ProdNm as material,
SUM(Qty) as totalQty
FROM BigEMerchandiser.dbo.tbl_Delivery_H as a
INNER JOIN BigEMerchandiser.dbo.tbl_Delivery_D as b
ON a.TransCtr = b.TransCtr
INNER JOIN BigESales.dbo.tbl_Materials as c
ON b.Material = c.ExtMatGrp
INNER JOIN BigESales.dbo.tbl_Customers as d
ON a.CustCode = d.CustCode
WHERE d.SOffc LIKE ISNULL('%' + #area + '%', d.SOffc)
AND a.DtRcv BETWEEN #DtRcvFrom AND #DtRcvTo
GROUP BY d.SOffcNm,
c.ProdNm
ORDER BY d.SOffcNm asc
current result:
Thankyou. I appreciate your help.
try to change your code according to the comments in the code below (only the foreach part)
<?php foreach ($summaryPerArea as $key => $value): ?>
<?php if ($value['area'] != $prevArea and $prevArea): // show total when changing area from second area onward ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
<?php $total = 0; // reset the total after displaying it ?>
<?php endif; ?>
<?php if ($value['area'] != $prevArea):// show area header at every area change ?>
<tr class="bg-info">
<td colspan="3"><?php echo $value['area']; ?></td>
</tr>
<?php endif; ?>
<?php //$currentQty = $value['totalQty']; // does not needed ?>
<?php $total += $value['totalQty']; ?>
<tr>
<td><?php echo $value['area']; ?></td>
<td><?php echo $value['material']; ?></td>
<td><?php echo $value['totalQty']; ?></td>
</tr>
<?php $prevArea = $value['area']; // set prevArea to the processed row ?>
<?php endforeach ?>
<?php // show the last total ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
notice that the order of the rows are repeated as follows:
--area total--
--area header--
--area item--
and followed by:
--area total--
on the first foreach, prevArea is still '' so the first condition in the --area total-- (and $prevArea) would result in false, so that the --area total-- is suppressed, but the --area header-- does not have that condition, so the --area header-- is not suppressed.
I use a different syntax of php, using if(condition){code};
If I understood your problem, I make a different solution, I controll if the next is different from the value that i use now, because if it was different you need to print.
I rewrite your code like this:
<tbody>
<?php
$prevArea = 'AnElementLikeFlag';
$total = 0;
$currentQty = 0;
?>
<?php foreach ($summaryPerArea as $key => $value): ?>
<?php $currentQty = $value['totalQty']; ?>
<?php if ($value['area'] != $prevArea && $value['area']!="AnElementLikeFlag"): ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
<tr class="bg-info">
<td colspan="3"><?php echo $value['area']; ?></td>
<?php $prevArea = $value['area']; ?>
</tr>
<?php $total=0; ?>
<?php endif; ?>
<?php $total += $value['totalQty']; ?>
<tr>
<td><?php echo $value['area']; ?></td>
<td><?php echo $value['material']; ?></td>
<td><?php echo $value['totalQty']; ?></td>
</tr>
<?php endforeach ?>
</tbody>
I hope I help you and I maybe make an error.
P.S. You print the total every time, because there is a if condition that it is not necessery
i currently have the following:
<?php
$field_name = "text_field";
$field = get_field_object($field_name);
if( isset($field['value'] ): ?>
<table class="">
<tbody>
<tr class="">
<th><?php echo $field['label']; ?></th>
<td><?php echo $field['value']; ?></td>
</tr>
</tbody>
</table>
<?php endif; ?>
my goal is to make the entire table row collapse and not display if there is no value entered.
clearly a novice. thanks for taking a look.
As per ACF documentation, field[‘value’] will always be set.
Instead do if (!empty($field['value']) or just if ($field['value']).
Thus it should look like this:
<?php
$field_name = "text_field";
$field = get_field_object($field_name);
?>
<table>
<tbody>
<?php
if ($field['value']): ?>
<tr>
<th><?php echo $field['label']; ?></th>
<td><?php echo $field['value']; ?></td>
</tr>
<?php endif; ?>
</tbody>
</table>
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'm displaying a set of rows here for every car I have in my database.
Each row has a form field where a logged in user can submit an offer.
When a user has made an offer for any car, the form field is replaced by a text displaying the value of the offer submitted.
What I'm experiencing, however, is less than ideal results.
If I make an offer for one row, great, the logic works. If I go ahead and make another offer for another row then the logic works, except the fact that the previous row now displays the form again.
I can provide more details if necessary but perhaps someone is familiar with this already.
Thanks in advance.
<?php
require("db-connect.php");
$display = "SELECT filename, car_id, make, model, year, mileage, vin, description, GROUP_CONCAT(filename) FROM scraplis_cars LEFT JOIN scraplis_images USING (car_id) GROUP BY car_id ORDER BY date_time DESC";
$dResult = mysql_query($display) or die('error:' . mysql_error());
$offer = "SELECT car_id, user_id, offer_id, value FROM scraplis_offers WHERE user_id = '".$_SESSION['user_id']."'";
$oResult = mysql_query($offer) or die('Error ' . mysql_error());
$oRow = mysql_fetch_array($oResult);
if(!isset($_SESSION['access'])){
header("location:index.php");
}
?>
<?php if($dResult): ?>
<table class="post">
<thead>
<tr>
<?php if(isset($_SESSION['email']) && $_SESSION['access'] == 0) : ?>
<th scope="col">Images</th>
<th scope="col">Make</th>
<th scope="col">Model</th>
<th scope="col">Year</th>
<th scope="col">Mileage</th>
<th scope="col">VIN #</th>
<th scope="col">Description</th>
<th scope="col">Offer</th>
</tr>
</thead>
<tbody>
<?php while($dRow = mysql_fetch_array($dResult)) : ?>
<?php $str = $dRow[8]; ?>
<?php $images = explode(',', $str); ?>
<tr>
<td>
<ul>
<?php if(!empty($str)) : ?>
<?php foreach($images as $value) :?>
<li>
<a href="images/<?php echo $value; ?>" rel="lightbox[<?php echo $row['car_id']; ?>]">
<img src="images/<?php echo $value; ?>"/>
</a>
</li>
<?php endforeach; ?>
<?php endif; ?>
<ul>
</td>
<td><?php echo $dRow['make']; ?></td>
<td><?php echo $dRow['model']; ?></td>
<td><?php echo $dRow['year']; ?></td>
<td><?php echo number_format($dRow['mileage']); ?></td>
<td><?php echo $dRow['vin']; ?></td>
<td><span><?php echo $dRow['description']; ?></span></td>
<td>
<?php if($oRow['car_id'] == $dRow['car_id']) : ?>
Offer pending approval - $<?php echo $oRow['value']; ?>
<?php else : ?>
<form id="offer" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="text" id="price" name="offer" />
<input type="hidden" name="submitted" value="<?php echo $dRow['car_id']; ?>" />
<input type="submit" name="price" value="Submit" />
</form>
<?php endif; ?>
</td>
</tr>
<?php endwhile; ?>
<?php else : ?>
<th scope="col">Delete</th>
<th scope="col">Images</th>
<th scope="col">Make</th>
<th scope="col">Model</th>
<th scope="col">Year</th>
<th scope="col">Mileage</th>
<th scope="col">VIN #</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<?php while($dRow = mysql_fetch_array($dResult)) : ?>
<?php $str = $dRow[8]; ?>
<?php $images = explode(',', $str); ?>
<tr>
<td>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="checkbox" name="record" value="<?php echo $row['car_id']; ?>" />
<input type="submit" name="delete-car" value="Delete" />
</form>
</td>
<td>
<ul>
<?php if(!empty($str)) : ?>
<?php foreach($images as $value) :?>
<li>
<a href="images/<?php echo $value; ?>" rel="lightbox[<?php echo $row['car_id']; ?>]">
<img src="images/<?php echo $value; ?>"/>
</a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</td>
<td><?php echo $dRow['make']; ?></td>
<td><?php echo $dRow['model']; ?></td>
<td><?php echo $dRow['year']; ?></td>
<td><?php echo number_format($dRow['mileage']); ?></td>
<td><?php echo $dRow['vin']; ?></td>
<td><span><?php echo $dRow['description']; ?></span></td>
</tr>
<?php endwhile; ?>
<?php endif; ?>
</tbody>
</table>
<?php endif; ?>
One important thing for security first:
SEARCH:
if(!isset($_SESSION['access'])){
header("location:index.php");
}
REPLACE WITH:
if(!isset($_SESSION['access'])) {
header("Location: index.php");
exit;
}
Take a look in the PHP documentation for header() or exit() - both describe the need (or security issue) of exit() here afair.
To your question:
You just have the first row of $oResult in $oRow - so you have (for example) 1000 cars but just one offer. You need to fetch the results of $oResult within a loop (while(), for(),... - what you prefer...) and then check wether you can find car_id (within $dRow also in the offers).
code sample (very easy for understanding):
<?php
// ...
// get the offers
// info: user_id would not be necessary here ;-)
$offer = "SELECT car_id, user_id, offer_id, value FROM scraplis_offers WHERE user_id = '".$_SESSION['user_id']."'";
$oResult = mysql_query($offer) or die('Error ' . mysql_error());
$oRows = array();
while($oRow = mysql_fetch_array($oResult)) {
$oRows[$oRow['car_id']] = array(
'offer_id' => $oRow['offer_id'],
'value' => $oRow['value']
);
}
// looping the through the cars
// just the while()-loop based on your code
while($dRow = mysql_fetch_array($dResult)) {
// check if offer exists
if(array_key_exists($dRow['car_id'], $oRows)) {
// H A V E an offer for that car ;-) - show offer details
} else {
// H A V E N O offer that car - show form
}
}
// ...
?>
I hope I didn't get you wrong, made no mistakes (needed to get up early) and this helps you ;-).