I am trying to populate my html table using data from my database but each a new row is added to the table, the table shifts downwards. If for example, 38 rows is added to the table, the table becomes invisible. You have to scroll down to see the table. What might the problem be?
echo "
<div class='container'>
<span class='cc' style='font-weight: bold;'>Count: $stat</span></br></br>
<table class='col-md-9' border='1'>
<th>Agent's name</th>
<th>Student's name</th>
<th>Student's number</th>
<th>Accommodation type</th>
<th>School</th>
<th>Date & Time</th>
";
foreach($db->query($select) AS $result){
echo "
<tr><td>{$result['agent_name']}</td>
<td>{$result['student_name']}</td>
<td>{$result['student_number']}</td>
<td>{$result['accommodation']}</td>
<td>{$result['school']}</td>
<td>{$result['contact_date']}</td>
</tr>
</br>
";
}
echo "
</table>
</div>
";
You cannot have <th> directly inside <table>. Please wrap them inside <tr>.
You must get the rows from the foreach in order to get the result rows. The one that you are looping is just the stdObject ResultSet. Change your code to:
$result = $db->query($select)
if (mysqli_num_rows($result) > 0)
while (false != ($row = mysqli_fetch_array($result)))
// Loop here.
Yes, and as others said, a <table> tag can contain only <tbody>, <thead>, <tfoot> and <tr>. Nothing else. There's no tag like </br>. It must be <br />. Brush up the basics of HTML.
Two things:
First, you need to wrap your header elements in a row:
<tr> <!-- here -->
<th>Agent's name</th>
<th>Student's name</th>
<th>Student's number</th>
<th>Accommodation type</th>
<th>School</th>
<th>Date & Time</th>
</tr> <!-- and here -->
Second, what on Earth are these?:
</br>
The browser is probably interpreting them as this:
<br />
Which means you're adding a line break inside your table structure with each row. Which is probably invalid (since it's outside the table cell and part of the table itself), but probably also why each row pushes the display down a little further. Remove that from your loop.
As it stands the code is generating invalid table markup, so styling and rendering the table is going to be somewhat undefined and possibly different for each browser.
You need to wrap you <th></th> in <tr></tr> too,
That's indeed the case.
Second of all, I will advice not using echo in this case. Here is what the code looks like the way I would do it:
<div class='container'>
<span class='cc' style='font-weight: bold;'><?php Count: $stat ?></span></br></br>
<table class='col-md-9' border='1'>
<tr>
<th>Agent's name</th>
<th>Student's name</th>
<th>Student's number</th>
<th>Accommodation type</th>
<th>School</th>
<th>Date & Time</th>
</tr>
<?php
foreach($db->query($select) AS $result):
?>
<tr>
<td><?=$result['agent_name']?></td>
<td><?=$result['student_name']?></td>
<td><?=$result['student_number']?></td>
<td><?=$result['accommodation']?></td>
<td><?=$result['school']?></td>
<td><?=$result['contact_date']?></td>
</tr>
</br>
<?php
endif;
?>
</table>
</div>
This would be the code I would prefer because you'll keep the HTML colors in your code editor, which makes it easier for others to debug.
Little explanation:
<?= and ?> are easy to use open and close tags to echo a variable within HTML.
Related
When trying to display a table which has SQL data present, the rows at the top of the table are repeating, which I do not want to happen! I know it is probably something stupid but I've tried for a while to solve this and can't. Images outline the code which I used, and the output which is displayed
Write the first <tr></tr> before while
like this :
echo "<table>";
echo "<tr>
<th>albul Name</th>
..
..
..
</tr>";
while($album=$stmt->fetchObject()) {
echo "<tr>
<td>$album->$album_name
...
...
</tr>";
}
The issue is you're echoing your Header row inside of your WHILE loop, so you're writing the header for each iteration of the loop.
To fix, move the header row out of your loop like this:
echo "<table><tr>
<th>album Name</th>
<th>Year</th>
<th>Genre</th>
<th>Artist Name</th>
<th>Total Running Time</th>
</tr>"
while ($album = $stmt->fetchObject()) {
//Display the data as a row.
echo "<tr>
<td>$album->album_name</td>
<td>$album->year</td>
<td>$album->genre</td>
<td>$album->artist_name</td>
<td>$album->total_time</td>
</tr>"
}//end loop
echo "</table>";
When you're looking at something for too long you just can't see it anymore. Just take your tags out of the while loop. You only need to the data rows in the loop.
Take the code out of the WHILE loop.
The first row and should be before the loop code starts
you need change your code is firts set your struture html table and next php code:
<?php
echo"
<table border="1">
<tr>
<td>ALBUM</td>
<td>YEAR</td>
<td>GENERE</td>
<td>ARTIST</td>
<td>TOTAL PLAYING TIME</td>
</tr>
";
now you need print your records from db
while($album=$stmt->fetchObject()) {
echo "<tr>
<td>$album->$album_name</td>
<td>$album->$year</td>
<td>$album->$genre</td>
<td>$album->$artist_name</td>
<td>$album->$total_time</td>
</tr>";
}//end while
all code is this:
<?php
echo"
<table border="1">
<tr>
<td>ALBUM</td>
<td>YEAR</td>
<td>GENERE</td>
<td>ARTIST</td>
<td>TOTAL PLAYING TIME</td>
</tr>
";
while($album=$stmt->fetchObject()) {
echo "<tr>
<td>$album->$album_name</td>
<td>$album->$year</td>
<td>$album->$genre</td>
<td>$album->$artist_name</td>
<td>$album->$total_time</td>
</tr>";
}//end while
I have used jquery datatable,
HTML Code
<table id="result_table" class="display table table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Activation Status</th>
<th>Recent Login</th>
<th>Total No. of login</th>
<th>Avg No. of login/day</th>
<th>Total No. of Exports</th>
<th>Total No. of access</th>
</tr>
</thead>
<tfoot>
<tr>
<th>#</th>
<th>Username</th>
<th>Activation Status</th>
<th>Recent Login</th>
<th>Total No. of login</th>
<th>Avg No. of login/day</th>
<th>Total No. of Exports</th>
<th>Total No. of access</th>
</tr>
</tfoot>
</table>
and I have to load the table data in ajax call in JSON datatype here my PHP code
while($row = mysql_fetch_array($run_query)){
$name = $row['first_name'];
$name_string = "<a edit_id='name_".trim($name)."' href='#' onClick=\"click_today('{$name}');\" data-toggle='modal' data-target='#myModal'>{$name}</a>";
//onClick=\"click_today('{$name}');\"
$result_array[] = array($row['id'], $name_string, $row['last_name'], $row['job_title'], $row['salary'],"-","-","-");
}
echo json_encode($result_array);
acutually my result is (sample output)
but i want following image output in datatable
I don't know how to implement vertical header in jquery data table I am always using horizontal thead but now I need vertical header because of needed pls share ur suggestion or solution
It is possible to make this kind of output from server side[php] only then
to assign datatable. I had also face this problem but I make way from server side
and another way is also there you change your expected format using PIVOT Table
using database
I have a bottstrap html table:
<table data-toggle="table" data-url="../../scripts/tags/s_displaytags.php" data-show-refresh="true" data-show-toggle="true" data-show-columns="true" data-search="true" data-select-item-name="toolbar1" data-pagination="true" data-sort-name="TagName" data-sort-order="desc">
<thead>
<tr>
<th data-field="SerialNumber" data-sortable="true">Serial #</th>
<th data-field="TagName" data-sortable="true">Tag Name</th>
<th data-field="CreatedBy" data-sortable="true">Created By</th>
</tr>
</thead>
</table>
I'm trying to populate it using a PHP script s_displaytags.php. Currently im testing it so the code is:
<?PHP
$ArrayValue = array();
$ArrayValue['SerialNumber'] = 1;
$ArrayValue['TagName'] = 'test';
$ArrayValue['CreatedBy'] = 50;
echo json_encode($ArrayValue);
?>
My HTML page loads but the table values do not get loaded.
I'm not using any functions at the moment. It's just a plain PHP file returning one result row encoded s JSON.
so i just changed echo json_encode($ArrayValue); to echo json_encode(array($ArrayValue)); and it worked.
I have table generated by mustache loop that looks like this:
names.mustache
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>
#
</th>
<th>
Name
</th>
</tr>
</thead>
<tbody>
{{#allnames}}
{{#active}}
<tr>
<td>
{{count}}
</td>
<td>
{{name}}
</td>
</tr>
{{/active}}
{{/allnames}}
</tbody>
</table>
And I want to iterate count so that my table can have row numbers. basically, i initialized $count=1. How can I implement that with clean efficient code? or is there a better simpler way?
UPDATED
For Mustache, you need to create a function
var data = {
items: allnames,
, index: function() {
return ++window['index']||(window['index']=0);
}
}
and then use {{index}} inside the loop
originally
Sorry, I was thinking of handlebars which works as below (see correct answer above)
Use {{#index}}
{{#allnames}}
{{name}} is {{#index}}
{{/allnames}}
Note: index starts from zero
If the array is not null (and has values in it), then I want to display the table.
But if it is null, then I don't want to display any table code at all.
Using an MVC framework which appends a footer to the page.
What is the best way to avoid a statement like:
<?php
if ($users) {
echo '<table id="tha_table" cellpadding="0" cellspacing="0" width="100%">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>';
} ?>
And, don't want to do another test to add the table footer.
I think I see what you are after...
I would place all of the HTML in a separate file, and conditionally include it.
if(!empty($users)) {
include "users_table.template";
}
Note that the template file can include php if you want it to.
I always use empty() to check whether an array is empty. Empty will also check whether the variable is null. Note that empty() does not throw a warning if the array variable is not set, which may or may not be desirable.
<?php
$displayUserTable = !empty($users);
?>
<?php if($displayUserTable): ?>
<table id="tha_table" cellpadding="0" cellspacing="0" width="100%">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php foreach($users as $user): ?>
<tr>
<td><?php echo htmlspecialchars($user['firstName']); ?></td>
<td><?php echo htmlspecialchars($user['lastName']); ?></td>
<td><?php echo htmlspecialchars($user['emailAddress']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php if($displayUserTable): ?>
<!-- show footer here... -->
<?php endif; ?>
I recommend you use either a templating system or any other vehicle to separate your PHP code from the HTML rendering.
All template systems I know of allow for a block to be skipped depending on a boolean, so you would just include the (template for the) table in your page template and surround it with whatever your chosen framework uses as an if or repeat n times construct.