I got the following table with a "view" button in a template.php. Im using bootstrap 3.3.7
<?php
if($num>0){
echo "<table class='table table-responsive table-sm table-hover table-bordered'>";
echo "<thead>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Description</th>";
echo "<th></th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>{$name}</td>";
echo "<td>{$description}</td>";
echo "<td>";
echo "<a href='view_thing.php?id={$id}' class='btn-info btn-sm'>";
echo "<span class='glyphicon glyphicon-eye-open'></span> View";
echo "</a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
It looks like:
table test 1
What i tried:
<th class="col-md-1"></th>
<th style="width: 10%"></th>
What it should do:
The last column should fit the button size.
Solution:
echo <th style=width: '10%'></th>
Related
i try to get some data from a database and put it in a table.
the problem is the loop, the second row is not going to the new row, is continuing in same row. I checked the code and I can not see the problem.
Infact in the database i have 3 row and in the website is geting just 2 of them and booth in same row.
Can anybody help me?
<h1>view database</h1>
<div class="container">
<div class="page-header">
<h1>Shop list</h1>
</div>
<?php
$query = "SELECT shop_name, shop_phone_number, shop_email, shop_address_no, shop_address_street, shop_town, shop_county, shop_postcode, shop_address_country FROM shop";
$stmt = $conn->prepare($query);
$stmt->execute();
$num = $stmt->rowCount();
//button to add new shop
echo "<a href='index.php?page=shop' class='btn btn-primary m-b-1em'>Add new shop</a>";
if($num>0){
echo "<table class='table table-hover table-responsive table-bordered'>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Phone</th>";
echo "<th>Email</th>";
echo "<th>Address no/name</th>";
echo "<th>Street</th>";
echo "<th>Town</th>";
echo "<th>County</th>";
echo "<th>Post Code</th>";
echo "<th>Country</th>";
echo "<th>Action</th>";
echo "</tr>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>{$shop_name}</td>";
echo "<td>{$shop_phone_number}</td>";
echo "<td>{$shop_email}</td>";
echo "<td>{$shop_address_no}</td>";
echo "<td>{$shop_address_street}</td>";
echo "<td>{$shop_town}</td>";
echo "<td>{$shop_county}</td>";
echo "<td>{$shop_postcode}</td>";
echo "<td>{$shop_address_country}</td>";
echo "<td>";
echo "<a href='read_one.php?name={$shop_name}' class='btn btn-info m-r-1em'>Read</a>";
echo "<a href='read_one.php?name={$shop_name}' class='btn btn-primary m-r-1em' >Edit</a>";
echo "<a href='#' onclick='delete_user({$shop_name})' class='btn btn-danger>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "<div class='alert alert-danger'>No Records found.</div>";
}
?>
</div>
?>
I cant view Invalid, any suggestion to make it run?
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Strand</th>
<th>Section</th>
</tr>
<?php
$conn=mysqli_connect("localhost","root","","search");
$set=$_POST['search'];
if ($set > 0) {
$show="SELECT * FROM search_student WHERE fname='$set'";
$result=mysqli_query($conn,$show);
while ($rows=mysqli_fetch_array($result) ) {
echo "<tr>";
echo "<td>";
echo $rows['fname'];
echo "</td>";
echo "<td>";
echo $rows['lname'];
echo "</td>";
echo "<td>";
echo $rows['strand'];
echo "</td>";
echo "<td>";
echo $rows['section'];
echo "</td>";
echo "<tr>";
echo "</br>";
}
}
The else statement here doesn't work
else{
echo "invalid";
}
?>
</table>
if(mysqli_num_rows($result)>0){
while ($rows=mysqli_fetch_array($result) ) {
echo "<tr>";
//etc
}
}
else
{
echo "invalid";
}
Following is the code
I want out put as
http://crysol.com/crysol_soft/Test/Screenshot_3.png
With following code I am getting output as
http://crysol.com/crysol_soft/Test/Screenshot_4.png
echo "<table border='1'>";
echo "<th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th>";
echo '<tr>';
echo "<td rowspan='5'>Cat</td>";
for($i=1;$i<=5;$i++){
echo '<td>'.$i.'</td>';
echo " </tr>";
}
echo "<td rowspan='10'>Good</td>";
?>
What are the changes required
Here is the code with your desired output
used if condition for print only 1 time good column and used rowspan='5'
<?php
echo "<table border='1'>";
echo "<th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th>";
echo '<tr>';
echo "<td rowspan='5'>Cat</td>";
for($i=1;$i<=5;$i++){
echo '<td>'.$i.'</td>';
if($i==1){
echo "<td rowspan='5'>Good</td>";
}
echo " </tr>";
}
?>
and also you can used this code
for($i=1;$i<=5;$i++){
echo '<tr>';
if($i==1){
echo "<td rowspan='5'>Cat</td>";
}
echo '<td>'.$i.'</td>';
if($i==1){
echo "<td rowspan='5'>Good</td>";
}
echo " </tr>";
}
Create Table inside td:
<?php
echo "<table border='1'>";
echo "<tr><th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th></tr>";
echo "<tr><td style=''>Cat</td>";
echo "<td><table style='width:100%;'>";
for($i=1;$i<=5;$i++){
echo "<tr>";
echo "<td style='border-bottom:1pt solid black;'>".$i.'</td>';
echo "</tr>";
}
echo "</table></td>";
echo "<td>Good</td></tr>";
?>
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"
}
});
});
I am a few stages further in learning PHP but I have come to another annoying pit stop. I have a really simple bit of code that retrieves book items from my database. I am displaying them in an html table however because it is a loop, if I use the th tags for table header I get a header above every single data item!
Here is my code extract: (as you can see I have put my th tags as comments as that doesn't work)
<table border="0">
<br />
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
//echo "<tr>";
//echo "<th>";
//echo "Book Title";
//echo "</th>";
//echo "<th>";
//echo "Book Author";
//echo "</th>";
//echo "<th>";
//echo "Book Publisher";
//echo "</th>";
//echo "<th>";
//echo "Book ISBN";
//echo "</th>";
//echo "</tr>";
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
Move those echos out of your loop. Also, you shouldn't have a <br /> directly inside of a <table> tag.
Move your table header code outside of the loop.
IDIOT! Sorry guys....
Needed to put the th tags outside of the loop.... simple I know but easy to miss when your learning!
[=
Simply take the header outside the loop, so echo before you begin your loop but after the opening<table>
You have to move the headers above the loop:
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>"
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
echo "<td>";
...
What is static, stays static.
Wjat is dynamic, becomes PHP