I have the following code and my MySQL table has a column (not showing) called ativo. I need the $result_rowset['Nome'][$i] = $row['nome']; to have background-color changed to red if ativo = 0 and blue if ativo = 1.
Can anyone help me on this? I've tried so many ways found here but I couldn't customize anyone to my case.
Thanks in advance.
<?php
$result_rowset = array();
$i = 0 ;
while( $row = mysqli_fetch_array($result)) {
$result_rowset['Nome'][$i] = $row['nome'];
$result_rowset['Campo 01'][$i] = $row['campo1'];
$result_rowset['Campo 02'][$i] = $row['campo2'];
$result_rowset['Campo 03'][$i] = $row['campo3'];
$result_rowset['Campo 04'][$i] = $row['campo4'];
$result_rowset['Campo 05'][$i] = $row['campo5'];
$i ++ ;
}
$result->data_seek(0);
?>
<section class="feature-area section-gap" style="padding-top: 10px;">
<div class="container">
<div class="row d-flex justify-content-center" style="max-width: 1240px;">
<div class="mytable table-responsive">
<table class="table table-striped table-bordered table-hover">
<tbody>
<?php foreach ( $result_rowset as $result_key => $result_value ) { ?>
<tr>
<td><?php echo $result_key ; ?></td>
<?php foreach ( $result_value as $val ) { ?>
<td><?php echo $val ; ?></td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</section>
First u need still save the column ativo when you fetch array
<?php
$result_rowset = array();
$i = 0 ;
while( $row = mysqli_fetch_array($result)) {
$result_rowset['Nome'][$i] = $row['nome'];
$result_rowset['ativo'][$i] = $row['ativo'];
$i ++ ;
}
$result->data_seek(0);
?>
and when you foreach the array you can add condition to make background color changed depends on column ativo
<table class="table table-striped table-bordered table-hover">
<tbody>
<?php foreach ( $result_rowset as $r ) { ?>
<tr>
<?php if($r->ativo==0){ ?>
<td style="background-color:red">
<?php }elseif($r->ativo==1){ ?>
<td style="background-color:blue">
<?php }else{ ?>
<td>
<?php } ?>
<?php echo $r->nome; ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
Related
My problem is that I call my function within my for, but only a part changes color and not above each record, the buttons work but the color of the corresponding row does not change, it had occurred to me to send a value that could be the pk of my table and this one that is changing in the for, if you know any way to achieve that I would really appreciate it
<!doctype html>
<html lang="es">
<head>
<body>
<?php
if (isset($_POST['submit']) && !hash_equals($_SESSION['csrf'], $_POST['csrf'])) {
die();
}
$error = false;
$config = include 'ClientesBeta/conexion/config.php';
try {
$dsn = 'mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['name'];
$conexion = new PDO($dsn, $config['db']['user'], $config['db']['pass'], $config['db']['options']);
if (isset($_POST['marca'])) {
$consultaSQL = "SELECT * FROM zonas WHERE idZonas <> 1 and nombreZona LIKE '%" . $_POST['marca'] . "%'";
} else {
$consultaSQL = "SELECT * FROM zonas where idZonas <> 1";
}
$sentencia = $conexion->prepare($consultaSQL);
$sentencia->execute();
$alumnos = $sentencia->fetchAll();
} catch(PDOException $error) {
$error= $error->getMessage();
}
?>
<table class="table table-bordered table-striped" style="margin-top:20px;">
<thead>
<th>ID</th>
<th>Zona</th>
<th>Acción</th>
</thead>
<tbody>
<?php
if ($alumnos && $sentencia) {
foreach ($alumnos as $row) {
?>
<tr>
<td id="prueba2"></td>
<td id="prueba"></td>
<td id="prueba1"></td>
<td >
<button type="button" onclick="cambiarColor2();">Inicio</button>
<button type="button" onclick="cambiarColor();">Proceso</button>
<button type="button" onclick="cambiarColor1();">Fin</button>
</td>
<script>
function cambiarColor(){
document.getElementById('prueba').style.backgroundColor = "blue";
}
</script>
<script>
function cambiarColor1(){
document.getElementById('prueba1').style.backgroundColor = "red";
}
</script>
<script>
function cambiarColor2(){
document.getElementById('prueba2').style.backgroundColor = "green";
}
</script>
</tr>
<tr>
<td><?php echo $row['idZonas']; ?></td>
<td><?php echo $row['nombreZona']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
It is difficult to understand your question. But I believe you are looking for the below answer. Let me know if it helps you.
I think you have placed the ID in the wrong tags, though. Changed the id to where you want to change the colour. I haven't tested the code also.
<?php
if ($alumnos && $sentencia) {
foreach ($alumnos as $key => $row) {?>
<tr>
<td id="prueba2_<?php echo $key; ?>"></td>
<td id="prueba0_<?php echo $key; ?>"></td>
<td id="prueba1_<?php echo $key; ?>"></td>
<td>
<button type="button" onclick="cambiarColor(<?php echo $key; ?>, 2, 'green');">Inicio</button>
<button type="button" onclick="cambiarColor(<?php echo $key; ?>, 0, 'blue');">Proceso</button>
<button type="button" onclick="cambiarColor(<?php echo $key; ?>, 1, 'red');">Fin</button>
</td>
</tr>
<tr>
<td><?php echo $row['idZonas']; ?></td>
<td><?php echo $row['nombreZona']; ?></td>
<td> </td>
</tr>
<?php
}
}
?>
<script>
function cambiarColor(id, eleid color){
document.getElementById('prueba'+eleid+'_'+id).style.backgroundColor = color;
}
</script>
I stored some values in HTML table to MySQL database. It successfully stored in the database, but when I fetched the data using Json decode(), the Html table nothing shows any data in the web page, but nothing error occurred.
Here is the php code:
<table class="table table-bordered mb-0">
<thead>
<tr>
<th>Medicine Name</th>
<th>Morning</th>
<th>Noon</th>
<th>Noght</th>
</tr>
</thead>
<tbody>
<?php
require_once 'auth/dbconnection.php';
$sql = "SELECT * FROM prescription";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
$medRecords = json_decode($row['med_records'],true);
if (is_array($medRecords) || is_object($medRecords)) {
foreach($medRecords as $key => $object) {
?> <tr>
<td><?php echo $object->medname ?></td>
<td><?php echo $object->morning ?></td>
<td><?php echo $object->noon ?></td>
<td><?php echo $object->night ?></td>
</tr>
<?php
} }}}
?>
</tbody>
</table>
Here is my database table
I don't know where I went wrong. How to improve the code segments?
Notice that you missing the fetch itself: $result->fetch_assoc()
You using the var $row but from where?
I think you code should be as:
$sql = "SELECT * FROM prescription";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
while ($row = $result->fetch_assoc()) { <---- Notice this line
$medRecords = json_decode($row['med_records'],true);
if (is_array($medRecords) || is_object($medRecords)) {
foreach($medRecords as $key => $object) {
...
i am loading a page for my pending orders in view and displaying the data in a table i am calculating line total through javascript and it is going well but when i try to show in the page there is error in getting the element by its id and is the error is: Cannot set property 'innerHTML' of null
<div class="ziehharmonika">
<?php if( count($pending_orders) ): ?>
<?php foreach ($pending_orders as $info): ?>
<h3 id="header3" style="background-color: #00bad3; color:white;"
onclick="get_line_total(<?php print_r($info['id']);?>);"><span
style="color: black;">order# </span> <?php print_r($info['id']);?>
Status: </span> <span id="status<?php print_r($info['id']);?>"><?php
print_r($info['status']);?></span></h3>
<div class="w3_agile_recipe-grid">
<section>
<table cellpadding="0" cellspacing="0" class="ordertable"
id="ordertable<?php print_r($info['id']);?>">
<tr>
<th>Sr_No</th> <!-- Dummy cell for the row number and row commands
<th>Id</th>
<th>item_Name</th>
<th>price</th>
<th>company</th>
<th>quantity</th>
<th>line_total</th>
</tr>
<?php if( count($info['porders']) ): ?>
<?php
$row_count = 1;
foreach ($info['porders']as $order_info){ ?>
<tr>
<td><?php echo $row_count; ?></td> <!-- Don't remove this column
as it's needed for the row commands -->
<td class="ord_id"><?php print_r($order_info['id']);?></td>
<td><?php print_r($order_info['name']);?></td>
<td class="ord_price"><?php print_r($order_info['price']);?></td>
<td><?php print_r($order_info['company']);?></td>
<td class="ord_qty"><?php print_r($order_info['quantity']);?></td>
<td class="line_total"></td>
</tr>
<?php $row_count++; }?>
</table>
<?php else: ?>
<?php endif;?>
<div><span>Total Amount</span><p id="tota<?php
print_r($order_info['id']);?>"></p></div>
<div class="clearfix"></div>
</section>
</div>
</div>
my javascript is;
var get_line_total = function(id){
var table = document.getElementById('ordertable'+id);
ord_price=0;
ord_qty=0;
linetotal=0;
for (var r = 1, n = table.rows.length; r < n; r++) {
ord_price = parseInt(table.rows[r].cells[3].innerHTML);
ord_qty = parseInt(table.rows[r].cells[5].innerHTML);
linetotal= ord_price * ord_qty;
table.rows[r].cells[6].innerHTML = linetotal;
}
ord_total=0;
for (var r = 1, n = table.rows.length; r < n; r++) {
ord_total = ord_total + parseInt(table.rows[r].cells[6].innerHTML);
}
// this line is giving above said error
document.getElementById('tota'+id).innerHTML = ord_total;
}
can any body help me to correct this code and get rid of the error
I'm using Session with PHP. And I'm not getting display horizontally. How do I do that?
PHP:
if(isset($_GET['nome']) && $_GET!=['nome']){
$lista_tarefa = array();
$lista_tarefa['nome'] = $_GET['nome'];
}
if(isset($_GET['descricao'])){
$lista_tarefa['descricao'] = $_GET['descricao'];
} else {
$lista_tarefa['descricao'] = '';
}
PHP+HTML:
<table>
<?php foreach ($lista_tarefa as $tarefa) : ?>
<tr>
<td> <?php echo $tarefa; ?> </td>
</tr>
<?php endforeach; ?>
</table>
I GUESS...
$a=0;
while($a<=3){
foreach ($lista_tarefas as $tarefa):
echo $tarefa[a];
if($a<=3){
????
}
$a++;
}
but does not work. Could you help me?
How do I find the right approach to getting this comparison to work? I've tried all kinds of approaches. I even used ids, but they don't respond. If I do this "gumboots" string check though, it does work. "gumboots" was just a value for a product name that existed somewhere on the table. This is how I know I do not need PHP at all for this, despite the tables displayed in PHP in the Index view below. Any idea? I would appreciate it.
Here's the javascript
$('#example tbody tr td').each(function()
{
//var p_no_in_stock = parseInt($('#p_no_in_stock')).val();
//var p_reorder_quantity = parseInt($('#p_reorder_quantity')).val();
var p_no_in_stock = parseInt(document.getElementById('p_no_in_stock')).value;
var p_reorder_quantity = parseInt(document.getElementById('p_reorder_quantity')).value;
//if ($product['Product']['p_no_in_stock'].val() < $product['Product']['p_reorder_quantity'].val())
if ($(this).text() == "gumboots")
//if ($(this).p_no_in_stock < $(this).p_reorder_quantity)
{
//$("#row_" +" td").effect("highlight", {}, 1500);
$(this).closest('tr').attr('style','background-color:red');
$(this).parent().css('background-color','red');
$(this).parent().attr('style','background-color:red');
$(this).parent().addClass('highlight');
$(this).parent().css('font-weight','bold');
}
});
And this is the application in a View called Products.index
<div class="active">
<h2><?php echo __('Products'); ?></h2>
<table cellpadding="0" cellspacing="0" class="table table-striped table-bordered" id ="example">
<tr>
<th><?php echo $this->Paginator->sort('p_name', 'Name'); ?></th>
<th><?php echo $this->Paginator->sort('category_name', 'Category'); ?></th>
<th><?php echo $this->Paginator->sort('p_no_in_stock','No. in Stock'); ?></th>
<th><?php echo $this->Paginator->sort('p_reorder_quantity', 'Re-order Quantity'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
<tbody>
<?php foreach ($products as $product): ?>
<tr>
<td><?php echo h($product['Product']['p_name']); ?></td>
<td> <?php echo $this->Html->link($product['Category']['category_name'],
array('controller' => 'categories', 'action' => 'view', $product['Category']['id'])); ?>
</td>
<td id = "p_no_in_stock" type ="number" ><?php echo h($product['Product']['p_no_in_stock']); ?> </td>
<td id ="p_reorder_quantity" type ="number" ><?php echo h($product['Product']['p_reorder_quantity']); ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $product['Product']['id']), array('class' => 'btn btn-mini')); ?>
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $product['Product']['id']), array('class' => 'btn btn-mini')); ?>
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $product['Product']['id']), array('class' => 'btn btn-mini'), __('Are you sure you want to delete # %s?', $product['Product']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Thanks in advance.
Is this what you're asking?
http://jsfiddle.net/SinisterSystems/z9SE7/1/
HTML:
<table>
<tr>
<td>Test</td>
<td>Test</td>
<td>Test</td>
<td>Test</td>
<td>Test</td>
</tr>
<tr>
<td>Test</td>
<td>Test</td>
<td>Test</td>
<td>Test</td>
<td>Test</td>
</tr>
</table>
CSS:
tr:hover td {
background:#F00;
}
I am sending you mine code, please modify it accordingly....
The PHP Code is as -
<?php
if($num>0)
{
echo '<table width="100%" id="dep_table" style="margin-top:10px;" cellspacing="1" cellpadding="2" border="0">';
echo '<tr bgcolor="#4682B4">';
echo '<th>Editor</th>';
echo '<th>Department Id</th>';
echo '<th>Department Name</th>';
echo '</tr>';
$i=0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$i++;
if($i % 2 == 0)
{
$bgcolor= "#6AA2C3";
}
else
{
$bgcolor= "#A2B5CD";
}
//extract row, this will make $row['firstname'] to just $firstname only
extract($row);
//creating new table row per record
echo "<tr bgcolor='$bgcolor' id='$DeptId' name='edit_tr'>";
echo '<td id="edit"><input id="edit" type="radio" name="deptid" value="DeptId" ?></td>';
echo "<td class='format'>{$row['DeptId']}</td>";
echo "<td class='format'>{$row['DeptName']}</td>";
echo "</tr>";
}
echo "</table>";
}
echo "</div>";
The JS for corresponding code is as -
$(document).ready(function() {
row_color();
$('#dep_table tr').click(function(e) {
$(this).find('td input:radio').prop('checked', true);
/* submit_fxn();
$('#form_ndesg').submit(function(e) {
return false;
});*/
});
});
//******* 1 Div Fade In/Out effect *******
function row_color(){
$('#dep_table tr').not(':first').hover(function(){
$(this).addClass('hover');
},function(){
$(this).removeClass('hover');
});
};
The corresponding CSS code is as -
tr.hover{
background-color:#E7ECB8;
color:#990000;
}
You will move your mouse on the table, it will change the row color as well as rwo text color and when you click on particular row, it will enable the row by selecting radio button..
If this answer is helpful for you then please like it for answer, so that others can use it for reference.... Thanks and best of luck