i have this table of trucks.
<div class="myform">
<form id="form" name="form" method="post" action="">
<table border="1">
<tr>
<th>Plate Number</th>
</tr>
<?php do { ?>
<tr>
<td><?php echo strtoupper($row_Trucks['truck_plate_no']); ?></td>
</tr>
<?php } while ($row_Trucks = mysql_fetch_assoc($Trucks)); ?>
</table>
</form>
</div>
and this list of trucks is related to a delivery details table
tbl_delivery_details, tbl_po, tbl_waybill, tbl_fuel_details, tbl_driver...
and also connected to a bunch of other tables in the database. How can i display the records related to each of the trucks when i click the truck plate number on the table and link me to another page that will show me all the records related to that truck, the truck table is a foreign key on the other tables.
Try something like this.
If truck_plate_number is unique,you can pass it along with the link as shown below.Otherwise,get the ID of record(primary key) and replace the $truck_plate_no with that ID in the below code.
<div class="myform">
<form id="form" name="form" method="post" action="">
<table border="1">
<tr>
<th>Plate Number</th>
</tr>
<?php do {
$truck_plate_no=$row_Trucks['truck_plate_no'];
echo '<tr><td><a href=anotherpage.php?truck_plate_no='.$truck_plate_no.'>'.strtoupper($truck_plate_no).'</td></tr>';
} while ($row_Trucks = mysql_fetch_assoc($Trucks));
?>
</table>
In anotherpage.php
<?php
$truck_plate_number=$_GET['truck_plate_no'];//get value from url
?>
Then query, details from the table using this id.
Related
Help needed!
I have done coding using php mvc technique, using wamp server and tried using datatables(only for search functionality).
I have a table display in my Admin index page which fetches data from mysql database. I also have checkbox included at each row so that it can be selected and deleted.
...
<form action="<?php echo URLROOT; ?>/posts" method="post">
<button type="submit" value="Delete" name="delete_multiple_data">Delete Multiple Items</button>
<table id="mtab" class="display table table-striped table-bordered dt-responsive nowrap" style="width:100%">
<thead class="thead-dark">
<tr class="danger">
<tr class="danger">
<th scope="col">ID</th>
<th scope="col">Pack Name</th>
<th scope="col">Select</th>
</tr>
</thead>
<tbody>
<?php foreach($data['posts'] as $post) : ?>
<tr>
<td><?php echo $post->id; ?></td>
<td><?php echo $post->pckname; ?></td>
<td><input type="checkbox" name="delete[]" value="<?php echo $post->id; ?>"></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
...
my controller,
...
if(isset($_POST['delete_multiple_data'])){
if(isset($_POST['delete'])){
foreach($_POST['delete'] as $deleteid){
$deletepack = $this->postModel->deletePost($deleteid);
}
}
}
$this->view('posts/index', $data);
...
without table id="mtab"(removing table id="mtab") everything is working fine.
but for search to be enabled in my table id="mtab" i used the below script,
...
<script type="text/javascript">
$(document).ready(function(){
$("#mtab").DataTable({
});
});
</script>
...
if i use this script, it hides my checkboxes totally from display, please help
I have identified the error it was <table class="display table table-striped table-bordered dt-responsive nowrap" style="width:100%">
after I removed nowrap it was working fine, both my checkbox as well as search feature was visible.
Thanks all.....
This is my controller, that i use to send the row to the model:
function excluir(){
$codcliente = $this->input->post('codcliente');
$this->load->model("Cliente_class");
$this->Cliente_class->excluirCliente($codcliente);
redirect('cliente/busca');
}
my model:
function excluirCliente($codcliente){
$this->db->delete('cliente', array('codcliente' => $codcliente));
}
my table (view):
<form action="#" method="POST" id="form">
<div style="float: left;">
<input type="button" onclick="cadastro();" value="Novo cliente" name="botao" class="btn btn-primary">
</div>
<div class="dataTable_wrapper">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Código</th>
<th>Nome</th>
<th>Cidade</th>
<th>Telefone</th>
<th> </th>
</tr>
<tbody>
<?php
foreach ($records as $row){ ?>
<tr>
<td><input name="codcliente" id="codcliente" value="<?php echo $row->codcliente ?>"></td>
<td><?php echo $row->nome ?></td>
<td><?php echo $row->cidade ?></td>
<td><?php echo ($row->telefone == null) ? "-" : $row->telefone ?></td>
<td><center><input type="button" onclick="excluir();" value="Delete"></td>
</tr>
<?php } ?>
</tbody>
</form>
</thead>
</table>
and my JS:
function excluir(){
document.forms['form'].action = "<?php base_url();?>excluir";
document.forms['form'].submit();
}
its working fine, but the code is deleting my last row in relation with the table on html. i used the order by too see whats beeing deleted, and it works the same...
i dont know what's wrong
Your problem is that you are using the same form name name="codcliente" for all your rows and then submitting the whole form when you delete. The server sees the last value for codcliente which will be your last row and hence deletes it.
To fix, I suggest you use unique form names for each row for all your row fields. Eg. Instead of name="codcliente" use name="codcliente_<?php echo $row->id ?>". And then when you post to the server to delete, use a data-name=<?php echo $row->codcliente ?> on the delete button and in the onClick handler use $(this).data('name') to get the unique name of the field and post that to the server. I suggest you use ids instead of names as well.
I want to fetch some data from one table in the same page when a row of other table is clicked.
Here is the code for of what I am trying to do and I am stuck:
<?php
$db_table="area";
$db_query='select areaid,area_name from ' . $db_table;
$query_result=mysqli_query($dbc,$db_query) or die('error querying db');?>
<div id="area1" >
<h3>Select ony two area from Category </h3>
<form>
<fieldset style="float: left; width: 25%">
<legend>Project Area</legend>
<table border=1>
<tr>
<th> </th>
<th> Area Name </th>
</tr>
<?php
while($row=mysqli_fetch_array($query_result)){
?>
<tr id="<?php echo $row['areaid'];?>" class="area" onlick="showfaculty()">
<td> <input class="selectarea" type="radio" name = "proarea" value="<?php echo $row['areaid'];?>"></td>
<td> <?php echo $row['area_name']; ?></td>
</tr>
<?php }
echo '</table></br></br>
</fieldset>
<fieldset id="avaguide" style="width: 25%">
<legend>Available Guide</legend>
<table border=1>
<tr>
<th> </th>
<th> Available Guides </th>
</tr>';
$db_table2="facultytable";
$db_query='select facultyID,facultName from ' . $db_table2.' where project_count<2';
$query_result=mysqli_query($dbc,$db_query) or die('error querying db');
while($row=mysqli_fetch_array($query_result)){
?>
<tr id="<?php echo $row['facultyID'];?>" class="area">
<td> <input class="selectguide" type="radio" name = "areaguide" value="<?php echo $row['facultyID'];?>"></td>
<td> <?php echo $row['facultName']; ?></td>
</tr>
<?php }
echo '</table></fieldset>
</form>
</div>;`
Now my code is working fine it shows both area and faculty table. But it displays full faculty table. What I want to do is when click the row of area table some function should be called which get the all the faculty id of the selected area id from the table faculty_area(a_id, f_id) and faculty table shows only the name of faculty with fetched faculty id. An example of that function is below.
function showFaculty($aeraid){
$db_table2="faculty_area";
$db_query='select f_id from ' . $db_table2.' where a_id = '.$areaid;
$query_result=mysqli_query($dbc,$db_query) or die('error querying db');
while($row=mysqli_fetch_array($query_result)){ some code that can be used to display faculty table with feteched f_id}
Please tell me how can I do it.
In my opinion the best way to do this task is using AJAX or you can build one page application with JS and RESTful service on PHP. I like AngularJS as frontend.
I am trying to delete -- using checkboxes -- an HTML table row/s populated by SQL data using PHP. I am new to this, so I would appreciate any improvements to my code. (especially SQL-injection, which I read quite a few times while researching)
Part of HTML with a table - inv_main.php
...
<div class="mid">
<h1>Inventory</h1>
<div class="content">
<!-- buttons -->
<div class="inv_btn">
Add
<!-- button to press to delete ticked checkboxes -->
<form action="inventory.php" method="post">
<input name="delete" type="submit" id="delete" value="Delete"/>
</form>
</div>
<!-- TABLE -->
<div id="inv_tbl">
<table class="table table-striped" id="tblSearch">
<thead>
<tr>
<th>Product Name</th>
<th>Supplier Name</th>
<th>Category</th>
<th>Unit Price</th>
<th>Retail Price</th>
<th>Est. profit per unit</th>
</tr>
</thead>
<tbody>
<?php foreach($allInfo as $info): ?>
<tr>
<td> <?= $info['product'] ?> </td>
<td> <?= $info["supplier_name"] ?> </td>
<td> <?= $info["category"] ?> </td>
<td>$ <?= $info["unit_price"] ?> </td>
<td>$ <?= $info["retail_price"] ?> </td>
<td>$ <?= number_format(($info["unit_price"] - $info["retail_price"]), 2) ?></td>
<td><input type="checkbox" name="checkbox[<?= $info['product'] ?>"] id="checkbox[]" value="<?= $info['product'] ?>" method="post"/></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
</div>
</div>
...
PHP - inventory.php
<?php
/**
* inventory.php
* configures the inventory page.
*/
// configuration
require("../includes/config.php");
require("../includes/render_dash.php");
// if user reached page via GET...
if($_SERVER["REQUEST_METHOD"] == "GET")
{
// get all the info from the inventory database
$allInfo = [];
$getInfo = query("SELECT * FROM inventory WHERE id = ?", $_SESSION["id"] );
foreach($getInfo as $info)
{
$allInfo[] = [
"product" => $info["product"],
"unit_price" => number_format($info["unit_price"], 2),
"retail_price" => number_format($info["retail_price"], 2),
"supplier_name" => $info["supplier_name"],
"category" => $info["category"],
];
}
// render HTML
render("inv_main.php", ["title" => "Inventory", "allInfo" => $allInfo ] );
}
Note the 'form' right after the comment <!-- button to press to delete ticked checkboxes --> and the last table data inside the tbody tag.
I already saw similar questions but I still can't figure it out. If it helps, my PRIMARY KEYS in the inventory database are the user's id and product. The id is used for the $_SESSION, so using that would delete all the inventory data for, say user id #30 so I'm trying to reach the product, which is a string.
I'm going to try to explain this as best I can. I'm returning an array of results from my controller. I wish to send over the primary key of the selected row to another controller that will manage inserting the record into the Database when a user clicks the "add employee" button. My primary key is MOVIE_ID. It's currently returning an array of two MOVIE_ID {12341, 31351} like so. My questions are:
How do I associate each movie ID uniquely with that corresponding rows' button?
Can I pass a variable to a controller through the action="" attribute , do I need JS?
<fieldset id= "results">
<?php if (isset($results ) And count($results)) : ?>
<table id="box-table-a" summary="Employee Sheet">
<thead>
<tr>
<th scope="col">Employee</th>
<th scope="col">Salary</th>
<th scope="col">Bonus</th>
<th scope="col">Supervisor</th>
<th scope="col">Action</th>
</tr>
<?php foreach ($results as $result) : ?>
<tr>
//Primary key <?php echo $result['MOVIE_ID!'];?>
<td><span class="Employee_name"><?php echo $result['Employee']; ?> </span></td>
<td><span class="Salary"><?php echo $result['Salary']; ?> </span> </td>
<td><span class="Bonus"><?php echo $result['Bonus']; ?> </span> </td>
<td><span class="Supervisor_name"><?php echo $result['Supervisor']; ?> </span></td>
<td><input type="submit" value="add employee" > </td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</thead>
<tbody>
</fieldset>
One trick is to pass the id via the submit button, like so:
<input name="submit[12341]" type="submit" value="add employee" />
When clicked it would send submit[12341]=add+employee in the POST data (assuming you have wrapped everything inside a <form> tag). The other buttons will not be submitted, only the one that's clicked.
In PHP you can retrieve the id like so:
$id = key($_POST['submit']);