PHP result set and HTML positioning - php

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

Related

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.

Formatting php into html table correctly

I have a php array that contains information from my database $name[] = $row['name']. There are also about 3 other rows that container email, age, and screen-resolution.
I am trying to neatly assemle this into a table that looks like:
name----------email----------age----------screen-res
name1---------email1--------age1---------res1
name2---------email2--------age2---------res2
However mine currently looks like this:
name----------email----------age----------screen-res
name1---------name2----------name3------name4---------name5------name6-------name7------name8
email1---------email2----------email3------email---------email5------email6-------email7
My Code
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Screen-Res</th>
</tr>
<?php
echo "<tr>";
foreach ($name as $nameVal) {
echo "<td>$nameVal</td>";
}
echo "</tr>";
echo "<tr>";
foreach ($email as $emailVal) {
echo "<td>$emailVal</td>";
}
echo "</tr>";
echo "<tr>";
foreach ($age as $ageVal) {
echo "<td>$ageVal</td>";
}
echo "</tr>";
echo "<tr>";
foreach ($screen-res as $screen-resVal) {
echo "<td>$screen-resVal</td>";
}
echo "</tr>";
?>
</table>
You are forming your tables incorrectly. You your table to look something like this. The good thing is you only need one array for all your data
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Screen-Res</th>
</tr>
<?php
foreach ($rows as $row) {
echo "<tr>";
echo "<td>".$row['nameVal']."</td>";
echo "<td>".$row['emailVal']."</td>";
echo "<td>".$row['ageVal']."</td>";
echo "<td>".$row['screen-resVal']."</td>";
echo "</tr>";
}
?>
</table>

Two PHP tables side by side

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

How to create a table in HTML using PHP

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) ?>

Displaying Reminders page from MySQL Database

I've created a PHP program for adding and viewing reminders. The add page works, but I'm having some trouble displaying them properly. How should I code this to only get the actual data? Also, how could I put this into an HTML table? (i.e. column for name, description, and date; rows are the retrieved data)
Thanks
When I open the file in my browser I get this as an output:
Array ( [reminderID] => 14 [reminderName] => Test [reminderDescript] => Test_Descript [reminderDate] => 2012 05 7 )
Code:
<?php include 'header.php'; ?>
<?php include 'database.php'; ?>
<div id="content">
<h1>Reminder List</h1>
<table align ="center">
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td>Date</td>
</tr>
</thead>
<?php
$query = 'SELECT * FROM reminder_event';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
print_r($row);
}
?>
</table>
<p><a href='reminder_add.php'>Add Reminder</a></p>
</div>
<?php include 'footer.php'; ?>
print_r() is a diagnostic tool for debugging, not to be used for real output. Instead, output HTML to a table using the array keys fetched from your row.
// Open a table
echo "<table>";
while($row = mysql_fetch_assoc($result)) {
// Output each row
echo "<tr>
<td>{$row['reminderName']}</td>
<td>{$row['reminderDescript']}</td>
<td>{$row['reminderDate']}</td>
</tr>";
}
// Close the table
echo "</table>";
Better still, escape each of the values for HTML output with [htmlspecialchars()] (http://us3.php.net/manual/en/function.htmlspecialchars.php) before output to prevent cross-site scripting attacks and broken HTML if characters like < > & are encountered in the values.
echo "<table>";
while($row = mysql_fetch_assoc($result)) {
// Encode all values for HTML output
$name = htmlspecialchars($row['reminderName']);
$desc = htmlspecialchars($row['reminderDescript']);
$date = htmlspecialchars($row['reminderDate']);
// Then output the encoded values
echo "<tr><td>$name</td><td>$desc</td><td>$date</td></tr>";
}
echo "</table>";
Change:
while($row = mysql_fetch_assoc($result)) {
print_r($row);
}
To:
while($row = mysql_fetch_assoc($result)) {
echo $row['reminderID'];
echo $row['reminderName'];
echo $row['reminderDescript'];
echo $row['reminderDate'];
}
You can echo those values out in whatever HTML you'd like. So, for example, if you want it in a table you would do something like this:
echo "<table><tr>";
while($row = mysql_fetch_assoc($result)) {
echo "<td>" . $row['reminderID'] . "</td>";
echo "<td>" . $row['reminderName'] . "</td>";
echo "<td>" . $row['reminderDescript'] . "</td>";
echo "<td>" . $row['reminderDate'] . "</td>";
}
echo "</tr></table>";
You can clean that up a bit to take some (or all) of the HTML out of the PHP.
<?php include 'header.php'; ?>
<?php include 'database.php'; ?>
<div id="content">
<h1>Reminder List</h1>
<table>
<thead><tr><td>id</td><td>name</td><td>description</td><td>date</td></tr></thead>
<tbody>
<?php
$query = 'SELECT * FROM reminder_event';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['reminderID']; ?></td>
<td><?php echo $row['reminderName']; ?></td>
<td><?php echo $row['reminderDescript']; ?></td>
<td><?php echo $row['reminderDate']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<p><a href='reminder_add.php'>Add Reminder</a></p>
</div>
<?php include 'footer.php'; ?>

Categories