i have two tables one item table and customer table:
in the table you can see the second item item id 1002 have two entries.and i want to add colspan to that item for column 1 and 3.
<table>
<tr>
<th>Item ID</th>
<th>Item Color</th>
<th>Customer</th>
</tr>
<?php
$sql = mysqi_query($con,"select * from item_table");
while($row = mysqli_fetch_array($sql)){
?>
<tr>
<td><?=$row['item_id'];?></td>
<td><?=$row['item_color'];?></td>
<td>
<select>
<?php
$sql_cust = mysqli_query($con,"select * from customer_tbl");
while($row_cust = mysqli_fetch_array()){
if($row['customer_id'] == $row_cust['customer_id']){
echo "<option selected='selected' >".$row['customer_name']."</option>";
}else{
echo "<option>".$row['customer_name']."</option>";
}
<?php
}
?>
</select>
</td>
</tr>
<?php
}
?>
</table>
But it print in normal way, i have no idea how to add rowspan in loop..please suggest some logic to solve its appreciated.
You can try it this way:
First, add a counter to your query that will indicate how many entries has a given item.
$sql_cust = mysqli_query($con,
"SELECT *, (SELECT COUNT(*) FROM item_table as it
WHERE it.item_id = item_table.item_id) as c
FROM item_table");
Then, when looping through the items you will set the rowspan to the number of entries the item has. Below is the whole code adjusted.
<?php
$sql = mysqi_query($con,
"SELECT *, (SELECT COUNT(*) FROM item_table as it
WHERE it.item_id = item_table.item_id) as entry_count
FROM item_table");
$buffer = [];
while($row = mysqli_fetch_array($sql)){
if(!isset($buffer[$row[$item_id]])) {
$buffer[$row[$item_id]] = 1;
}
?>
<tr>
<?php if(!isset($buffer[$row[$item_id]])) {?>
<td rowspan="<?=$row['entry_count']?>"><?=$row['item_id'];?></td>
<?php }?>
<td><?=$row['item_color'];?></td>
<?php if(!isset($buffer[$row[$item_id]])) {?>
<td rowspan="<?=$row['entry_count']?>">
<select>
<?php
$sql_cust = mysqli_query($con,"select * from customer_tbl");
while($row_cust = mysqli_fetch_array()){
if($row['customer_id'] == $row_cust['customer_id']){
echo "<option selected='selected' >".$row['customer_name']."</option>";
}else{
echo "<option>".$row['customer_name']."</option>";
}
<?php
}
?>
</select>
</td>
<?php }?>
</tr>
<?php
}
?>
Note that I added a buffer where I set which item was already displayed. That array is used so you only open one td with the wanted rowspan, instead of doing it on every iteration.
i have a simple idea
give TD a ID like
<td id="dynamically-Generate"> (you need to verify that TD id need to be equal in .rowSpan ="here" inside script )
and set this TD if dynamically-Generate is greater than 1 then don't show
and again if dynamically-Generate is greater than 1 then then use this script
<script>
document.getElementById("dynamically-Generate").rowSpan = "dynamically-Generate";
</script>
use script inside loop and both dynamically-Generate need to be same inside everyloop and change after each loop
Related
I'm working on a PHP script that uses delete and I only want to delete 1 row. But everytime I tried to delete 1 row, the SQL executes but deletes the entire rows in the table.
The following is my PHP script
<table>
<tr>
<th>No.</th>
<th>Publisher Code</th>
<th>Publisher Name</th>
<th>City</th>
<th> </th>
</tr>
<!-- PHP FETCH/RETRIEVE FROM DB -->
<?php
include_once 'dbconnect.php';
$sql = mysqli_query($conn, "SELECT * FROM tbl_publisher");
$ctr = 1;
$record = mysqli_num_rows($sql);
if ($record > 0) {
while ($record = mysqli_fetch_array($sql)) {
?>
<tr>
<td> <?php echo $ctr++ ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_CODE']; ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_NAME']; ?> </td>
<td> <?php echo $record['CITY']; ?> </td>
<td id="actions">
<center>
Delete
</center>
</td>
</tr>
<!-- closing tag for php script -->
<?php
}
}
?>
<!-- -->
</table>
and below is my delete.php
if(isset($_GET['del-pub']))
{
$row = intval($_GET['del-pub']);
$sql = mysqli_query($conn,"DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;");
if ($sql) {
header("Location: publisher.php");
}
else {
echo "<script>alert('Publisher was not deleted.');</script>";
header("Location: publisher.php");
}
}
I want to know how to delete only the 1 row. But it keeps deleting the entire rows in the table.
After dumping the contents of $sql, I found out that my sql syntax is the culprit.
Since TBL_PUBLISHER_CODE uses char as ID. Instead of doing "="
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;
I used
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE LIKE '".$row."'
I have a problem for my program.
This my script:
$x=1;
$no=1;
$tot=0;
$bobot=0;
$jumlah=4;
for($i=1;$i<=$jumlah;$i++) {
<tr>
<th> echo $no++; </th>
for($a=$i;$a<=$jumlah;$a++) {
echo "<td>".$x/$a."</td>";
}
<td>Count</td>
</tr>
}
and this is my result
result page
I want to replace the words "count" with the sum of each column in the same row. and my question, how I can add all column in one rows.
example : row 1 = 1 + 0.5 + 0,33 + 0.25 = 2,08
Thank you for helping, I am sorry if my English so bad.
Try something like this
initialize $count as zero before the inner loop
and in inner loop increment county
<?php
$x=1;
$no=1;
$tot=0;
$bobot=0;
$jumlah=4;
for($i=1;$i<=$jumlah;$i++) {
?>
<tr>
<th> <?php echo $no++;?> </th>
<?php
$count=0;
for($a=$i;$a<=$jumlah;$a++) {
echo "<td>".$x/$a."</td>";
$count=$count+($x/$a);
}
?>
<td><?=$count;?></td>
</tr>
<?php
}
?>
I'm making a report where i need to filter details by category. I have two categories in my itr table which are Resident and 4Ps.
So far, this is my code. I can print all the details of the itr table here.
<table>
<tr>
<th style = "padding-right:80px;padding-left:150px;">
<center>Name</center></th>
<th style = "padding-right:10px;padding-left:15px;"><center>Age</center>
</th>
<th style = "padding-right:10px;padding-left:20px;">
<center>Gender</center></th>
<th style = "padding-right:30px;padding-left:40px;">
<center>Purok</center></th>
</tr>
<?php
$query = $conn->query("SELECT * FROM itr LIMIT 30") or
die(mysqli_error());
for($a = 1; $a <= 30; $a++){
$fetch = $query->fetch_array()
?>
<tr>
<td><?php echo $a.". ".$fetch['firstname']." ".$fetch['firstname']?></td>
<td><center><?php echo $fetch['age']?></center></td>
<td><center><?php echo $fetch['gender']?></center></td>
<td><center><?php echo $fetch['address']?></center></td>
</tr>
<?php
}
$conn->close();
?>
What I want is to print only those who are in 4Ps category using a button like "Filter by Category" where i can choose to filter by resident or 4Ps so that i can use only one php file. I don't know what how to do it. Please Help :)
this is the html of for category
<label for = "category">Please select category:</label>
<select style = "width:22%;" class = "form-control" name = "category" required = "required">
<option value = "">--Select category--</option>
<option value = "RESIDENT">RESIDENT</option>
<option value = "4Ps">4Ps</option>
</select>
<br />
This is my itr table:
And this is my desired output:
1.You need to put category-select-box inside a form
2.When user submits the form based on coming category,query need to be changed.
So do like below:-
<form method="POST">
<label for = "category">Please select category:</label>
<select style = "width:22%;" class = "form-control" name = "category" required = "required">
<option value = "">--Select category--</option>
<option value = "RESIDENT">RESIDENT</option>
<option value = "4PS">4Ps</option> <!-- changed option value to capitals-->
</select>
<input type="submit" name="submit" value="submit">
</form>
<table>
<tr>
<th style = "padding-right:80px;padding-left:150px;">
<center>Name</center></th>
<th style = "padding-right:10px;padding-left:15px;"><center>Age</center>
</th>
<th style = "padding-right:10px;padding-left:20px;">
<center>Gender</center></th>
<th style = "padding-right:30px;padding-left:40px;">
<center>Purok</center></th>
</tr>
<?php
if(isset($_POST['category']) && $_POST['category'] !==''){
$category = $_POST['category'];
$query = $conn->query("SELECT * FROM itr where category = '$category' LIMIT 30") or die(mysqli_error());
}else{
$query = $conn->query("SELECT * FROM itr LIMIT 30") or die(mysqli_error());
}
for($a = 1; $a <= 30; $a++){
$fetch = $query->fetch_array()
?>
<tr>
<td><?php echo $a.". ".$fetch['firstname']." ".$fetch['firstname']?></td>
<td><center><?php echo $fetch['age']?></center></td>
<td><center><?php echo $fetch['gender']?></center></td>
<td><center><?php echo $fetch['address']?></center></td>
</tr>
<?php
}
$conn->close();
?>
Note:- Your code is wide-open for SQL INJECTION. to prevent from it use prepared statements
Reference:-
mysqli prepared statements
PDO prepared statements
Although I prefer AliveToDie's solution I want to give you an alternative that does not refresh the page on category selection.
You could show and hide columns with javascript when you change the value of your category select.
For that you need to give the tr's a class with the category name
<!-- table with id for easy js -->
<table id="itr_table">
<!-- table headers... -->
<!-- table body -->
<tr class="tr_body tr_<?php echo $fetch['category'] ?>">
<td><?php echo $a.". ".$fetch['firstname']." ".$fetch['firstname']?></td>
<td><center><?php echo $fetch['age']?></center></td>
<td><center><?php echo $fetch['gender']?></center></td>
<td><center><?php echo $fetch['address']?></center></td>
</tr>
<!-- category select, I added a changelistener -->
<select style = "width:22%;" class = "form-control" name = "category" required = "required" onchange="filterByCategory(this)">
<option value = "">--Select category--</option>
<option value = "1">RESIDENT</option> <!-- the value should be the actual id of the category. -->
<option value = "2">4Ps</option>
</select>
<!-- javascript -->
<script>
filterByCategory(selectElement){
category = $(selectElement).value();
//hide all trs
$('#itr_table .tr_body').hide();
//show trs with chosen category
$('#itr_table .tr_'.category).show();
}
</script>
Please note that this is not tested and may have some errors. I just wanted to give you an idea on how to approach the problem.
How to make so a checkbox is added beside each row in a dynamic table? So if i get 5 result from databse i want to display 5 checkbox next to these results.
<table border="2">
<tr>
<th>title</th>
</tr>
<?php
include 'database.php';
$valid_query = "SELECT * FROM agencies ";
$valid_result = mysqli_query($link, $valid_query);
while ($row = mysqli_fetch_array($valid_result)) {
echo '<tr>';
echo '<td>'.$row['title'].'</td>';
<input type = 'checkbox' name = '$row[title]'>
echo '<tr>';
}
?>
</table>
i need to display all courses at once
in my code only display one course what should i do to fix it ?
when i click delete it reload page and display next courses
my code ::
$course = $conn->query("select * from student_course where student_id = $id ") ;
if($course->num_rows > 0) {
?>
<table class="table" >
<tr>
<thead>
<th> courseName </th>
<th> courseID </th>
<th> teacher </th>
<th> DELETE </th>
</thead> </tr>
<?php
while ($Scourse = $course->fetch_assoc()){
$cid=$Scourse['course_id'];
$tid=$Scourse['teacher_id'];
$teacher = $conn->query("select * from teacher where id = $tid ") ;
if($teacher->num_rows > 0) {
while($teacherC=$teacher->fetch_assoc()){
$course = $conn->query("select * from course where id = '$cid' ") ;
if($course->num_rows>0){
while($Ncourse=$course->fetch_assoc()){ ;
?>
<tr> <td><?php echo $Ncourse['name']; ?> </td>
<td><?php echo $Scourse['course_id']; ?> </td>
<td> <?php echo $teacherC['name']; ?> </td>
<td><form method="post"><button type="submit" name="<?php echo $cid ?>"> Delete </button></form> </td></tr>
<?PHP
if (isset($_POST["$cid"])){
$dcourse = $conn->query("delete from student_course where course_id = '$cid' and
student_id=$id ") ;
header("location:scourse.php");
}
}
}
}
}
}
}
</table>
i try to delete teacher and course query it work as i need
You're handling associate arrays improperly.
You will need a foreach loop to loop through each row in the associate array.
Example:
foreach($Scourse as $x) {
echo $x['name'];
echo $x['course_id'];
}
See Loop through Associate Arrays here:
https://www.w3schools.com/php/php_arrays.asp
Edit: See #Qirel comments