How to hide a column if no data is present - php

I have a table that pulls information from the database. To keep it looking neat for the people visiting the webpage, I would like some columns to hide if no information is there.
My table is for results from motorsport events throughout the year. At the beginning of the year there will only be 1 event but by the time it comes to December there will be 20+ results added. So, instead of having lots of empty columns, I want them to be hidden until the title of that event has been added.
I tried to use !empty but this did not work as I wanted to it. It just moved the empty rows to the left leaving the headers not matching the correct results.
Here is my code:
<?php
//MySqli Select Query
$results = $mysqli->query("SELECT *, (u181 + u182 + u183 + u184 + u185 + u186 + u187 + u188 + u189 + u1810 + u1811 + u1812 + u1813 + u1814) AS total FROM results WHERE u18 = '1'");
$title = $mysqli->query("SELECT * FROM closedevents");
while ($t = $title->fetch_assoc()){
echo "<table width=\"1000\" border=\"0\" cellpadding=\"5\" cellspacing=\2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<td>Overall</td>
<td>Competitor</td>
<td>" . $t["u18a"] . "</td>
<td>" . $t["u18b"] . "</td>
<td>" . $t["u18c"] . "</td>
<td>" . $t["u18d"] . "</td>
<td>" . $t["u18e"] . "</td>
<td>" . $t["u18f"] . "</td>
<td>" . $t["u18g"] . "</td>
<td>" . $t["u18h"] . "</td>
<td>" . $t["u18i"] . "</td>
<td>" . $t["u18j"] . "</td>
<td>" . $t["u18k"] . "</td>
<td>" . $t["u18l"] . "</td>
<td>" . $t["u18m"] . "</td>
<td>" . $t["u18n"] . "</td>
<td>Total</td>
</tr>";
}
//set counter
$counter = 1;
while($row = $results->fetch_assoc()) {
echo "<tr align=\"center\">";
echo "<td>" . $counter . "</td>";
echo '<td>'.$row["competitor"].'</td>';
echo '<td>'.$row["u181"].'</td>';
echo '<td>'.$row["u182"].'</td>';
echo '<td>'.$row["u183"].'</td>';
echo '<td>'.$row["u184"].'</td>';
echo '<td>'.$row["u185"].'</td>';
echo '<td>'.$row["u186"].'</td>';
echo '<td>'.$row["u187"].'</td>';
echo '<td>'.$row["u188"].'</td>';
echo '<td>'.$row["u189"].'</td>';
echo '<td>'.$row["u1810"].'</td>';
echo '<td>'.$row["u1811"].'</td>';
echo '<td>'.$row["u1812"].'</td>';
echo '<td>'.$row["u1813"].'</td>';
echo '<td>'.$row["u1814"].'</td>';
echo '<td>'.$row["total"].'</td>';
echo "</tr>";
$counter++; //increment count by 1
}
echo "</table>";
?>
So, if there was no title name in u18j, k, l, m, n then them columns would not be shown on the table.

A PHP alternative:
<?php
//MySqli Select Query
$results = $mysqli->query("SELECT *, (u181 + u182 + u183 + u184 + u185 + u186 + u187 + u188 + u189 + u1810 + u1811 + u1812 + u1813 + u1814) AS total FROM results WHERE u18 = '1'");
$title = $mysqli->query("SELECT * FROM closedevents");
$all_cols=array("u18a" => "u181","u18b"=> "u182","u18c"=> "u183","u18d"=> "u184","u18e"=> "u185","u18f"=> "u186","u18g"=> "u187",
"u18h"=> "u188","u18i"=> "u189","u18j"=> "u1810","u18k"=> "u1811","u18l"=> "u1812","u18m"=> "u1813","u18n"=> "u1814");
while ($t = $title->fetch_assoc()){
// remember empty cols
$empty_cols=array();
echo "<table width=\"1000\" border=\"0\" cellpadding=\"5\" cellspacing=\2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<td>Overall</td>
<td>Competitor</td>";
foreach ($all_cols as $col => $value) {
if (!empty($t[$col])) {
echo "<td>" . $t[$col] . "</td>";
} else {
// set this column as empty for later
$empty_cols[]=$col;
}
} unset($col); unset($value);
echo "
<td>Total</td>
</tr>";
}
//set counter
$counter = 1;
while($row = $results->fetch_assoc()) {
echo "<tr align=\"center\">";
echo "<td>" . $counter . "</td>";
echo '<td>'.$row["competitor"].'</td>';
foreach ($all_cols as $col => $value) {
if (!in_array($col, $empty_cols)) {
// echo non-empty values
echo '<td>'.$row[$value].'</td>';
}
} unset($col); unset($value);
$counter++; //increment count by 1
}
echo "</table>";
?>

Well. I help you in jQuery. You must to include jquery library in your code, obviously. More information: http://jquery.com
First, you must to change your table with an identifier attribute:
echo "<table width=\"1000\" border=\"0\" cellpadding=\"5\" cellspacing=\2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<td>Overall</td>
<td>Competitor</td>
<td rel='a'>" . $t["u18a"] . "</td>
<td rel='b'>" . $t["u18b"] . "</td>
<td rel='c'>" . $t["u18c"] . "</td>
<td rel='d'>" . $t["u18d"] . "</td>
<td rel='e'>" . $t["u18e"] . "</td>
<td>Total</td>
</tr>";
And down, in your while, the same:
echo '<td rel="a">'.$row["u181"].'</td>';
echo '<td rel="b">'.$row["u182"].'</td>';
echo '<td rel="c">'.$row["u183"].'</td>';
echo '<td rel="d">'.$row["u184"].'</td>';
echo '<td rel="e">'.$row["u185"].'</td>';
Then the javascript magic:
// select the first row and found all td in the first row
$('table.entrywriting tr:first-child').find('td').each(function() {
var text = $(this).text(); // get the inner text
if(!text) { // if text is empty
var position = $(this).attr('rel'); //position like a, b, c...
// iterate all tds in this position
$('table.entrywriting td[rel="'+position+'"]').each(function() {
$(this).hide(); // hide the td with the position
});
}
});
Good luck!

I think you can test varibables and reduc to one echo instructions like this :
<?php
$counter = 1;
$out ='';
while($row = $results->fetch_assoc()) {
$out .= "<tr align=\"center\">";
$out .= "<td>" . $counter . "</td>";
$out .= (isset($row["competitor"]) && !empty(trim($row["competitor"]))?'<td>'.$row["competitor"].'</td>':'';
..... etc
$out .= "</tr>";
$counter++; //increment count by 1
}
echo $out;

Related

Formatting an SQL PHP script

I'm in a class, and I've been asked to retrieve information from an SQL table and display it as an invoice. I've got the basics down, however I've now been asked to format the PHP page to look something like this.
I'm a little confused as to how to do this or even where to begin for that matter.
PHP:
<?php
require("connect.php");
$inNo = $_POST["inNo"];
$sql = "SELECT invoice.invoice_no, invoice.date, invoice.cust_id, invoice.emp_id, invoice_line.prod_id, invoice_line.qty, product.cost_price, (product.cost_price * invoice_line.qty) FROM invoice INNER JOIN invoice_line ON invoice.invoice_no = invoice_line.invoice_no INNER JOIN product ON invoice_line.prod_id = product.id WHERE cust_id = '" . $inNo . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
//open table
echo '<table class="table table-striped" id="outTable">';
echo "<tr><th>Invoice no.</th><th>Date</th><th>Customer ID</th><th>Employee ID</th><th>Product ID</th><th>Qty</th><th>Price</th><th>Total cost</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>" . $row["invoice_no"]. "</td>
<td>" . $row["date"]. "</td><td>" . $row["cust_id"]. "</td>
<td>" . $row["emp_id"]. "</td><td>" . $row["prod_id"]. "</td>
<td>" . $row["qty"]. "</td><td>" . $row["cost_price"]. "</td>
<td>" . $row["(product.cost_price * invoice_line.qty)"]. "</td>
</tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Try this:
<?php
require("connect.php");
$inNo = $_POST["inNo"];
$sql = "SELECT invoice.invoice_no, invoice.date, invoice.cust_id, invoice.emp_id, invoice_line.prod_id, invoice_line.qty, product.cost_price, (product.cost_price * invoice_line.qty) FROM invoice INNER JOIN invoice_line ON invoice.invoice_no = invoice_line.invoice_no INNER JOIN product ON invoice_line.prod_id = product.id WHERE cust_id = '" . $inNo . "'";
$result = $conn->query($sql);
echo"<div style='width:50em'>";
if ($result->num_rows > 0) {
$count =0;
while($row = $result->fetch_assoc()) {
if ($count==0){
echo '<h2 style="text-align: center">Invoice no. " . $row["invoice_no"]. "</h2>';
echo '<table width=100%>';
echo "<tr style='text-align: left'><th>Date</th><th>Customer ID</th><th>Employee ID</th></tr>";
echo '<td>" . $row["date"]. "</td><td>" . $row["cust_id"]. "</td>
<td>" . $row["emp_id"]. "</td></table>';
echo '<table class="table table-striped" id="outTable" width=100% style="text-align: left">';
echo '<th>Product ID</th><th>Qty</th><th>Price</th></tr>';
}
echo "
<tr>
<td>" . $row["prod_id"]. "</td>
<td>" . $row["qty"]. "</td><td>" . $row["cost_price"]. "</td>
</tr>";
if ($count==$result->num_rows-1){
echo '<div style="text-align: right">Total cost: " . $row["(product.cost_price * invoice_line.qty)"]. "</div>';
}
$count++;
}
echo "</table></div>";
} else {
echo "0 results";
}
$conn->close();
?>

How can I check if all checkboxes have been checked in PHP?

I am implementing a list of items and each has a checkbox. I currently can see which checkboxes have been checked but what I want to do is check if all of them have been checked. How can I implement that?
Here is my code:
<form action="" method="post">
<?php
echo "<table>
<tr>
<th>Customer ID</th>
<th>Report ID</th>
<th>Report message</th>
<th>Device</th>
<th>Device no.</th>
<th>Barcode</th>
<th>IMEI</th>
<th>Sale-date</th>
</tr>";
while ($row2 = $clientUsername->fetch_assoc()) {
$_SESSION['cl_username'] = $row2["username"];
while ($row = $message->fetch_assoc()) {
$_SESSION['accept'] = $row["acceptance"];
$_SESSION['client_comment'] = $row["message"];
$_SESSION['name'] = $row["name"];
$_SESSION['sales_date'] = $row["sales_date"];
$_SESSION['date_sent'] = $row["date_sent"];
$_SESSION['countable_array'] = $row;
?>
<?php if ($row['acceptance'] == 3) {
echo "<tr> <td>
" . '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' . "
</td> <td>" . $cus_id . " </td> <td>" . $rep_id . "</td> <td>" . $_SESSION['client_comment'] . "</td> <td>" . $_SESSION['name'] . "</td> <td>" . $row["device_no"] . "</td> <td>" . $row["barcode"] . "</td> <td>" . $row["serial_imei"] . "</td> <td>" . $row["serial_no"] . "</td> <td>" . $row["sales_date"] . "</td></tr>";
echo "</table>";
}
}
}
</form>
if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['rejected'])) {
if (count($count_devices) == 1) {
...
}
}
<?php
while ($row2 = $clientUsername->fetch_assoc()) {
$_SESSION['cl_username'] = $row2["username"];
$i = 0; // initiate the variable here
$total_number_of_rows = $message->num_rows(); // Get total number of rows from your object
while ($row = $message->fetch_assoc()) {
$_SESSION['accept'] = $row["acceptance"];
$_SESSION['client_comment'] = $row["message"];
$_SESSION['name'] = $row["name"];
$_SESSION['sales_date'] = $row["sales_date"];
$_SESSION['date_sent'] = $row["date_sent"];
$_SESSION['countable_array'] = $row;
if ($row['acceptance'] == 3) {
$i++; // When conditions trues, increment the variable.
echo "<tr> <td>
" . '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' . "
</td> <td>" . $cus_id . " </td> <td>" . $rep_id . "</td> <td>" . $_SESSION['client_comment'] . "</td> <td>" . $_SESSION['name'] . "</td> <td>" . $row["device_no"] . "</td> <td>" . $row["barcode"] . "</td> <td>" . $row["serial_imei"] . "</td> <td>" . $row["serial_no"] . "</td> <td>" . $row["sales_date"] . "</td></tr>";
echo "</table>";
}
}
if($i == $total_number_of_rows){ // Here implement this condition, If both equal then all inputs have checked.
echo "Check box checked all inputs";
}
}
?>
I think you expecting the same as above. We need to check the Total number of rows with Incremental variable value.
Please review my comment inside the code part, so that you can understand terms.

Highlight or color the values of a specific row in table fetched using PHP and Mysql

I am a beginner to PHP. I am looking to highlight or color the values of first and last row in a table fetched using PHP and Mysql.
Below is the code.
<?php
$result_trend = mysql_query("select Week, New_Tickets, Closed_Tickets,
Backlogged, Resolution_Rate, Transactions from table");
while($row = mysql_fetch_array($result_trend))
{
echo "<tr>";
echo "<td>" . $row['Week'] . "</td>";
echo "<td>" . $row['New_Tickets'] . "</td>";
echo "<td>" . $row['Closed_Tickets'] . "</td>";
echo "<td>" . $row['Backlogged'] . "</td>";
echo "<td>" . $row['Resolution_Rate'] . "</td>";
echo "<td>" . $row['Transactions'] . "</td>";
}
echo "</tr>";
?>
I am not really sure how to proceed further. Any suggestion is much appreciated. Thanks in advance.
You can define a CSS style for the first and last rows as below:
.my-table tbody tr:first-child {
background-color: yellow;
}
<table class="my-table">
<thead>
<tr>
<td>Col 1</td>
<td>Col 2</td>
</tr>
</thead>
<tbody>
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>row</td>
</tr>
<tr>
<td>Third</td>
<td>row</td>
</tr>
</tbody>
</table>
You can simply obtain number of results and then count iterations. And if this is 1. row or last, add the style you need or css class that you'll style in your css file. Like this:
<?php
$result_trend = mysql_query("select Week, New_Tickets, Closed_Tickets,
Backlogged, Resolution_Rate, Transactions from table");
$rows_count = mysql_num_rows($result_trend);
$i = 0;
while($row = mysql_fetch_array($result_trend))
{
$i++;
$style = "";
if($i == 1 || $i == $rows_count) {
// $style .= "bgcolor=\"#FF0000\"" //for inline style, bad practise
$style .= "class=\"colorful\""
}
echo "<tr " . $style . ">";
echo "<td>" . $row['Week'] . "</td>";
echo "<td>" . $row['New_Tickets'] . "</td>";
echo "<td>" . $row['Closed_Tickets'] . "</td>";
echo "<td>" . $row['Backlogged'] . "</td>";
echo "<td>" . $row['Resolution_Rate'] . "</td>";
echo "<td>" . $row['Transactions'] . "</td>";
}
echo "</tr>";
?>

PHP IF inside <tr> <td> loop while looping results from MySQL database

I would need to display results in 3 rows and have same values under each other. Currently results are all going under each other. My code would have 3 IF sentences inside the loop.
My code is here:
echo "
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" class=\"data\">
<tr>
<td class=\"dataHeader\"><h2>Vapaa</h2></td>
<td class=\"dataHeader\"><h2>Varattu</h2></td>
<td class=\"dataHeader\"><h2>Käyttö estetty</h2></td>
</tr>
";
while($result = mysql_fetch_array($sql)) {
echo "<tr>
if ($result[tila] == 'vapaa') {
<td>" . $result['id'] . " " . $result['tila']. "</td>
}
</tr>";
}
echo "</table>";
I think you don't want the if to be as text. So you have to do like this:
while($result = mysql_fetch_array($sql)) {
echo "<tr> ";
//First if
if ($result[tila] == 'vapaa') {
echo "<td>" . $result['id'] . " " . $result['tila']. "</td>";
} else {
echo '<td></td>';
}
//Second if
if ($result[tila] == 'varattu') {
echo '<td>' , $result['id'] , ' ' , $result['tila']. '</td>';
} else {
echo '<td></td>';
}
// Here you can add more ifs.
echo "</tr>";
}
Because tables need to have the same number of cells (<td>) in a you need to add empty <td>s as describe above.
Wrong use of string quote delimiters.
echo '<table>';
while($result = mysql_fetch_array($sql)) {
echo '<tr>';
if ($result[tila] == 'vapaa') {
echo '<td>' , $result['id'] , ' ' , $result['tila']. '</td>';
}
echo '</tr>';
}
echo '</table>';

How to put database information as headers in table

I want some information that is stored in my database to be an header in a table.
Basically its got a list of competitors down the side with theirs scores running in rows. Each competitor has their own row. At the top of the table is the name of the event they took part in. I cant manage to make the event name come from the database so got to edit the page each time I add a set of scores.
I have attempted as you will see in the code below;
<?php
require("db_connect.php");
$result = mysql_query("SELECT *, (ct1 + ct2 + ct3 + ct4 + ct5 + ct6) AS results FROM resultsopen WHERE ct='1'");
if(mysql_num_rows($result) == 0) { echo 'No competitors found'; } else {
echo "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\" align=\"center\">
<tr align=\"center\">
<td>Competitor</td>
<td>Member No</td>
<td>" . $row['cta'] . "</td>
<td>E2</td>
<td>E3</td>
<td>E4</td>
<td>E5</td>
<td>E6</td>
<td>E7</td>
<td>Overall</td>
<td></td>
</tr>";
$x=1;
while($row = mysql_fetch_array($result))
{
if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#EEEEEE"; endif;
echo "<tr align=\"center\" bgcolor=\"" .$rowbgcolor. "\">";
echo "<td>" . $row['competitor'] . "</td>";
echo "<td>" . $row['memberno'] . "</td>";
echo "<td>" . $row['ct1'] . "</td>";
The line in question is <td>" . $row['cta'] . "</td>
The other lines are just text which I manually change each time.
I also know that mysql should be changed to mysqli but I am not confident in the change just yet.

Categories