Im doing a page to visualice the content of some tables(mysql) and then I thought to do a button to print this tables but some tables cut the content. I will post the table to print and some images.
The code of the table to print:
<?php
$a = $index - 1;
echo "<div id='{$a}' class='table-responsive d-block'>"
?>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<?php
$val = [];
$sql = "SHOW COLUMNS FROM {$valor}";
$result = $connection->query($sql);
while ($row = mysqli_fetch_array($result)) {
echo "<th class='text-secondary col-md-auto text-capitalize'>{$row['Field']}</th>";
$val[] = $row['Field'];
}
?>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM {$valor}";
$result = $connection->query($sql);
if (!$result) {
echo ("No se encontro resultado");
die("Invalid query: " . $connection->error);
}
while ($row = $result->fetch_assoc()) {
echo "<tr>";
foreach ($val as &$campo) {
if (isset($row['Link']) && !empty($row['Link'])) {
echo "<td class='text-primary' id='{$campo}'><a style='text-decoration: none;' target='_blank' href='{$row['Link']}'>$row[$campo]</a></td>";
} elseif (isset($row['Marca']) && isset($row['Modelo']) && !empty($row['Marca']) && !empty($row['Modelo'])) {
echo "<td class='text-primary' id='{$campo}'><a style='text-decoration: none;' target='_blank' href='http://www.google.com/search?q={$row['Marca']}+{$row['Modelo']}'>$row[$campo]</a></td>";
} else {
echo "<td class='text-primary' id='{$campo}'>$row[$campo]</td>";
}
}
echo "</tr>";
}
?>
</tbody>
</table>
</div>
And some examples:
Perfect example
Bad example
please... I'm trying to make the question clear, to answer well and to do my best, don't ban my post as usual or insult me. English is not my forte either, I do what I can, constructive criticism would help me much more.
I only want that table with long link, dont be cut.
Maybe a solution is deleting links and write shorcuts?
Well, finally I got the solution:
class='text-wrap'
From boostrap.
That's works too:
#media print {
table,
table tr td,
table tr th {
page-break-inside: avoid;
}
}
Related
I am trying to build a editable table with dynamic headers and dynamic rows based on header, but I lost my way in the process.
I got the dynamic headers:
<div class="jumbotron">
<div class="container">
<table class='table'>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Emails</th>
<?php
include("../connection/connection.php");
$get_title = "SELECT DISTINCT(title) FROM attendance";
$query1 = mysqli_query($con,$get_title);
if ($query1) {
# code...
while ($row=mysqli_fetch_array($query1)) {
# code...
$title[] = $row['title'];
}
$count = count($title);
for ($i=0; $i <$count ; $i++) {
# code...
echo "<th>".$title[$i]."</th>";
}
}
?>
</tr>
</thead>
<tbody>
<?php
$get_emails = "SELECT * FROM subjects";
$query2 = mysqli_query($con,$get_emails);
if ($query2) {
# code...
while ($row=mysqli_fetch_array($query2)) {
# code...
$emails = $row['email'];
$id = $row['id'];
$get_name = "SELECT * FROM users";
$query3 = mysqli_query($con,$get_name);
if ($query3) {
# code...
while ($row=mysqli_fetch_array($query3)) {
# code...
$fullname = $row['user_fullname'];
echo "<tr>";
echo "<td contenteditable>".$id."</td>";
echo "<td contenteditable>".$fullname."</td>";
echo "<td contenteditable>".$emails."</td>";
echo "</tr>";
}
} else {
echo "<tr>";
echo "<td contenteditable>".$id."</td>";
echo "<td contenteditable></td>";
echo "<td contenteditable>".$emails."</td>";
echo "</tr>";
}
}
} else {
echo "<tr>";
echo "<td contenteditable></td>";
echo "<td contenteditable></td>";
echo "<td contenteditable></td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
Now I need to echo the table td which would be dynamic based on the headers after the emails - td inside the tr.
Any help is appreciated.
The major problem you have is that the array element isn't going to be called title but rather DISTINCT(title) because you didn't alias it as something else, the code below shows it being aliased back as title, and as well eliminating what appears to be an unnecessary loop.
$get_title = "SELECT DISTINCT(title) title FROM attendance";
$query1 = mysqli_query($con,$get_title);
if ($query1) {
# code...
while ($row=mysqli_fetch_array($query1)) {
# code...
echo "<th>".$title[$i]$row['title']."</th>";
}
}
Looking in Stack Overflow I found alternating row color.
While this seems to work on a static table, my results is that all rows are pink. I cannot get it to work on a PHP dynamically created table with bootstrap:
$mdbFile = "\myAccessDatabase.mdb";
$pdo = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$mdbFile", "", "");
$query = $pdo->prepare("SELECT * FROM Table");
$query->execute();
echo "<table class='table table-striped table-bordered table-hover table-condensed table-responsive'>";
echo "<thead><tr>";
echo "<th>Last</th><th>First</th><th>XXX</th><th>YYY</th><th>ZZZ</th><th>WWW</th>";
echo "</tr></thead>";
for($i=0; $row = $query->fetch(); $i++){
echo '<tbody><tr>';
//echo "<th scope='row'>1</th>";
echo "<td>".$row['LAST']."</td>";
echo "<td>".$row['FIRST']."</td>";
echo "<td>".$row['XXX']."</td>";
echo "<td>".$row['YYY']."</td>";
echo "<td>".$row['ZZZ']."</td>";
echo "<td>".$row['WWW']."</td>";
}
echo "</tr></tbody></table>";
unset($pdo);
unset($query);
CSS:
.table-striped>tbody>tr:nth-child(odd)>td,
.table-striped>tbody>tr:nth-child(odd)>th {
background-color: pink;
}
Problem is probably that your opening tbody tag is inside a loop. So each row is treated as a frist one. Try taking it outside:
echo '<tbody>';
for($i=0; $row = $query->fetch(); $i++){
echo '<tr>';
//echo "<th scope='row'>1</th>";
echo "<td>".$row['LAST']."</td>";
echo "<td>".$row['FIRST']."</td>";
echo "<td>".$row['XXX']."</td>";
echo "<td>".$row['YYY']."</td>";
echo "<td>".$row['ZZZ']."</td>";
echo "<td>".$row['WWW']."</td>";
echo '</tr>';
}
echo "</tbody></table>";
Try this .table-striped>tbody>tr:nth-of-type(odd) { background-color: pink; }
I insert MySQL db result into HTML table, after that I am trying to change row colors using two colors,
for example if 1st row has red 2nd row has yellow again 3rd row has
red like wise..
what is the best possible way to do that, I wrote PHP code using PHP modulus function, is there any easist way to do that thank you..
<?php
$result = mysqli_query($link, "SELECT * FROM example");
?>
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<?php
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
if ($i % 2 == 0) {
$bgColor = ' style="background-color:#CCFFFF;" ';
} else {
$bgColor = ' style="background-color:#FFFF99;" ';
}
echo "<tr>";
echo "<td $bgColor>";
echo $row['name'];
echo "</td>";
echo "<td $bgColor>";
echo $row['age'];
echo "</td>";
echo "</tr>";
$i++;
}
?>
</table>
It can be done via CSS
tr:nth-child(even) {background: yellow}
tr:nth-child(odd) {background: red}
Source: http://www.w3.org/Style/Examples/007/evenodd.en.html
include css
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
replace table tag with
<table class="table table-striped" >
Instead of inline styles you should use classes .red and .yellow.
<?php
$result = mysqli_query($link, 'SELECT * FROM example');
?>
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<?php
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr class="' . (($i % 2 == 0) ? 'red' : 'yellow') . '">';
echo '<td>';
echo $row['name'];
echo '</td>';
echo '<td>';
echo $row['age'];
echo '</td>';
echo '</tr>';
$i++;
}
?>
</table>
You able to make it not using PHP at all.
Just use CSS with :nth-child pseudo-class
tr:nth-child(2n) {
background: #f0f0f0; /* row background color */
}
tr:nth-child(1) {
background: #666; /* row background color */
color: #fff; /* text color */
}
By the way, It's very bad way to show data in interface to mixing process of getting it from DB and showing it in view (putting to output buffer). This code looks like as an traning example fragment from old ugly bloody book for PHP3. It's reqired to separate 2 process: first getting ALL data from DB, and after that puting it into output with appearance.
I have problem with positioning html tags I did try to solve the problem but for me the code looks fine however the output tells something different I did try do to sort out the output of SQL query however I wasn't able to achieve this and when I did asked for help in the forum in reply I get that it would be much easier to do it in PHP. So I have produce following code but I am not getting required results:
<body>
<div class="container">
<?php
$sdate = '';
foreach($rows as $row) {
if($row['shieldDate'] != $sdate){
$sdate = $row['shieldDate'];
echo '<h2>';
echo $row['shieldDate'],' ','opponent',' ',$row['shieldTeam'];
echo '</h2>';
echo "
<p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>
<table class='table'>
<thead>
<tr>
<th>Player</th>
<th>Score</th>
</tr>
</thead>
<tbody>
";
echo "<tr>";
echo "<td>";
echo $row["firstname"],' ', $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["score"];
echo "</td>";
echo "</tr>";
}else{
echo "<tr>";
echo "<td>";
echo $row["firstname"],' ', $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["score"];
echo "</td>";
echo "</tr>";
}
}
?>
</tbody>
</table>
</div>
</body>
I am trying to acheive following output:
However I am getting:
You don't close your tbody and table tags between two dates.
Each time you encounter a new date, you open a new table tag, but you never close it before having a new h2 date title. Add a line to close it.
$sdate = '';
foreach($rows as $row) {
if($row['shieldDate'] != $sdate){
// It's a new date
if (!empty($sdate)) {
// It's not the first date: close previous table
echo '</tbody></table>';
}
$sdate = $row['shieldDate'];
echo '<h2>';
echo $row['shieldDate'],' ','opponent',' ',$row['shieldTeam'];
echo '</h2>';
echo "<p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>
<table class='table'>
<thead>[...]</thead>
<tbody>";
[...]
} else {
[...]
}
}
if (!empty($sdate)) {
// There has been at least one date (at least one table): close it
echo '</tbody></table>';
}
Note that the [...] content is the same in if and else instructions.
You could do this to have something cleaner. The less duplicate code you have, the more easy it is to read. The more easy to read your code is, the better.
foreach($rows as $row) {
if($row['shieldDate'] != $sdate){
// Do your h2 and open table stuff
}
echo "<tr>";
echo "<td>";
// etc. No need to put it in the if AND in the else instructions.
}
I have 5 pictures stored in a folder and their links stored on the database.
I want to put them in a table of three columns on each row.
<body>
<center>
<table border='1'>
<?php
$host="";
$username="";
$password="";
$db_name="fruits_db";
$tbl_name="fruits_tbl";
$connection=mysqli_connect("$host","$username","$password","$db_name");
if (mysqli_connect_errno())
{
echo "The application has failed to connect to the mysql database server: " .mysqli_connect_error();
}
$result = mysqli_query($connection, "SELECT * FROM fruits_tbl")or die("Error: " . mysqli_error($connection));
$num_rows=mysqli_num_rows($result);
$rows = $num_rows/3;
for($i=1; $i<=$rows ; $i++)
{
echo "<tr>";
for($j=1; $j<=3; $j++)
{
while($row = mysqli_fetch_array($result))
{
echo
("<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>"
);
}
}
echo "</tr>";
}
mysqli_close($connection);
?>
</table>
</center>
</body>
</html>
The above code I created, contains two FOR loops. The script should count the number of rows in the table, and then divide by 3(the number of columns on each row in the HTML table).
I wonder where I'm going wrong wit this code.
With your while($row = mysqli_fetch_array($result)){} inside your 1st for loop it will run through all your rows, before the outside loop runs 2nd/3rd time.
Here is another way to do it -
$counter = 1;
// start 1st row
echo "<tr>";
while($row = mysqli_fetch_array($result)){
// if the 4th cell, end last row, and start new row
if ($counter%3==1){
echo "</tr><tr>";
}
echo
"<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>";
// increase the counter
$counter++;
}
// close the last row
echo "</tr>";
You're looping through all the results in the first table cell.
Try something like this instead:
for($i=1; $i<=$rows ; $i++) {
echo "<tr>";
for($j=1; $j<=3; $j++) {
$row = mysqli_fetch_array($result);
if ($row) {
echo(
"<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>"
);
}
}
echo "</tr>";
}
If you just want to format the display with a new row after every 3 records, you could use the modulus operator:
$cntr = 0;
echo '<tr>';
while($row = mysqli_fetch_array($result)) {
$cntr++;
echo '
<td width="180px" height="200px">
<div class="fruit_image">
<img src="'.$row['fruit_image'].'" />
</div>
<div class="fruit_title">'.$row['fruit_name'].'</div>
</td>';
if ($cntr % 3 == 0 && $cntr != $num_rows)
echo '</tr><tr>';
}
echo '</tr>';
Keep in mind however that all the solutions presented so far may leave you with a last row with one or two td elements. You can fill this if you desire with empty <td> </td> columns.
print "<center><table border=1>
<tr>
<td>id</td>
<td>name</td>
<td>company</td>
<td>branch</td>
<td>job title</td>
<td>contact</td>
<td>email</td>
<td>mgs</td>
</tr>";
while($row=mysql_fetch_array($query))
{
print "<tr>";
for ($i=0;$i<=(count($row)/2);$i++)
{
print "<td>$row[$i]</td>";
} print"</tr>";
}
}
else{echo " <p>No Records Found</p>";}
<?php
function studentTable($name,$grade){
echo "<tr> <td>$name</td><td>$grade</td></tr>";
}
?>
<table style="width:100%" border="solid">
<tr>
<th>Name</th>
<th>Grade</th>
</tr>
<?php studentTable("Sarah", 90) ?>