Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
My database data is echoed in a table list but I will like for the list to be ordered for reference purposes. I have tried several approaches but it doesn't seem to work. Any way possible? here is my code
<table width="auto" border="0" align="center" cellpadding="2" cellspacing="1" class="text">
<tr align="center" id="listTableHeader">
<td>S/N</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
<?php
while($row = dbFetchAssoc($result)) {
extract($row);
if ($i%2) {
$class = 'row1';
} else {
$class = 'row2';
}
$i += 1;
?>
<tr class="<?php echo $class; ?>">
<td width="70" align="center">Delete</td>
<td></td>
<td><?php echo $Firstame; ?></td>
<td><?php echo $LastName; ?></td>
</tr>
<?php
} // end while
?>
<tr>
<td colspan="5"> </td>
</tr>
<tr>
<td colspan="5" align="right"></td>
</tr>
</table>
I can see some mistakes in your code like disorder of some statements.I don't know what is your actual code.How ever here iI am share with my knowledge through the small example.I think it may be helps to solve your issue.You just look the following code and change it with your tables and database connection credential.If any problem in this,you can tell me.I will give the further solution as I can.
<table width="auto" border="0" align="center" cellpadding="2" cellspacing="1" class="text">
<tr align="center" id="listTableHeader">
<td></td>
<td>S/N</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
<?php
$conn= mysqli_connect("localhost", "root", "", "test");
if(!$conn)
{
echo 'not';die;
}
$result=mysqli_query($conn,"select * from animals");
$i=0;
$j=1;
while($row = mysqli_fetch_array($result)) {
$Firstname=$row['name'];
$LastName=$row['animal'];
$id=$row['animal_ID'];
if ($i%2) {
$class = 'row1';
} else {
$class = 'row2';
}
$i += 1;
?>
<tr class="<?php echo $class; ?>">
<td width="70" align="center">Delete</td>
<td><?php echo $j;?></td>
<td><?php echo $Firstname; ?></td>
<td><?php echo $LastName; ?></td>
</tr>
<?php
$j++;
} // end while
?>
<tr>
<td colspan="5"> </td>
</tr>
<tr>
<td colspan="5" align="right"></td>
</tr>
</table>
Related
Im trying to teach myself php.... I have created a database with 4 elements... region, details, store_name, web_address.
I have created ado-while loop that displays all the records in a 'region'... it displays 'store_name', then 'web_address', then 'details' sorted so all the records with the same 'details' are grouped together;
<table width="600" align="center" border="0" cellspacing="4" >
<tr>
<td colspan="2"><h2>Web Stockists: <strong></strong></h2></td>
<td width="251" colspan="2" align="right"><h3>Region: <?php echo $region_text ?></h3></td>
</tr>
<?php do { ?>
<tr>
<td colspan="2"><strong><?php echo $row_RegionList['store_name']; ?></strong></td>
<td colspan="2" rowspan="2" align="center"> <?php echo '' . $row_RegionList['web_address'] . ''; ?></td>
</tr>
<tr>
<td width="14">
<td width="313"><?php echo $row_RegionList['details']; ?> </tr>
<?php } while ($row_RegionList = mysql_fetch_assoc($RegionList)); ?>
</table>
I now want to make it so every record with the same 'details' value get listed under a 'details' sub heading.
This is my attempt;
<table width="600" align="center" border="0" cellspacing="4" >
<tr>
<td colspan="2"><h2>Web Stockists: <strong></strong></h2></td>
<td width="251" colspan="2" align="right"><h3>Region: <?php echo $region_text ?></h3></td>
</tr>
<?php $details = $row_RegionList['details'];
do { ?>
<tr>
<td width="313"> <h3><?php echo $row_RegionList['details']; ?> </h3></td>
</tr>
<?php do { ?>
<tr>
<td colspan="2"><strong><?php echo $row_RegionList['store_name']; ?></strong></td>
<td colspan="2" rowspan="2" align="center"> <?php echo '' . $row_RegionList['web_address'] . ''; ?></td>
</tr>
<?php } while ($row_RegionList['details'] == $details);
$details = $row_RegionList['details'];
} while ($row_RegionList = mysql_fetch_assoc($RegionList)); ?>
</table>
This makes a heading of 'region' with a sub heading of 'details' but then creates an eternal loop of the first record in the database. can someone show me what I have done wrong?
This is the code below! It echos out all answers for a specific question in my forum. I want about 6 answers shown on each page. Hence if there are 12 answers on one specific question then it should show the first six from the start then if the user clicks "next page" it should show the user the next 6 answers. I don't know how to fix this step in my progress. Any help is more than appreciated! Thanks.
<?php
$tbl_name2="forum_answers"; // Switch to table "forum_answer"
$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";
$result2=mysqli_query($con, $sql2)or die(mysqli_error($con));
while($rows=mysqli_fetch_array($result2)){
?>
<!DOCTYPE HTML SYSTEM>
<table id="answers" width="400" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td id="answerid">#<?php echo $rows['a_id']; ?></td>
</tr>
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" >
<tr>
<?php
INCLUDE 'dbh.php';
$query = mysqli_query($conn,"SELECT * FROM forum_answers LEFT JOIN users ON forum_answers.a_name = users.username WHERE forum_answers.a_id=".$rows['a_id']." AND forum_answers.question_id=".$rows['question_id']);
while ($row = mysqli_fetch_assoc($query)) {
if ($row ['image'] == "") {
echo "<img src='bilder/default.jpg'";
?><td width="77%" bgcolor="#F8F7F1"> <?php echo $rows['a_name']; ?></td><?php
}
else {
echo "<img src='bilder/".$row ['image']."'";
?> <td width="77%" bgcolor="#F8F7F1"> <?php echo $rows['a_name']; ?></td><?php
}
echo "<br>";
}
?>
<td id="datetimeanswer" bgcolor="#F8F7F1"><?php echo $rows['a_datetime']; ?></td>
</tr>
<tr>
<td id="answertext" bgcolor="#F8F7F1"><strong>Answer:</strong></td>
<td bgcolor="#F8F7F1"><?php echo $rows['a_answer']; ?>
<button id="removeanswer" name="remove">Remove</button>
<button id="removeanswer">Edit</button>
</td>
</tr>
</table></td>
</tr>
</table>
I am having a nested table with a while loop, I want to add one more nested table in the same row:
Now I want to add one more nested table as each cd contains more than one data like below:
My code is as follows
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td><?php echo $rowcd['well_no'] ?></td>
<td><?php echo $rowcd['well_name'] ?></td>
<td>
<table border="1" width="100%">
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
/* I want to add one more nested table here*/
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
I tried some thing like this,after my second while loop
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
<td>
<table>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<tr>
<td><?php echo $rowl['logs'] ?></td>
</tr>
<?php
}
?>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
but the result was messed up. I am confused, where I want to end my while loop, I think.
Finally i got as i wish, and i am sharing the code as below
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div class="container">
<table border="1" align="center" border-collapse="collapse">
<thead>
<tr >
<th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th width="100">CD No:</th>
<th width="150">Logs</th>
<th width="100">Bottom Depth</th>
<th width="100">Top Depth</th>
<th width="100">Date of Log</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td align="center"><?php echo $rowcd['well_no'] ?></td>
<td align="center"><?php echo $rowcd['well_name'] ?></td>
<td colspan="5">
<table rules="all">
<tr>
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<td width="100" align="center"><?php echo $rowcd['cd_no'] ?></td>
<td colspan="4">
<table rules="all">
<tr>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<td width="155"><?php echo $rowl['logs'] ?></td>
<td width="105" align="center"><?php echo $rowl['bottom'] ?></td>
<td width="100" align="center"><?php echo $rowl['top'] ?></td>
<td width="100" align="right"><?php echo $rowl['date'] ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
?>
</table>
</td>
<?php
}
}
?>
</tr>
</table>
I hope this is what you meant as per your data table shown above
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<tr>
<td>id</td>
<td>well</td>
<td>name</td>
<td>
<table border="1" width="100%">
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
</table>
</td>
<td>
<table border="1" width="100%">
<tr>
<td>Log1</td>
</tr>
<tr>
<td>Log2</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
I'm trying to do this in while loop PHP, number of assignments are unlimited so rowspan should also adjust itself with the number of rows, is there any proper way to do it with minimum numbers of line?
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>Assignment No</th>
<th>Student Name</th>
<th>Assignment Marks</th>
<th>Overall Result</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">S1</td>
<td align="center">5</td>
<td rowspan="3" align="center">B Grade</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">S1</td>
<td align="center">8</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">S1</td>
<td align="center">7</td>
</tr>
</tbody>
</table>
Try This:
<? foreach($dataarray as $data)
{?>
<tbody>
<tr>
<td align="center"><? echo $data[0]; ?></td>
<td align="center"><? echo $data[1]; ?></td>
<td align="center"><? echo $data[2]; ?></td>
</tr>
<? if($data[3]!=Null) { ?>
<td rowspan="3" align="center"><? echo $data[3]; ?></td>
<? } ?>
<? } ?>
and put if condition for last column
Assuming that data comes in a linear fashion (ie a plain one dimensional array), and that the grade is not in the array, I'd do something like this:
<?
$sizeofArray = count($data);
$rowspan = floor($sizeofArray/3);
for($arrCnt = 0; $arrCnt < $sizeofArray; $arrCnt +=3)
{?>
<tbody>
<tr>
<td align="center"><?=$data[$arrCnt]; ?></td>
<td align="center"><?=(($arrCnt+1 < $sizeofArray)?$data[$arrCnt+1]:" "); ?></td>
<td align="center"><?=(($arrCnt+2 < $sizeofArray)?$data[$arrCnt+2]:" "); ?></td>
</tr>
<? if($data[3]!=Null) { ?>
<td rowspan="<?=$rowspan?>" align="center"><?=$grade; ?></td>
<? } ?>
<? } ?>
EDIT: added "padding" code, to avoid index out of bounds error
Based on your case, rowspan is different from one student to another. What you would want to do, is group data by student. The number of assignments per student is what will give you rowspan value.
While ... {
$data['student'][] = ['name' => $row -> name]; // all student inf
$data['student][]['assignments'][] = ['id' => $row -> assignement_id];//student assigments
}
Hope this help or put in the right direction to resolve your problem.
use foreach to loop
foreach ($data as $student){
$rowspan = count($student['assignments'];
foreach ($student['assigments'] as $assignment ){
// html table
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm selecting records from database and showing it as
<table>
<tr>
<td align="center" >Name</td>
<td align="center" >Contact</td>
</tr>
<?php
foreach($rowset as $row)
{
?>
<tr>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
</tr>
<?php
}
?>
</table>
It is displayed as
| Name | Contact |
a 9956874525
b 9856426569
c 9865989657
d 8956789565
e 8956879899
f 8985656465
Here each record is shown in separate <tr>
But now i've to show the records in such a way that ,two records are shown in a single <tr>
So my design should be
| Name | Contact | | Name | Contact |
a 9956874525 b 9856426569
c 9865989657 d 8956789565
e 8956879899 f 8985656465
What changes should i do in above code? Please help
You need create an counter to count how many times you have printed the data.
Maybe like this (not tested yet):
<?php
$i = 0;
foreach($rowset as $row)
{
if($i % 2 == 0) echo "<tr>";
?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php
if($i % 2 == 0) echo "</tr>"; $i++;
}
?>
modify the code like this
<table>
<tr>
<td align="center" >Name</td>
<td align="center" >Contact</td>
</tr>
<?php
$i=1;
foreach($rowset as $row)
{
if($i%2==1) {
?>
<tr>
<?php } ?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php if($i%2==0) { ?>
</tr>
<?php } ?>
<?php
$i++; }
?>
You can apply the loop conditionally on the basis of indexes.
<table>
<tr>
<td align="center">Name</td>
<td align="center">Contact</td>
<td align="center">Name</td>
<td align="center">Contact</td>
</tr>
<tr>
<?php
foreach($rowset as $i=>$row) {
if($i!=0 && $i%2 == 0) {
?>
</tr><tr>
<?php } ?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php } ?>
</tr>
</table>
Try using a for loop instead
$num_contacts = sizeof($rowset);
for($i = 0; $i < $num_contacts; $i++)
{
if($i%2 == 0)
echo "<tr>";
?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php
if($i%2 == 0)
echo "</tr>";
}?>
Its the appropriate way..
<table>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
<tr>
<?php
foreach($rowset as $i=>$row) {
if($i!=0 && $i%2 == 0) {
?>
</tr><tr>
<?php } ?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php } ?>
</tr>
</table>
First connect to database using: $con = mysql_connect(host,username,password);
Select Database : mysql_select_db(dbname,$con);
Write a query: $qselect = mysql_query("select * from mytable");
Now Let's start displaying in a HTML Table:
<table>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
<?php
while($result = mysql_fetch_array($qselect))
{
extract($result);
?>
<tr>
<td><?= $name?></td>
<td><?= $addr?></td>
</tr>
<?php
}
?>
</table>
Note: $name and $addr are field names from mysql table.