Bootstrap tables only displaying one row in <td> - php

(http://imgur.com/2Ev8u4L)
As you can see from the image the first row from the table is only in the actual table. It could be code error I'm not sure?
$selectNews = $PDO->query("SELECT * FROM `news`");
echo '<table class="table">';
echo '<thead>';
echo '<th>Update Number</th>';
echo '<th>Title</th>';
echo '<th>Description</th>';
echo '<th>Created On</th>';
echo '<th>Created By</th>';
echo '</thead>';
while ($results = $selectNews->fetch(PDO::FETCH_ASSOC)) {
echo "<tbody>";
echo "<tr><td>";
echo $results['ID']."</td><td>";
echo $results['Title']."</td><td>";
echo $results['Description']."</td><td>";
echo date('d/m/Y g:i:s A', strtotime($results['Time']))."</td><td>";
echo $results['Creater']."</td></tr>";
echo "</tbody>";
echo "</table>";
}

You don't check, if $selectNews is really valid. From PDO::query
Return Values
PDO::query() returns a PDOStatement object, or FALSE on failure.
Update:
You have put tbody and table inside your while loop. You must put the opening and closing tags outside
echo "<tbody>";
while ($results = $selectNews->fetch(PDO::FETCH_ASSOC)) {
echo "<tr><td>";
...
echo $results['Creater']."</td></tr>";
}
echo "</tbody>";
echo "</table>";

Related

Looping rows in html table with php

I want to write the rows as many as the number stated in $row. (instead of one row only). How can I achieve this? What am I doing wrong? thanks.
<?php
$row = 50;
echo "<table border='1'>";
for($i=0;$i<$row;$i++)
echo "<tr>";
echo "<td>L1</td><td>L2</td><td>L3</td>";
echo "</tr>";
echo "</table>";
echo $i+1;
}
?>
You are closing table inside the loop. Change to the following
<?php
$row = 50;
echo "<table border='1'>";
for($i=0;$i<$row;$i++){
echo "<tr>";
echo "<td>L1</td><td>L2</td><td>L3</td>";
echo "</tr>";
}
echo "</table>";
?>
try this
<?php
$row = 50;
echo "<table border='1'>";
for($i=0;$i<$row;$i++)
{
echo "<tr>";
echo "<td>L1</td><td>L2</td><td>L3</td>";
echo "</tr>";
}
echo "</table>";
?>
try this code i have added some mistake done by you
<?php
$row = 50;
echo "<table border='1'>";
for($i=0;$i<$row;$i++){ //add bracket here
echo "<tr>";
echo "<td>L1</td><td>L2</td><td>L3</td>";
echo "</tr>";
//echo $i+1; //remove this one
}
echo "</table>"; //close table tag outside the loop
?>

Adding datatables to a table (wordpress)

I'm trying to add datatables to my project and it shows up with the arrows and the search bar, but neither work.
I tried testing the code on a page with just the scripts and table and it worked but when I moved it to my wordpress site I run into problems.
Any suggestions? I have the jquery and datatables scripts and css in the header page, as well as the script for the table itself after the /head:
<script>
$(document).ready(function(){
$('#myTable').DataTable();
});
</script>
My table itself looks like
echo "<table id='myTable' class='display table' width='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Foster ID</th>";
echo "<th>Foster's Name</th>";
echo "<th>City</th>";
echo "<th>E-Mail</th>";
echo "</tr>";
echo "</thead>";
if(!empty($result)){
foreach ($result as $results){
$fosterId = $results->memberId;
$fosterName = $results->memberName;
$city = $results->city;
$email = $results->email;
echo "<tbody>";
echo "<tr>";
echo "<td>$fosterId</td>";
echo "<td>$fosterName</td>";
echo "<td>$city</td>";
echo "<td>$email</td>";
echo "</tr>";
}
} else {
echo "<tr>";
echo "<td colspan='5'>No Fosters</td>";
echo "<tr>";
echo "</tbody>";
}
echo "</table>";
Your PHP code contains tbody element in the loop. Also DataTables doesn't support colspan attribute in table body.
Corrected PHP code:
echo "<table id='myTable' class='display table' width='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Foster ID</th>";
echo "<th>Foster's Name</th>";
echo "<th>City</th>";
echo "<th>E-Mail</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach ($result as $results){
$fosterId = $results->memberId;
$fosterName = $results->memberName;
$city = $results->city;
$email = $results->email;
echo "<tr>";
echo "<td>$fosterId</td>";
echo "<td>$fosterName</td>";
echo "<td>$city</td>";
echo "<td>$email</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
Corrected JavaScript code:
$(document).ready(function(){
$('#myTable').DataTable({
"language": {
"emptyTable": "No Fosters"
}
});
});

How can I display each record individually into a while loop?

I use this while loop to display events from facebook. I want to add an href link in each record that will open a new tab displaying only this event. I have created links for each record but I can't understand how I can make the link to lead to that event.
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<table class='table table-hover table-responsive table-bordered'>";
echo "<tr>";
echo "<td rowspan='6' style='width:20em;'>";
echo "<img src=". $row["COVER_PHOTO"]." width='200px' />";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:15em;'>What:</td>";
echo "<td><b>". $row["NAME"]."</b></td>";
echo "</tr>";
//owner
echo "<tr>";
echo "<td>Who:</td>";
echo "<td>". $row["OWNER"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>What kind:</td>";
echo "<td>". $row["PAGE_CAT"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>When:</td>";
echo "<td>". $row["START_DATE"]." at ". $row["START_TIME"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Where:</td>";
echo "<td>". $row["PLACE"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<a href='view.php?more=". $row["EVENT_ID"]."' target='_blank' <?php echo >Λεπτομέρειες</a>";
echo "<tr>";
echo "<td>Description:</td>";
echo "<td>". $row["DESCRIPTION"]."</td>";
echo "</tr>";
Your code for this page is just fine. Now to display each individual event in the view.php file, you will need to access the data (in your case the event ID, stored in more variable) sent via GET method in the view.php page, as thus:
view.php
<?php
if(isset($_GET['more']))
{
$more = $_GET['more'];
}
?>
The value of the more variable sent via the URL will now be saved in the $more PHP variable. For example, if the page called is view.php?view=22, the $more variable will have the value of 22.
You can now use this $more variable - which stores the event ID - to fetch whatever other detail you require.
echo "<a href='view.php?more=". $row["EVENT_ID"]."' target='_blank'> Λεπτομέρειες</a>";

Displaying records from database on webpage

I'm attempting to display my data in a table from my database, I used the exact same code for another table but for some reason the table appears but is empty.
Thanks its solved!
you have to put brackets in while loop, close tag and the last :
echo "<table border=1 cellpadding=5>";
echo "<tr>";
echo "<th>Workshop Name</th>";
echo "<th>Workshop Price (£)</th>";
echo "<th>Workshop Description</th>";
echo "<th>Further Information</th>";
echo "</tr>";
$SQL="select workshopId,workshopName, workshopPrice,workshopDescription from Workshops order by workshopId";
$exeSQL=mysql_query($SQL) or die (mysql_error());
while ($arrayworkshop=mysql_fetch_array($exeSQL))
{
echo "<tr>";
//display details
echo "<td>".$arrayworkshop['workshopName']."</td>";
echo "<td>".$arrayworkshop['workshopPrice']."</td>";
echo "<td>".$arrayworkshop['workshopDescription']."</td>";
echo "<td><a href='ViewWorkshop.php?workshopId=".$arrayworkshop['workshopId']."'>";
//Display name of the Task
echo "Further Information"."</a></td>";
echo "</tr>";
}
echo "</table>";

PHP Table Format

I've put together the following code which creates a table upon a selection being made with a drop down menu.
echo "<table>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
The problem I've got is that for each record that is returned the 'headers' are repeated. The live page can be found here. I just wonder whether someone could perhaps take a look at this please and tell me where I've gone wrong.
Apologies for the really simple question, but I've been looking at this for a while and I just can't find the answer. I think it just needs a fresh pair of eyes to look at it.
Try this, you just need to get headers out of while loop
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
The answer is obvious, you are repeating the output of the headers in the loop. Move the
while($rows=mysql_fetch_array($result)){
after the first
echo "</tr>";
You need to put the headers outside of the while loop:
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result = mysql_query($query);
while ($rows = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $rows['findname'] . "</td>";
echo "<td>" . $rows['finddescription'] . "</td>";
echo "</tr>";
}
echo "</table>";
This should work:
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
Your headers are being repeated because you are writing them inside the loop, for each row returned by the query. You simply need to move your headers outside the loop so they're only written once, before the rows returned by the query start printing:
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
headers are repeating becasue they are in while loop, it should work well
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
Change to:
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
echo "<tr>";
while($rows=mysql_fetch_array($result)){
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";

Categories