Two PHP tables side by side - php

I am new to PHP; please help me.
I am trying to align two PHP tables with images in them side by side but they are displaying one below the other. I want two tables side by side under one heading and two tables side by side under second heading. I've seen some solutions in HTML but I am looking for PHP. Please find screenshot of my error and my code below:
Please feel free to ask for any clarifications.
$prodcatSQL="select prodcatid, prodcatname, prodcatimage from prodcat"; // create an $sql variable and store the sql statement
$exeprodcatSQL=mysql_query($prodcatSQL) or die (mysql_error());
while ($arrayprod=mysql_fetch_array($exeprodcatSQL))
{
echo "<strong>Using display: inline-block; </strong><br>\n";
echo "<table border=1 class=\"inlineTable\">\n";
echo "<tr>\n";
echo "<td><p><a href=products.php?u_prodcatid=".$arrayprod['prodcatid'].">";
echo $arrayprod['prodcatname'];
echo "<p><img src=images/".$arrayprod['prodcatimage']."></p>";
echo "</a></p></td>\n";
echo "</tr>\n";
echo "</table>\n";
}
echo "<h3><center>".$subheading."</center></h3>";
$treatcatSQL="select treatcatid, treatcatname, treatcatimage from treatcat"; // create an $sql variable and store the sql statement
$exetreatcatSQL=mysql_query($treatcatSQL) or die (mysql_error());
while ($arrayprod=mysql_fetch_array($exetreatcatSQL))
{
echo "<strong>Using display: inline-block; </strong><br>\n";
echo "<table border=1 class=\"inlineTable\">\n";
echo "<tr>\n";
echo "<td><p><a href=treatmentpackages.php?u_treatcatid=".$arrayprod['treatcatid'].">";
echo $arrayprod['treatcatname'];
echo "<p><img src=images/".$arrayprod['treatcatimage']."></p>";
echo "</a></p></td>\n";
echo "</tr>\n";
echo "</table>\n";
}

You have nested <p> tags which may be introducing unnecessary new lines. Remove the tags and replace them with separate <td> elements and the alignment should be fine.
$prodcatSQL="select prodcatid, prodcatname, prodcatimage from prodcat"; // create an $sql variable and store the sql statement
$exeprodcatSQL=mysql_query($prodcatSQL) or die (mysql_error());
while ($arrayprod=mysql_fetch_array($exeprodcatSQL))
{
echo "<strong>Using display: inline-block; </strong><br>\n";
echo "<table border=1 class=\"inlineTable\">\n";
echo "<tr>\n";
echo "<td><a href=products.php?u_prodcatid=".$arrayprod['prodcatid'].">";
echo $arrayprod['prodcatname'];
echo "</a></td><td><a href=products.php?u_prodcatid=".$arrayprod['prodcatid']."><img src=images/".$arrayprod['prodcatimage'].">";
echo "</td></a>\n";
echo "</tr>\n";
echo "</table>\n";
}
$treatcatSQL="select treatcatid, treatcatname, treatcatimage from treatcat"; // create an $sql variable and store the sql statement
$exetreatcatSQL=mysql_query($treatcatSQL) or die (mysql_error());
while ($arrayprod=mysql_fetch_array($exetreatcatSQL))
{
echo "<strong>Using display: inline-block; </strong><br>\n";
echo "<table border=1 class=\"inlineTable\">\n";
echo "<tr>\n";
echo "<td><a href=treatmentpackages.php?u_treatcatid=".$arrayprod['treatcatid'].">";
echo $arrayprod['treatcatname'];
echo "</a></td><td><a href=treatmentpackages.php?u_treatcatid=".$arrayprod['treatcatid'].">";
echo "<img src=images/".$arrayprod['treatcatimage']."></a>";
echo "</td>\n";
echo "</tr>\n";
echo "</table>\n";
}
If this does not work, please edit the HTML output into the question.

You need to add this to your table, if you don't want external CSS
Replace this in each,
echo "<table border=1 class=\"inlineTable\">\n";
With this
echo "<table border=1 class=\"inlineTable\" style=\"width:50%;float:left;\">\n";

Related

Tinymce echo Images next to each other instead below

I have a MYSQL database where I store images for reports I can import them to the Tinymce text area but they are being placed below each other. Is there away I can make them place two two next to each other. Some reports have 2 photos and other might have 10 depending on how many was loaded to the DB
This is my current code:
$result = mysqli_query($db, "SELECT * FROM images WHERE file_nr = '$id'");
?>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img width='300' height='300' display: inline-block;
src='../../cases_1/overdue/images/".$row['image']."' >";
echo "<p>".$row['image_text']."</p>";
echo "</div>";
}
?>
I tried inline-block not working.
I don't think this is the cleanest method but got this working
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img width='350px' height='350px' style='float: left; padding: 4px;' src='../../cases_1/overdue/images/".$row['image']."' >";
echo "</div>";
}
?>

Bootstrap alternating row color in dynamically created table

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; }

Change cell color based on MYSQL value

I want to change td color using if statement but somehow my code is not affecting all rows
this is my code :
require_once("../model/materiel.class.php" . "");
$mt=new materiel();
$data=$mt->afficher_tous1();
echo '<table id="customers2" class="table datatable table-striped">';
echo "<thead>
<tr>
<th>Qte disponible</th>
<th>Alert</th>
</tr>
</thead>";
echo "<tbody>";
foreach($data as $t){
echo "<tr>";
if ($t['qte_disponible_m'] == 0){
echo "<td bgcolor='red'>".$t['qte_disponible_m']."</td>";
}else if ($t['qte_disponible_m'] > $t['alert_m']){
echo "<td bgcolor='green'>".$t['qte_disponible_m']."</td>";
}else if ($t['qte_disponible_m'] == $t['alert_m']){
echo "<td bgcolor='yellow'>".$t['qte_disponible_m']."</td>";
}
echo "<td>".$t['alert_m']."</td>";
echo "</tr>";
}
echo "</tbody>";
echo"</table>";
the problem i have see the screenshot below :
If statement is like jumping next row
Add a class with the CSS background-color property (with !important if needed) to the TD instead of bgcolor. The bgcolor gets overwritten by the table-striped class.

PHP result set and HTML positioning

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'],'&nbsp','opponent','&nbsp',$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"],'&nbsp', $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["score"];
echo "</td>";
echo "</tr>";
}else{
echo "<tr>";
echo "<td>";
echo $row["firstname"],'&nbsp', $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'],'&nbsp','opponent','&nbsp',$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.
}

Applying custom font to table cell inside PHP echo

I am currently working on a project which pulls in tables from a mySQL which are shown in a html table. Below you can see the code.
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<table cellpadding=1 border=1 width=100%>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>"."<center>".$row[0]."</center>"."</td>";
echo "<td>" .$row[2]."</td>";
echo "<td>" .$row[1]."</td>";
echo "<td>" .$row[3]."</td>";
echo "</tr>";
}
echo "</table>";
}
I need to change it to
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<table cellpadding=1 border=1 width=100%>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>"."<center>".$row[0]."</center>"."</td>";
echo "<td>" .$row[2]."</td>";
echo "<td>" .$row[1]."</td>";
echo "<td><font face="Georgia, Times New Roman, Times, serif">" .$row[3]."</font></td>";
echo "</tr>";
}
echo "</table>";
}
When I do this is throws back errors. I am doing this so the user sees a barcode which i have a font for.
Am I doing this wrong? or is there another method which will work better.
Thanks
Ryan
Change " to ' for face:
echo "<td><font face='Georgia, Times New Roman, Times, serif'>" .$row[3]."</font></td>";
Also instead of inline font you should use css class or id. (check this for example of css class Applying css to a php table)
Try
echo "<td><font face='Georgia, Times New Roman, Times, serif'>" .$row[3]."</font></td>";
OMG.. don't use font tag. It's deprecated since HTML 4.01 and not available in HTML 5. The error comes from not escaping properly.
echo "<td><font face=\"Georgia, 'Times New Roman', Times, serif\">" ...
But please use a class and do the styling with CSS.
echo "<td class=\"font-georgia\">...
And in css:
.font-georgia {
font-family: Georgia, 'Times New Roman', Times, serif;
}
Note the quotes for 'Times New Roman' because it has spaces.
The same for center tag. Use text-align:center CSS instead.

Categories