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.
Related
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>";
}
?>
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.
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 this code reading values from a MySQL DB.
I've fiddled with classes in css to style the table cell backgrounds - but can't see how to do it in this case. It's almost like the php gets in the way - but I need it to read the results!!
The 'grades' in each variable will only ever be 1,2,3 or 4.
Any ideas?
<?php while($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td>'.$row['si_grade'].'</td>';
echo '<td>'.$row['att_grade'].'</td>';
echo '<td>'.$row['pro_grade'].'</td>';
echo '<td>'.$row['qot_grade'].'</td>';
echo '<td>'.$row['qoe_grade'].'</td>';
echo '</tr>';
}?>
Eventually, I want a 'set of coloured cells'...
You could set a CSS class based on the values and use that
.val1 { background-color: red; }
.val2 { background-color: blue; }
# etc
Then you do this in the PHP code
<?php while($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td class="val'.$row['si_grade'].'">'.$row['si_grade'].'</td>';
echo '<td class="val'.$row['att_grade'].'">'.$row['att_grade'].'</td>';
echo '<td class="val'.$row['pro_grade'].'">'.$row['pro_grade'].'</td>';
echo '<td class="val'.$row['qot_grade'].'">'.$row['qot_grade'].'</td>';
echo '<td class="val'.$row['qoe_grade'].'">'.$row['qoe_grade'].'</td>';
echo '</tr>';
}?>
The information from the database (1,2,3,4) will populate as data inside the cells of the table. What you want is to put it in the class of the <td> like such:
<?php while($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td class='.$row['si_grade'].'></td>';
echo '<td class='.$row['att_grade'].'></td>';
echo '<td class='.$row['pro_grade'].'></td>';
echo '<td class='.$row['qot_grade'].'></td>';
echo '<td class='.$row['qoe_grade'].'></td>';
echo '</tr>';
}
Edited to Add: Machavity's answer also gives the value in the cell, mine only colors the cell.
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";