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

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

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>";
?>

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

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>";
?>

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

A simple php script error result with tables

Hello guys i'm in need of php experts advice.
Here's my problem. A user has 2 inputs the start and the end this are all integers. this will able to identify odd and even. i have solve already odd and even and almost done. main problem ex. start value 1 end 5. 1 is odd it should be displayed on odd in table. but the problem is it is found in even table. initial value is the problem. the rest was good.
here's my code
<?php
$firstnum = $_POST['first_input'];
$secondnum = $_POST['second_input'];
$counter = 0;
echo "<table border='1'>";
if ($firstnum < $secondnum) {
echo "<tr>"; //first tr
echo "<th>"; echo "Even numbers"; echo "</th>";
echo "<th>"; echo "Odd numbers"; echo "</th>";
echo "</tr>";
for ($counter=$firstnum; $counter <= $secondnum ; $counter++) {
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
} else {
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}
}
} elseif ($firstnum > $secondnum) {
# code...
//first num is < second num
echo "<tr>"; //first tr
echo "<th>"; echo "Even numbers"; echo "</th>";
echo "<th>"; echo "Odd numbers"; echo "</th>";
echo "</tr>";
for ($counter=$firstnum; $counter >= $secondnum ; $counter--) {
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
} else {
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}
}
}
echo "</table>";
?>
Your issue is you have invalid html resulting from your if/else blocks.
If your if you have
<tr>
<td><td>
and in your else you have
<td></td>
</tr>
Both of these need full row/cell tags
<tr>
<td></td>
<td></td>
</tr>
So your code should look like
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
echo "<td>";
echo "</td>";
echo "</tr>";
} else {
echo "<tr>";
echo "<td>";
echo "</td>";
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}

Table formatting in PHP

I am trying to make a table in PHP.
All things are fine but all the rows are printed in same line.
How to print new line after each row in table? But in html there is no need to write extra code for new line why it is showing not a new line with PHP?
Here is that part of code:
<div class="contest-table" id="contest-table">
<table class="contest-details" id="contest-details">
<tr>
<th>CODE</th>
<th>NAME</th>
<th>START</th>
<th>END</th>
</tr>
<?php
//Show contest detials -> Contest Code|Contest Name |Start Date | End Date
$con=mysqli_connect("localhost","root","chandan","judge");
$result=mysqli_query($con,"SELECT * FROM judge_contest ");
echo "<tr>";
while($row = mysqli_fetch_array($result))
{
$contest_code=$row['contest_code'];
$contest_name=$row['contest_name'];
$contest_start_date=$row['start_date'];
$contest_end_date=$row['end_date'];
echo "<td>";
echo " $contest_code ";
echo "</td>";
echo "<td>";
echo " $contest_name ";
echo "</td>";
echo "<td>";
echo $contest_start_date;
echo "</td>";
echo "<td>";
echo $contest_end_date;
echo "</td>";
}
echo "</tr>";
?>
</table>
</div>
The problem is obvious, because you have put the statement echo "<tr>" and echo "</tr>" outside the while loop. You should put these two statement into the while loop.
echo '<tr>'
and
echo '</tr>'
should be inside the wile loop

Categories