MySql Table with different colors by value - php

I am using mysql queries to fetch data from db. All my data are showing fine in tables. Now I want to color the status by value less than 2 or more than 3.
The below code is not working.
Need help.
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
?>
<?php
function status_style($row) {
if ($row < 2) return 'background-color: #ff0000'; //red
if ($row > 3) return 'background-color: #33cc33'; //green
return '';
}
?>
<?php
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
?>

You have already created function to color the rows but haven't called the function during showing output. That's why it is not working.
Change your $output variable with:
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td style="'.status_style($row["status"]).'">'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';

You can try something like this:
<?php
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
function status_style($row) {
if ($row < 2) return $color = '#ff0000'; //red
if ($row > 3) return $color = '#33cc33'; //green
return $color = '';
}
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr style='"'background-color: '"' . $color . '"'>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
You didn't do anything with the background color.

Related

PHP - Inputting data into MySQL cell

I am trying put row['user'] into cell but it doesn't work.
When I uncomment "echo" it works fine.
PHP:
function mod_Something($database)
{
$sql = "SELECT user FROM table_name";
if ($result = mysqli_query($database, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
$html = $html . '<tr><td>' . $row['user'] . '</td></tr>';
// echo $row['user'];
}
return $html;
}
}
I also have a HTML view file where I have:
<table id="data-table-basic" class="table table-striped">
<thead>
<tr>
<th>user</th>
</tr>
</thead>
<tbody>
%mod_Something%
</tbody>
</table>
I know that HTML isn't a function but I must return it because there is a script which allows to return "view".
try this:
function mod_Something($database)
{
$sql = "SELECT user FROM table_name";
if ($result = mysqli_query($database, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr><td>' . $row['user']; . '</td></tr>';
}
return $html;
}
}
<table id="data-table-basic" class="table table-striped">
<thead>
<tr>
<th>user</th>
</tr>
</thead>
<tbody>
<?php mod_Something(); ?>
</tbody>
</table>
You are not getting anything from the function mod_Something because you are not building & returning HTML properly.
See the below code.
function mod_Something($database)
{
$html = "";
$sql = "SELECT user FROM table_name";
if ($result = mysqli_query($database, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
$html .= '<tr><td>' . $row['user'] . '</td></tr>';
}
}
return $html;
}
Hope this works for you!

PHP pagination error with only 1 page

I have a problem with pagination, i see only first page, i try to undestand. I have a file HTML with a script and the script launch a file php. But the result is only a file
<?php
include 'database.php';
$output = '';
$start=0;
$limit=2;
if(isset($_GET['id'])){
$id=$_GET['id'];
$start=($id-1)*$limit;
}else{
$id=1;
}
$search = $_POST["search"];
if($search == ''){
$sql = "SELECT * FROM dati ORDER BY id DESC LIMIT $start, $limit";
}
else {
$sql = "SELECT * FROM dati WHERE nome LIKE '%".$_POST["search"]."%' ORDER BY id DESC LIMIT $start, $limit";
}
$retval = mysqli_query( $dbconfig, $sql);
$output .= '
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Nome</th>
<th>Categoria</th>
<th>Numero di Telefono</th>
</tr>
</thead>
<tbody>';
while($row=mysqli_fetch_array($retval)){
$output .= '
<tr>
<td>'. $row['nome'] . '</td>
<td>'. $row['categoria'] . '</td>
<td>'. $row['telefono'] . '</td>
</tr>';
}
$output .= '
</tbody>
</table>';
$ret = mysqli_num_rows(mysqli_query( $dbconfig, $sql));
$total=ceil($ret/$limit);
$output .= '
<div class="row">
<ul class="page">';
for($i=1;$i<=$total;$i++){
if($i==$id) {
$output .= '<li class="corrente">'.$i.'</li>';
}else {
$output .= '<li><a href=?id='.$i.'>'.$i.'</a></li>';
}
}
$output .= '
</ul>
</div>';
echo $output;
?>
This is file PHP with pagination but nothing
<input type="submit" name="test" id="test" value="Ricerca">

how to set text values for CardNum when i click export to excel

if(isset($_POST["export_excel"]))
{ $sql = "SELECT tract1,CardNum,tract2,tract3 FROM exceldata a LEFT JOIN tblcardnum b on a.EXID =b.EXID ";
$result = mysqli_query($connect, $sql);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<table class="table" bordered="1">
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["tract1"].'</td>
<td>'.$row["CardNum"].'</td>
<td>'.$row["tract2"].'</td>
</tr>
';
}
$output .= '</table>';
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=download.xls");
echo $output;
}
}

DataTable plugin not working with dynamically generated php table (arrows wouldn't show up too)

I created a table from using php and added the plugin (used demo_table.css). The arrows wouldn't show on the result page and it doesn;t apply any css.
Here is my php code:
$result = mysqli_query($link, $sql);
$num_rows = mysqli_num_rows($result);
echo '<table cellpadding = "0" cellspacing="0" border="0" id="datatables" class="display">';
echo '<thead>';
echo '<tr>';
echo '<th>SNo.</th>
<th>Username</th>
<th>Trip ID</th>
<th>Destination</th>
<th >Leave Date</th>
<th>Return Date</th>
<th>Total</th>
<th>Submit Date</th>
<th >Status</th>
<th >Reason</th>
<th >Update</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
} else {
echo('not valid user');
}
if (!$result) {
die(mysqli_error($link));
}
if ($num_rows<=0){
echo('not valid user');
}
elseif ($num_rows>0){
$i=1;
while($row = mysqli_fetch_assoc($result)) {
$grandtotal = $row['flight_estimate'] + $row['registration_fee'] + $row['total_cost'];
$status = $row['status'];
if(($status == 'Approved') || ($status == 'Denied')){
$newcolumn = '<td></td>';
}
else{
$newcolumn = '<td>Approve/Deny</td>';
}
echo '<tr>';
echo '<td>'. $i++ .'</td><td>'.$row['username'].'</td><td>'.$row['trip_id'].'</td><td>'.$row['destination'].'</td><td>'.date('d/m/Y',strtotime($row['leave_date'])).'</td><td >'.date('d/m/Y',strtotime($row['return_date'])).'</td><td >'. round($grandtotal,2).'</td><td >'.date('d/m/Y',strtotime($row['updateddate'])).'</td><td >'.$row['status'].'</td><td >'.$row['reason'].'</td>'.$newcolumn;
}
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
?>
Is there anything that I'm missing? All it does is center all the column values. But when I click on header nothing happens.
You need to set 'bJQueryUI' to true when you init dataTable
$("#datatables").dataTable({ 'bJQueryUI' : true });
It would help if you posted your js script as that is where your problem is.

php Multiple looping problem

Im writing a function to get the parent and child ids but for the third loop there is a
problem the loop gets even the previous loops id also .
How can i avoid it?
<?
$results = '
<table>
<thead>
<tr >
<td id="ticket" align="center" ><b>Task<br />ID</b></td>
<td id="ticket" align="center" ><b>col1</td>
<td id="ticket" align="center" ><b>col2</td>
</tr>
</thead>
<tbody>';
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
$results .='
<tr >
<td align="center">
'.$row['Task_id'].'
</td>';
$results .= '<td align="center">';
$gg = mysqli_query($dbc,"select * from Tasks where ParentTask_Id='".$row['Task_id']."'");
echo "<br>";
while ($rowdd = mysqli_fetch_assoc($gg))
{
$results .= $rowdd['Task_id']."<br><br>";
$gg2 = mysqli_query($dbc,"select * from Tasks where ParentTask_Id='".$rowdd['Task_id']."'");
while ($rowdd2 = mysqli_fetch_assoc($gg2))
{
$results2 = $rowdd2['Task_id']."<br><br>";
}
echo "<br>";
}
// $results .= $car ;
// $results .= $t;
$results .='</td>';
$results .=' <td align="left" >'?>
<?
$results .= $results2;
$results .='</td>';
$results .='
</tr>';
}
?>
Is the $results variable empty? I only see it being concatenated.
Also, on your table you have multiple ids that are the same. You either need to change that to a class or have a unique value for each id.

Categories