Two loops in one table - php

I need to insert two loops in one table, but I have a problem.
<table border="1">
<tr>
<th>Position</th>
<th>Name</th>
</tr>
<?php
for ($x=1; $x<=2; $x++) {
?>
<tr>
<td><?php echo $x ?></td>
<?php
}
?>
<?php
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
?>
<td><?php echo $name ?></td>
</tr>
<?php
}
?>
</table>
But the result is:
http://prntscr.com/6m9v25
One name is in the wrong position.

Just put while loop into for loop.
// Code goes here
<table border="1">
<tr>
<th>Position</th>
<th>Name</th>
</tr>
<?php
for ($x=1; $x<=2; $x++) {
?>
<tr>
<td><?php echo $x ?></td>
<?php
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
?>
<td><?php echo $name ?></td>
</tr>
<?php
}
?>
<?php
}
?>
</table>

<table border="1">
<tr>
<th>Position</th>
<th>Name</th>
</tr>
<?php
$x = 1;
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
?>
<tr>
<td><?php echo $x ?></td>
<td><?php echo $name ?></td>
</tr>
<?php
$x++
}
?>
</table>
Try This Code!! (Edited Again!)

Related

DataTables - PHP while loop issue can't find the last result and add new TR

I am using DataTables and I want to add new TR at the end of while loop.
I know we can add <tfoot></tfoot>, but I don't want to add '' because I am filtering data with custom Ajax.
I have tried below code but it's not working:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>
Also, when I use <tfoot></tfoot> it's not printable.
Probably your problem is in $numRows which is not defined.
So you can try this:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
$numRows = mysqli_num_rows($Itesres);
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>

Table from SQL query with header

I have created a table from a SQL query and displayed it in the same order as they appear in the table. (Table A in image).
This is working okay.
However it would be great if the data could be clubbed under the member category. As in Table B in image.
SQL Query ...
$row = mysqli_num_rows($sql);
if($row > 0) {
while ($result = mysqli_fetch_assoc($sql)){
$category[] = trim($result['category']);
$name[] = trim($result['f_name']).' '.trim($result['l_name']);
$memid[] = trim($result1['memid']);
$addr[] = trim($result['addr']);
$phone[] = trim($result['phone']);
}
} ?>
<table>
<tr>
<th>Category</th>
<th>Mem ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
</tr>
<?php
if ($row>0) {
for ($i=0; $i<=$row-1; $i++){ ?>
<tr>
<td><?php echo $category[$i]; ?></td>
<td><?php echo $memid[$i]; ?></td>
<td><?php echo $name[$i]; ?> </td>
<td><?php echo $addr[$i]; ?> </td>
<td><?php echo $phone[$i]; ?> </td>
</tr>
<?php }
} ?>
</table>
A little update of your code souhld do it:
$categories = [];
$row = mysqli_num_rows($sql);
if($row > 0) {
while ($result = mysqli_fetch_assoc($sql)) {
$result_category = trim($result['category']);
if (!isset($categories[$result_category])) {
$categories[$result_category] = [];
}
$new = [];
$new['category'] = $result_category;
$new['name'] = trim($result['f_name']).' '.trim($result['l_name']);
$new['memid'] = trim($result['memid']);
$new['addr'] = trim($result['addr']);
$new['phone'] = trim($result['phone']);
$categories[$result_category][] = $new;
}
} ?>
<table>
<tr>
<th>Category</th>
<th>Name</th>
<th>Phone</th>
</tr>
<?php
if ($row>0) {
foreach ($categories as $category_name => $data){ ?>
<tr>
<td><?php echo $category_name; ?></td>
<td></td>
<td></td>
</tr>
<?php foreach ($data as $row) {?>
<tr>
<td><?php echo $row['memid']; ?></td>
<td><?php echo $row['name']; ?> </td>
<td><?php echo $row['phone']; ?> </td>
</tr>
<?php }
}
} ?>
</table>
While loop though the data mark on array with group of category name and then print the result
$row = mysqli_num_rows($sql);
if($row > 0) {
$mainArray = [];
while ($result = mysqli_fetch_assoc($sql)){
$category = $result['category'];
if(isset($mainArray[$category])){
$mainArray[$category][] = $result;
} else {
$mainArray[$category] = $result;
}
}
}
foreach($mainArray as $cateName => $data){ ?>
<tr>
<td style="text-align:left"><?php echo $cateName; ?></td>
<td></td>
<td></td>
</tr>
<?php
foreach($data as $row){ ?>
<tr>
<td><?php echo $row['memid']; ?></td>
<td><?php echo $row['f_name'].' '.$row['l_name']; ?></td>
<td><?php echo $row['phone']; ?></td>
</tr>
<?php } ?>
}
?>

Query always returns recently added data only

I'm trying to show all my data from the database I made in mysql.
I am using this code:
<table border= "3">
<tr>
<th>ID</th>
<th>Game Name</th>
</tr>
<?php
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$id = $row['game_id'];
$name = $row['game_name'];
}
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
</table>
My problem is that not all the data show up, only the data I recently added. I believe that SELECT * means selecting all the data.
But I don't know what's the problem why it does not show all the data, anyone would happen to know?
You need to add your td inside your while loop
<?php
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
?>
<tr>
<?php
while ($row = mysql_fetch_array($result)) {
$id = $row['game_id'];
$name = $row['game_name'];
echo " <td>" . $id . "</td>";
echo " <td>" . $name . "</td>";
}
?>
</tr>
Note:- mysql is deprecated instead use mysqli and PDO
Try using this:
<table border= "3">
<tr>
<th>ID</th>
<th>Game Name</th>
</tr>
<?php
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$id = $row['game_id'];
$name = $row['game_name'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
<?php } ?>
</table>
add this inside while,
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
Final Code
while($row = mysql_fetch_array($result)) {
$id = $row['game_id'];
$name = $row['game_name']; ?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
<?php } ?>
Switch to mysqli_* or PDO instead of mysql_* which is deprecated.
If you are not connected with database then follow this code it will help you.
<table border= "3">
<tr>
<th>ID</th>
<th>Game Name</th>
</tr>
<?php
$link=mysql_connect("localhost", "root","");
mysql_select_db('dbname', $link);
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {?>
<tr>
<td><?php echo $row['game_id']; ?></td>
<td><?php echo $row['game_name']; ?></td>
</tr>
<?php }
?>
</table>

Can't put data from database to a table with PHP

My problem is that I can't fill my table with the data I got in my database. I wrote a code in PHP to do it but it doesn't work and I don't know how to make it work. Any idea ? Here's my code :
view gestJury :
<h3>Liste des jurys :</h3>
<div class="tablebox">
<table>
<thead>
<tr>
<th>N°</th>
<th>Professeur1</th>
<th>Professeur2</th>
<th>Salle</th>
<th>Heure</th>
<?php if($rang == 0){ ?>
<th class="action" >Supprimer</th>
<?php } ?>
</tr>
</thead>
<?php $i = 0; ?>
<?php while($donnees = $listeJury->fetch(PDO::FETCH_ASSOC)){
$i = $i + 1;
?>
<tbody>
<tr class="row0">
<td><?php echo $donnees['idJury'] ?></td>
<td><?php echo $donnees['idProf'] ?></td>
<td><?php echo $donnees['idProf2'] ?></td>
<td><?php echo $donnees['Salle'] ?></td>
<td><?php echo $donnees['horaireJury'] ?></td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
index.php :
function listJuryGlobal(){
$jury = new Professeur();
$listeJury = $jury->getJuryGlobal();
require('view/gestJury.php');
}

Combine array value with an increment integer

I have a table called subject_scores which has the following fields:
candidateNumber, paperCode, paperNumber, question1, question2, ... , question20
The issue is displaying it in the view (CodeIgniter), here's my code:
<table class="table table-striped table-condensed" id="example">
<thead>
<tr>
<th>Candidate Number</th>
<th>Paper Code</th>
<th>Paper Number</th>
<?php for ($i = 1; $i <= 20; $i++) : ?>
<th><?php echo 'Q'.$i; ?></th>
<?php endfor; ?>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php if (isset($scores)) : foreach ($scores as $row) : ?>
<tr>
<td><?php echo $row->candidateNumber; ?></td>
<td><?php echo $row->paperCode; ?></td>
<td><?php echo $row->paperNumber; ?></td>
<td><?php echo $row->status; ?></td>
<?php for ($i = 1; $i <= 20; $i++) : ?>
<?php if ($row->question.$i == NULL) : ?>
<td><span class="red">NA</span></td>
<?php else : ?>
<td><?php echo $row->question.$i; ?></td>
<?php endif; ?>
<?php endfor; ?>
</tr>
<?php endforeach; endif; ?>
</tbody>
</table>
From the above code you'll notice I've used something like this $row->question.$i to combine the array value with the increment to create fields from question1 to question20.
How do I do this correctly either in PHP or CodeIgniter?

Categories