How to display content in horizontal? - javascript

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?

Related

Can you guide me with a question, I am trying to change the color every time I press a button

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>

Change cell background depending on mysql field value

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>

change <td> tag to editable and submit form after editing cakephp 2

I am working in a cakephp 2 project. On my view I have a table on which I want to make a cell editable. After editing I want want to save it.
I have tried contentEditable of HTML 5. By that I can edit but data is not passed to controller. I also tried innerHTML which change the cell to input but I can't write anything on the input field. How can I solve the problem? Thanks
<script>
function makeInput(e) {
document.getElementById('test').innerHTML= "<input type='text'>"
}
function formSubmit2(){
document.getElementById('StockReportEditForm').submit();
}
</script>
<?php if(!empty($results)): ?>
<?php echo $this->Form->create('StockReport',array('url'=>array('type'=>'post','action'=>'edit')));?>
<table>
<tr>
<th>Year Week</th>
<th>Counter</th>
<th>Refrigerator</th>
<th>Freezer</th>
<th>Summary</th>
</tr>
<?php foreach ($results as $result): ?>
<?php $today = date("Y-m-d", strtotime("+1 week"));
$date = new DateTime($today);
$week = $date->format("W");
$week_int = (int)$week;
$week_int=$week_int-1;
$last_week = (string)$week_int;
$year = date("Y"); ?>
<tr>
<?php if($year.$last_week==$result['StockReport']['yearweek']): ?>
<td onclick="makeInput(this)" id="test">
<?php echo $result['StockReport']['yearweek']; ?>
<?php else:?>
<td><?php echo $result['StockReport']['yearweek'];?></td>
<?php endif ?>
</td>
<td><?php echo $result['StockReport']['counter']; ?></td>
<td><?php echo $result['StockReport']['refrigerator']; ?></td>
<td><?php echo $result['StockReport']['freezer']; ?></td>
<td><?php echo $result['StockReport']['summary']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
// echo $this->Form->submit('Submit');
$options = array(
'label' => 'Submit',
'div' => array(
'class' => 'glass-pill',
),
'id'=>'submitButton',
'onclick'=>'formSubmit2()'
);
echo $this->Form->end($options); ?>
<?php endif; ?>
I would create a hidden input within the td element and when the form is submitted I copy the htmlinner of the contenteditabe to the hidden input value.

How to use PHP loop variable in javascript?

I have run an SQL statement to get all the records I need to show in a HTML table.
I have then run a while loop to display the records from the database. (The code for this is below.)
<table class="projects-table">
<tr>
<th>Complete?</th>
<th>Paid?</th>
<th>Project Name</th>
<th>£ / hr</th>
<th>End Date</th>
<th>Hours Logged</th>
<th><i class="fa fa-trash"></i></th>
</tr>
<?php
$select_id_jobs = mysqli_query($mysqli, "SELECT id FROM users WHERE username='$login_user'");
while($row = mysqli_fetch_array($select_id_jobs)) {
$id_jobs = $row['id'];
}
$select_jobs_with_usrid = mysqli_query($mysqli, "SELECT * FROM jobs WHERE username_id = '$id_jobs';");
while($row = mysqli_fetch_array($select_jobs_with_usrid)) {
?>
<tr id="<?php echo $rowId; ?>">
<td>
<!-- Complete Checkbox -->
<input type="checkbox" id="<?php echo $completeCheck;?>" onclick="compTask();">
</td>
<td>
<!-- Paid checkbox -->
<input type="checkbox" onclick="paidTask()">
</td>
<td>
<?php echo $row['project_title']; ?>
</td>
<td>
<?php echo $row['cost_hour']; ?>
</td>
<td>
<?php echo $row['completion_date']; ?>
</td>
<td>
<?php echo $row['time_spent']; ?>
</td>
<td>
<div class="delete-btn"><a onclick="deleteTask()">DELETE</a></div>
</td>
</tr>
<?php } ?>
</table>
As you can see from the checkbox for completing a task. What I want to do is use javascript so that when the checkbox is checked the text from the other records turns green.
I have included the javascript I am trying to use below. I don't know why but I can't access the inputs ID in order to change the css.
<script>
function compTask() {
if (document.getElementById("<?php echo 'complete-' . $row['id'] ?>").checked == true) {
document.getElementById("<?php echo 'tr' . $row['id']; ?>").style.color = "green";
alert("hello");
} else {
document.getElementById("<?php echo 'tr' . $row['id']; ?>").style.color = "black";
}
}
Okay easy way to do that is to print id as parameter in js function
something like that:
<input type="checkbox" id="<?php echo $completeCheck;?>"
onclick="compTask( '<?php echo $row['id'];?>' );">
and in js function deal with id from parameter:
function compTask(id) {
if (document.getElementById('complete-' + id).checked == true) {
document.getElementById('tr' + id).style.color = "green";
alert("hello");
}
}
Hy,
You need to add id in onclick="deleteTask('<?php echo $row['id']; ?>')">
Now in you function have id:
function deleteTask(id) { console.log(id) }

Cart is updated only once using Jquery and Codeigniter

I am working on Updating Codeigntier Cart with Jquery using Ajax call to update. Here is my jquery function
$(function() {
$('.cart_form select').on('change', function(ev) {
var rowid = $(this).attr('class');
var qty = $(this).val();
var postData_updatecart = {
'rowid' : rowid,
'qty' : qty
};
//posting data to update cart
$.ajax({
url : base_url + 'bookings/update_booking_cart',
type : 'post',
data : postData_updatecart,
beforeSend : function() {
$('#cart_content').html('Updating...');
},
success : function(html) {
//window.location.href = "postproperty.php?type="+suffix+'&page=page1';
$('#cart_content').html(html);
}
});
});
});
I have cart view file as
<div class="cart_form">
<?php echo form_open(base_url().'index.php/bookings/customerdetails', array('class' => 'bookings_form_customer', 'id' => 'bookings_form_customer')); ?>
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
<th style="text-align:left">Item Description</th>
<th style="text-align:left">QTY</th>
<th style="text-align:right">Item Price</th>
<th style="text-align:right">Sub-Total</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items): ?>
<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
<tr>
<td>
<?php echo $items['name']; ?>
<?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
<p>
<?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
<strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
<?php endforeach; ?>
</p>
<?php endif; ?>
</td>
<td>
<select name="<?php echo $i.'[qty]'; ?>" class="<?php echo $items['rowid']; ?>">
<?php
for($tt=0; $tt<=10; $tt++)
{
?>
<option value="<?php echo $tt; ?>" <?php if($tt==$items['qty']) echo 'selected="selected"'; ?>><?php echo $tt; ?></option>
<?php
}
?>
</select>
</td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
<td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td colspan="2"> </td>
<td class="right"><strong>Total</strong></td>
<td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<p><?php echo form_submit('update_cart', 'Update your Cart'); ?></p>
</form>
</div>
Things are working Nice. But, lets say if I have 2 products, if i select one item's qty from 1 to 2, cart is updated and updated cart is shown using AJAX. But, if I immediately change another item's qty then it doesnt work.
The cart view file is inside class 'cart_form'.
Helping hands are appreciated.
I got an answer. You can refer below link
Ajax Request works only once
OR
Directly follow this
REPLACE jquery code part by
$(function() {
$(document).on( "change", "#bookings_form_customer select", function(ev) {
// your code goes here
});
});

Categories