html after td Rowspan, the Next column data goes to next row - php

Following is the code
I want out put as
http://crysol.com/crysol_soft/Test/Screenshot_3.png
With following code I am getting output as
http://crysol.com/crysol_soft/Test/Screenshot_4.png
echo "<table border='1'>";
echo "<th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th>";
echo '<tr>';
echo "<td rowspan='5'>Cat</td>";
for($i=1;$i<=5;$i++){
echo '<td>'.$i.'</td>';
echo " </tr>";
}
echo "<td rowspan='10'>Good</td>";
?>
What are the changes required

Here is the code with your desired output
used if condition for print only 1 time good column and used rowspan='5'
<?php
echo "<table border='1'>";
echo "<th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th>";
echo '<tr>';
echo "<td rowspan='5'>Cat</td>";
for($i=1;$i<=5;$i++){
echo '<td>'.$i.'</td>';
if($i==1){
echo "<td rowspan='5'>Good</td>";
}
echo " </tr>";
}
?>
and also you can used this code
for($i=1;$i<=5;$i++){
echo '<tr>';
if($i==1){
echo "<td rowspan='5'>Cat</td>";
}
echo '<td>'.$i.'</td>';
if($i==1){
echo "<td rowspan='5'>Good</td>";
}
echo " </tr>";
}

Create Table inside td:
<?php
echo "<table border='1'>";
echo "<tr><th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th></tr>";
echo "<tr><td style=''>Cat</td>";
echo "<td><table style='width:100%;'>";
for($i=1;$i<=5;$i++){
echo "<tr>";
echo "<td style='border-bottom:1pt solid black;'>".$i.'</td>';
echo "</tr>";
}
echo "</table></td>";
echo "<td>Good</td></tr>";
?>

Related

Why Session Variable Sending only last row data to another page

here the problem is i need to view the whole row data in view details page when the button is clicked but when i click the last row data is sent through session variable and last row is displayed but i need particular row to display fully when the name is selected of the employee. please help me out in this .
thank you
index.php
<?php
include('connection.php');
$sql = "SELECT empname,salary,contact_no,department,empdesg FROM add_emp ORDER BY empname ASC ";
$result = $con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
?>
<center>
<table cellpadding="20px" cellspacing="40px;" border="2px" align="center" width="device">
<tr>
<th>Name</th>
<th>Department</th>
<th>Designation</th>
<th> </th>
<?php
while($row = $result->fetch_assoc())
{
$name='';
$name=$row["empname"];
$department=$row["department"];
$designation=$row["empdesg"];
?>
</tr>
<td>
<?php echo $name?>
</td>
<td>
<?php echo $department?>
</td>
<td>
<?php echo $designation?>
</td>
<td><a href="viewdetails.php">
<button>Details</button>
</a>
<?php
$_SESSION['name']= $name;
?>
</td>
<?php
}
}
else {
echo "0 results";
}
?>
viewdetails.php
<?php
session_start();
include('connection.php');
$sql="select * from add_emp where empname='$_SESSION[name]'";
$result = mysqli_query($con, $sql); // First parameter is just return of "mysqli_connect()" function
echo "<br>";
echo " <table cellpadding='5px' cellspacing='10px' border='1px' align='center'>";
while ($row = mysqli_fetch_assoc($result))
{ // Important line !!! Check summary get row on array ..
echo "<tr>";
echo "<th>" ."Name". "</th>";
echo "<td>" .$row['empname']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."DOB". "</th>";
echo "<td>" .$row['eage']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Adhaar Number". "</th>";
echo "<td>" .$row['adhaar']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Address". "</th>";
echo "<td>" .$row['address']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Salary". "</th>";
echo "<td>" .$row['salary']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Phone Number". "</th>";
echo "<td>" .$row['contact_no']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Department". "</th>";
echo "<td>" .$row['department']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Designation". "</th>";
echo "<td>" .$row['empdesg']. "</td>";
echo "</tr>";
echo "<tr>";
}
echo "</table>";
?>

Adding datatables to a table (wordpress)

I'm trying to add datatables to my project and it shows up with the arrows and the search bar, but neither work.
I tried testing the code on a page with just the scripts and table and it worked but when I moved it to my wordpress site I run into problems.
Any suggestions? I have the jquery and datatables scripts and css in the header page, as well as the script for the table itself after the /head:
<script>
$(document).ready(function(){
$('#myTable').DataTable();
});
</script>
My table itself looks like
echo "<table id='myTable' class='display table' width='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Foster ID</th>";
echo "<th>Foster's Name</th>";
echo "<th>City</th>";
echo "<th>E-Mail</th>";
echo "</tr>";
echo "</thead>";
if(!empty($result)){
foreach ($result as $results){
$fosterId = $results->memberId;
$fosterName = $results->memberName;
$city = $results->city;
$email = $results->email;
echo "<tbody>";
echo "<tr>";
echo "<td>$fosterId</td>";
echo "<td>$fosterName</td>";
echo "<td>$city</td>";
echo "<td>$email</td>";
echo "</tr>";
}
} else {
echo "<tr>";
echo "<td colspan='5'>No Fosters</td>";
echo "<tr>";
echo "</tbody>";
}
echo "</table>";
Your PHP code contains tbody element in the loop. Also DataTables doesn't support colspan attribute in table body.
Corrected PHP code:
echo "<table id='myTable' class='display table' width='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Foster ID</th>";
echo "<th>Foster's Name</th>";
echo "<th>City</th>";
echo "<th>E-Mail</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach ($result as $results){
$fosterId = $results->memberId;
$fosterName = $results->memberName;
$city = $results->city;
$email = $results->email;
echo "<tr>";
echo "<td>$fosterId</td>";
echo "<td>$fosterName</td>";
echo "<td>$city</td>";
echo "<td>$email</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
Corrected JavaScript code:
$(document).ready(function(){
$('#myTable').DataTable({
"language": {
"emptyTable": "No Fosters"
}
});
});

Why does PHP nested foreach loop displays only first row from sql database

I am trying to display a table from database using two nested foreach loops with some criteria. here is the code-
echo "<div class='container'>";
echo "<table class='table table-hover table-bordered table-condensed' style='width:95%' align='center'>";
echo "<tr>";
echo "<th>";
echo "Edit";
echo "</th>";
echo "<th>";
echo "Delete";
echo "</th>";
echo "<th>";
echo "Sl. No.";
echo "</th>";
echo "<th>";
echo "Group";
echo "</th>";
echo "<th>";
echo "Component";
echo "</th>";
echo "<th>";
echo "Quantity";
echo "</th>";
echo "</tr>";
if($rslt->rowCount() > 0)
{
foreach($rslt as $item)
{
foreach($rslt3 as $item3)
{
/*echo $item3['component'];
if($item3['component']===$item['component'])
{
if($Qty>=$item3['Qty'])
{
$item3[Qty]=$item3[Qty]-$item[Qty];
*/
//will implement this after the second loop starts working
$id = $item['entry_id'];
$Qty = $item['Qty'];
$group_ID = $item['group_ID'];
$component = $item['component'];
$vendor_ID = $item['vendor_ID'];
echo "<tr>";
echo "<td>";
echo "<a href='production_edit.php?id=$id&Qty=$Qty&group_ID=$group_ID&component=$component&vendor_ID=$vendor_ID'>Edit</a>";
echo "</td>";
echo "<td>";
echo "<a href='production_delete.php?id=$id&vendor_ID=$vendor_ID'>Delete</a>";
echo "</td>";
echo "<td>";
echo $item['entry_id'];
echo "</td>";
echo "<td>";
echo $item['group_ID'];
echo "</td>";
echo "<td>";
echo $item['component'];
echo "</td>";
echo "<td>";
echo $item['Qty'];
echo "</td>";
echo "</tr>";
}
}
}
echo "</table></div><br>";
}
Now the problem here is when I use the second foreach loop the table displays the first entry in each table row... I am curious where I am at fault with this second foreach loop.. Thanks in advance

PHP Table Format

I've put together the following code which creates a table upon a selection being made with a drop down menu.
echo "<table>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
The problem I've got is that for each record that is returned the 'headers' are repeated. The live page can be found here. I just wonder whether someone could perhaps take a look at this please and tell me where I've gone wrong.
Apologies for the really simple question, but I've been looking at this for a while and I just can't find the answer. I think it just needs a fresh pair of eyes to look at it.
Try this, you just need to get headers out of while loop
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
The answer is obvious, you are repeating the output of the headers in the loop. Move the
while($rows=mysql_fetch_array($result)){
after the first
echo "</tr>";
You need to put the headers outside of the while loop:
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result = mysql_query($query);
while ($rows = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $rows['findname'] . "</td>";
echo "<td>" . $rows['finddescription'] . "</td>";
echo "</tr>";
}
echo "</table>";
This should work:
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
Your headers are being repeated because you are writing them inside the loop, for each row returned by the query. You simply need to move your headers outside the loop so they're only written once, before the rows returned by the query start printing:
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
headers are repeating becasue they are in while loop, it should work well
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
Change to:
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
echo "<tr>";
while($rows=mysql_fetch_array($result)){
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";

PHP loop - creating a table using a loop and giving it headers

I am a few stages further in learning PHP but I have come to another annoying pit stop. I have a really simple bit of code that retrieves book items from my database. I am displaying them in an html table however because it is a loop, if I use the th tags for table header I get a header above every single data item!
Here is my code extract: (as you can see I have put my th tags as comments as that doesn't work)
<table border="0">
<br />
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
//echo "<tr>";
//echo "<th>";
//echo "Book Title";
//echo "</th>";
//echo "<th>";
//echo "Book Author";
//echo "</th>";
//echo "<th>";
//echo "Book Publisher";
//echo "</th>";
//echo "<th>";
//echo "Book ISBN";
//echo "</th>";
//echo "</tr>";
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
Move those echos out of your loop. Also, you shouldn't have a <br /> directly inside of a <table> tag.
Move your table header code outside of the loop.
IDIOT! Sorry guys....
Needed to put the th tags outside of the loop.... simple I know but easy to miss when your learning!
[=
Simply take the header outside the loop, so echo before you begin your loop but after the opening<table>
You have to move the headers above the loop:
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>"
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
echo "<td>";
...
What is static, stays static.
Wjat is dynamic, becomes PHP

Categories