I am using this code to create an infinite table for my mysql queries:
<table cellspacing='0'> <!-- cellspacing='0' is important, must stay -->
<!-- Table Header -->
<thead>
<tr>
<th>User</th>
<th>SteamID</th>
<th>Banned by</th>
<th>Admin SteamID</th>
<th>Time Banned (min)</th>
<th>Reason</th>
</tr>
</thead>
<!-- Table Header -->
<!-- Table Body -->
<tbody>
<?php
echo '<tr>';
for($i = 0; $bans = mysqli_fetch_array($query2); $i = ($i+1)%3){
echo '<td>'.$bans['name'].'</td>';
echo '<td>'.$bans['steamid'].'</td>';
echo '<td>'.$bans['nameAdmin'].'</td>';
echo '<td>'.$bans['steamidAdmin'].'</td>';
echo '<td>'.$bans['time'].'</td>';
echo '<td>'.$bans['reason'].'</td>';
if($i == 2)
echo '</tr><tr>';
}
echo '</tr>';
?>
</tbody>
I got that code from Mysql fetch array, table results
It works fine, except it doesn't CORRECTLY go further down than 6 rows. The other rows for whatever reason are placed to the right of my last column as shown in this screenshot:
http://puu.sh/h0qZF/a12de1dd87.png
How can I fix this? Is there something wrong with my code? Why is it happening?
Well, your looping makes no sense. Using $i to inject new rows, like is done here, is not necessary; you can just loop over each row and then output it as a row:
<table>
<!-- <thead>...</thead> -->
<tbody>
<?php while ($bans = mysqli_fetch_array($query2)): ?>
<tr>
<td><?php echo $bans['name'] ?></td>
<td><?php echo $bans['steamid'] ?></td>
<td><?php echo $bans['nameAdmin'] ?></td>
<td><?php echo $bans['steamidAdmin'] ?></td>
<td><?php echo $bans['time'] ?></td>
<td><?php echo $bans['reason'] ?></td>
</tr>
<?php endwhile ?>
</tbody>
</table>
You are making two columns.
You have code that will print out the end of the table row every two sets of data:
if($i == 2)
echo '</tr><tr>';
It should just be echo '</tr><tr>';
Use a while loop as instructed here . So something like this:
$result = $conn->query($sql);
while($bans = $result->fetch_assoc()) {
echo '<td>'.$bans['name'].'</td>';
echo '<td>'.$bans['steamid'].'</td>';
echo '<td>'.$bans['nameAdmin'].'</td>';
}
Related
I have the below code to display content from a database. The while loop is supposed to display data in a table row. The output is different: the data from the database are displayed before the table. The while seems to run and ignore the html tags, and then run again and ignore the variables.
PHP Code:
// Instantiate database
$database = new Database();
$db = $database->getConnection();
// List transactions without bundles
$query = "SELECT * FROM stock_trx WHERE bundleId IS NULL";
$statement = $db->prepare($query);
$statement->execute();
$n = $statement->rowCount();
if($n > 0){
echo "<table>
<thead>
<tr>
<th>Select</th>
<th>Date</th>
<th>Type</th>
<th>Quantity</th>
<th>Ticker</th>
<th>Amount</th>
<th>Currency</th>
</tr>
</thead>
<tbody>";
// Retrieve table content
while($row = $statement->fetch(PDO::FETCH_ASSOC)){
// Extract row
extract($row);
echo "<tr>".$id."</tr>";
echo "<tr>".$date."</tr>";
echo "<tr>".$type."</tr>";
echo "<tr>".$qty."</tr>";
echo "<tr>".$ticker."</tr>";
echo "<tr>".$amount."</tr>";
echo "<tr>".$currency."</tr>";
}
echo "</tbody></table>";
} else {
echo "No transaction";
}
HTML output:
<html>
<body>
12020-05-13 00:00:00BUYTEST1000022020-05-19 00:00:00SELTEST1200032020-05-24 00:00:00DIVTEST250<table>
<thead>
<tr>
<th>Select</th>
<th>Date</th>
<th>Type</th>
<th>Quantity</th>
<th>Ticker</th>
<th>Amount</th>
<th>Currency</th>
</tr>
</thead>
<tbody><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>
</tbody>
</table>
</body></html>
Thank you
You need rows <tr> and cells <td> inside the row:
while($row = $statement->fetch(PDO::FETCH_ASSOC)){
// Extract row
extract($row);
echo "<tr>"; // start row
echo "<td>".$id."</td>";
echo "<td>".$date."</td>";
echo "<td>".$type."</td>";
echo "<td>".$qty."</td>";
echo "<td>".$ticker."</td>";
echo "<td>".$amount."</td>";
echo "<td>".$currency."</td>";
echo "</tr>"; // end row
}
echo "</tbody></table>";
Contrary to the other answer I would either do this, since you're using double-quotes:
echo "<tr>"; // start row
echo "<td>$id</td>";
// etc...
Or combine into one, maybe even one line:
echo "
<tr>
<td>$id</td>
<td>$date</td>
<td>$type</td>
<td>$qty</td>
<td>$ticker</td>
<td>$amount</td>
<td>$currency</td>
</tr>";
While some others have mentioned that you need TD tags, I'd just like to point out some weirder PHP-type things that may or may not help you now / in the future.
Your statement can be rewritten
<?php
while($row = $statement->fetch(PDO::FETCH_ASSOC)):
// Extract row
extract($row);?>
<tr>
<td><?php echo $id; ?>?</td>
<td><?php echo $date; ?>?</td>
<td><?php echo $type; ?>?</td>
<td><?php echo $qty; ?>?</td>
<td><?php echo $ticker; ?>?</td>
<td><?php echo $amount; ?>?</td>
<td><?php echo $currency; ?>?</td>
</tr>
<?php endwhile; ?>
This uses some lesser seen formats for using HTML in PHP files, but it should work never-the-less (tested locally with the PHP 7.4 CLI). Some people prefer it to having to use echo to output all of the HTML, but you do still have to have a bunch of echo statements to print the variables.
To reiterate, there's nothing wrong with any of the other answers, I just feel that in cases like this where you are using a bunch of HTML with PHP that this sort of notation can be a bit easier to work with.
As some people replied, the below tag is missing:
<td>
I retrieved a list of data from an SQL database and now I would like to display it in a neat table rather than in a list. I managed to find a way to do this (probably not very elegant, though), but the column headers seem to be offset and I have not idea how to fix this.
I'm completely new to PHP, so any hints on how to solve this will be much appreciated!
echo '<table>';
echo '<tr>';
echo '<th>';
echo '<td>Word</td>';
echo '<td>Frequency</td>';
echo '</th>';
echo '</tr>';
$response = $db->query("SELECT * FROM frequencies WHERE freq BETWEEN 900 AND 910 ORDER BY freq");
while ($row = $response->fetch())
{
echo '<tr>';
echo '<td>'.$row['word'].'</td>';
echo '<td>'.$row['freq'].'</td>';
echo '</tr>';
}
echo '</table>';
$response->closeCursor();
A <th> element is a table header element and should be used instead of <td> (table data) element in your header row - it should never be a wrapper around <td> elements.
echo '<table>';
echo '<tr>';
echo '<th>Word</th>';
echo '<th>Frequency</th>';
echo '</tr>';
I prefer combining php and html
<table >
<thead>
<tr>
<th >Word</th>
<th >Frequency</th>
</tr>
</thead>
<?php
$response = $db->query("SELECT * FROM frequencies WHERE freq
BETWEEN 900 AND 910 ORDER BY freq");
?>
<tbody>
<?php
while ( $row = $response->fetch()) {
?>
<tr>
<td><?php echo $row['word']; ?></td>
<td><?php echo $row['freq']; ?></td>
</tr>
<?php }
$response->closeCursor();
?>
</tbody>
</table>
I try to display 2 table side by side with php code....
but i need to display only 6 table on single page ......and rest on another page......
so plz can any one help me to break page or break loop after 6th iteration..... 7th table display on another page like wise...
plz see my image attached below....i am facing problem on print preview...
page breaks my table during print...like below image...
I attached croped imaged here...
my page actuaally display 8 tables on single page...but i need is only 6 on one page.
below is my code..
<?php if (is_array($data)) { foreach($data as $row) { ?>
<table border="1px solid #666" summary="" width="48%" class="pos_fixed1">
<thead>
<tr>
<td colspan="4">Dainik Bhaskar Nagpur</td>
</tr>
<tr>
<td>Receipt</td>
<td><?php echo htmlspecialchars($row['receipt_no']); ?></td>
<td>Coupon</td>
<td><?php echo htmlspecialchars($row['coupon']); ?></td>
</tr>
<tr>
<td>Receipt Date</td>
<td><?php echo htmlspecialchars($row['bookingdate']); ?></td>
<td>Coupon Date</td>
<td><?php echo htmlspecialchars($row['coupondate']); ?></td>
</tr>
<tr>
<td>Copy Start Date</td>
<td><?php echo htmlspecialchars($row['startingdate']); ?></td>
<td>HawkerName</td>
<td><?php echo htmlspecialchars($row['hawkername']); ?></td>
</tr>
<tr>
<td>SubagentName</td>
<td><?php echo htmlspecialchars($row['subagentname']); ?></td>
<td>CenterName</td>
<td><?php echo htmlspecialchars($row['ward']); ?></td>
</tr>
<tr>
<td>customer</td>
<td><?php echo htmlspecialchars($row['customer_name']); ?></td>
<td>Address</td>
<td><?php echo htmlspecialchars($row['society']); ?></td>
</tr>
</thead>
</table>
<?php } }?>
Try using CSS:
<style>
#media print {
.pageBreak {
page-break-after: always;
}
}
</style>
And at every 6 table, add a pageBreak:
<?php
$lineCounter = 0;
if (is_array($data)) {
foreach($data as $row) {
$lineCounter++;
?>
<!-- output a table... -->
<?php
if($lineCounter % 6 == 0) {
echo '<span class="pageBreak"></span>' . PHP_EOL;
}
}
}
?>
<?php
// get total records
TotalNoOfRecords = count($data);
$Count = 1;
foreach($data as $row) {
// write here your content
// break page after 6 rows
if($Count % 6 == 0 && $Count != $TotalNoOfRecords)
{
echo "<p style='page-break-after:always'></p>";
}
$Count++;
}
?>
try this is code or visit link
<?php
$q = "SELECT * FROM your_table ";
$myq = mysqli_query($link, $q);
$fixtures ='';
$i=0;
while($row=mysqli_fetch_assoc($myq)) {
$r[]=$row;
}
foreach ($r as $val) {
$i++;
?>
<!-- your value from database -->
<table>
<tr>
<td><?php echo $val['your_column']; ?></td>
</tr>
</table>
<!-- your value from database -->
<?php
if($i % 6==0){
echo '<div style="page-break-after: always;">[------ break ------]</div>' . PHP_EOL;
$i=0;
}
}
?>
I will be generating a HTML table with data pulled from MySQL.The number of rows in my MySQL table are not fixed.
<?php
while($row=mysql_fetch_assoc($result))
{ ?>
<tr>
<td><?php echo $row['col1'];?></td>
<td><?php echo $row['col2'];?></td>
</tr>
<?php } ?>
Now how do I have the table rows and table data elements assigned unique id ??
Another loop to generate them won't work as I can't set an exit condition for the new loop as number of rows are not fixed.
Please guide me as to how to go forward about it. I can only use Javascript and not JQUERY.
Why can't you do something like this ?
<?php
$i = 1;
while($row=mysql_fetch_assoc($result))
{ ?>
<tr id="row<?php echo $i;?>">
<td id="cell-left-<?php echo $i;?>"><?php echo $row['col1'];?></td>
<td id="cell-right-<?php echo $i;?>"><?php echo $row['col2'];?></td>
</tr>
<?php
$i++;
} ?>
Please note, I have added ids row, cell-left- and cell-right- by myself. You may change them as per your requirements.
You can use a counter when iterating through the rows, maybe something like this:
<?php
$rowCount = 0;
while($row=mysql_fetch_assoc($result))
{
$rowCount++;
?>
<tr id="<?php echo 'row' . $rowCount;?>">
<td><?php echo $row['col1'];?></td>
<td><?php echo $row['col2'];?></td>
</tr>
<?php
}
?>
You can now select an element with
var rowID = 1;
document.getElementById("row" + rowID);
Hope this helps.
I have a while loop which displays records in a table. Works OK, but there is an extra table row at the top of the table which is empty.
<table width="100%" border="0" cellpadding="5">
<?php do {
$count++;
?>
<tr bgcolor=<?php echo processRow($count); ?>>
<td><?php echo $row['title']; ?></td>
<td width="8%">edit</td>
<td width="12%">delete</td>
</tr>
<?php
if($count == 2){
$count = 0;
}
} while ($row = mysql_fetch_assoc($result));
?>
</table>
The first time the loop starts $row = mysql_fetch_assoc($result) is not yet executed therefore no result is being fetched.
Add $row = mysql_fetch_assoc($result); at the beginning of the file (before the loop) or convert the do-while loop into a while to solve the problem.
the do {} while() construct goes over the contents of the block first, before evaluating what is passed to while. if you write it like this, is should get rid of the empty row:
<?php while ($row = mysql_fetch_assoc($result)): ?>
<? $count++; ?>
<tr bgcolor=<?php echo processRow($count); ?>>
<td><?php echo $row['title']; ?></td>
<td width="8%">edit</td>
<td width="12%">delete</td>
</tr>
<?php
if($count == 2){ $count = 0; }
endwhile;
?>