same class div sum , display in div and read $variable on php - php

I want to sum all numbers in the same div class (class is dinamic variable called $events[2]) and display it on my class div result on the loop but i cant make javascript read the variable $('$events[2]') on the php
<script>
var sum = 0;
$('$events[2]').text(function(i, v) {
sum += parseFloat(v.replace(',', ''));
});
$('.result').text('Result : ' + sum);
</script>
<?php
echo "<td class='tb1'>{$events[0]}</td></div>"; //foto
echo "<td>";
echo "<div class='w_promotora_images'>{$events[7]}</td></div>"; //foto
echo "<td>";
echo "<div class='w_promotora_images'>{$events[3]}</td></div>"; //data
echo "<td class=''>{$events[2]}</td>"; //nome
echo "<td class=' tb1'>{$events[4]}</td>"; //evento
echo "<td class=' tb1'>{$events[5]}</td>"; //horario
echo "<td class=' tb1'>{$events[6]}</td>"; //obs
echo "<td class=' tb1'>{$events[10]}</td>"; //horas
echo "<td class=' tb1'>{$events[9]}</td>"; //valor hora
echo "<td class=' tb1'>{$events[1]}</td>"; //Props
echo "<td ><div class='{$events[2]}'> {$events[8]}</td></div>"; //t parcial
echo "<td class=''><div class='result'> </div> </td></tr>";
}?>
</table>

If your variable is in php, javascript will not be able to parse it. You must output the var in php :
$('.<?= $events[2] ?>').text(function(i, v) {
sum += parseFloat(v.replace(',', ''));
});
I also add the "." for if you don't add it, jquery will not know that you are fetching an element by its class name.

Related

PHP - How to change font size of HTML table?

I'm generating table in a foreach loop
echo "<tr>";
echo "<td align='left' bgcolor='$bgclr'>".$monthName."</td>";
echo "<td align='center' bgcolor='$bgclr'>".$id."</td>";
echo "<td align='center' bgcolor='$bgclr'>".$total."</td>"
echo "</tr>";
I need to increase font size of the table, but i'm not sure how. Does the html table has attribute to set font size or I should use CSS ?
Just use font style css in style attribute :
echo "<tr>";
echo "<td align='left' bgcolor='$bgclr' style='font-size:10px'>".$monthName."</td>";
echo "<td align='center' bgcolor='$bgclr' style='font-size:10px'>".$id."</td>";
echo "<td align='center' bgcolor='$bgclr' style='font-size:10px'>".$total."</td>"
echo "</tr>";
use the html styling on the echoed elements. For example echo '<table style="font-size: 100px">';
You need to define font size in one variable and then use in inline as below code:
foreach{
$fontsize = 'font-size: '.((isset(FONT_SIZE_IN_FOREACH))?FONT_SIZE_IN_FOREACH:12);
echo "<tr>";
echo "<td align='left' bgcolor='$bgclr' style='$fontsize'>".$monthName."</td>";
echo "<td align='center' bgcolor='$bgclr'>".$id."</td>";
echo "<td align='center' bgcolor='$bgclr'>".$total."</td>"
echo "</tr>";
}
You can add class to the table and write css for that class.
HTML:
<table class='fontsize'>
</table>
CSS:
.fontsize {
font-size:16px;
}

Check if variable repeats and add change table column color in loop

I have this foreach loop getting my table values... I now want to check if the the column pid row numbers repeats it self $events[12] and if so I want to add a class to the table column <tr> so I can replace the color of all repeating columns so they look liked group together. Here's my page : http://www.toppromomkt.com/?wallet=dezembro-2015
And the code:
//now loop over data instead of mysql_fetch_array
foreach ($data as $events) {
echo '<table id="wallet_table1" width="900px" border="0" cellspacing="2" cellpadding="0">';
?>
<tr id='<?php echo "row".$events[0] ?>' class='<?php echo $events[2] ?>' onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
<?php
echo "<td class='tb1'>{$events[0]}</td></div>"; //foto
echo "<td>";
echo "<div class='w_promotora_images'>{$events[7]}</td></div>"; //foto
echo "<td>";
echo "<div class='w_promotora_images'>{$events[3]}</td></div>"; //data
echo "<td class=''>{$events[2]}</td>"; //nome
echo "<td class='sle'>{$events[12]}</td>"; // this the PID i want to check if repeats
echo "<td class=' tb1'>{$events[4]}</td>"; //evento
echo "<td class=' tb1'>{$events[5]}</td>"; //horario
echo "<td class=' tb1'>{$events[6]}</td>"; //obs
echo "<td class=' tb1'>{$events[10]}h</td>"; //horas
echo "<td class=' tb1'>{$events[9]}€</td>"; //valor hora
echo "<td class=' tb1'>{$events[1]}</td>"; //Props
echo "<td class=' tb1'><div class='{$events[2]}'> {$events[8]}</td></div>"; //t parcial ?>
<td class='tbrr'> <?php
$strSQL1 = "SELECT nome, SUM(totalparcial) as Soma FROM bruno_wallet where nome='{$events[2]}' GROUP BY nome";
$rs1 = mysql_query($strSQL1);
$sum1 = 0;
$data1 = array();
while($row2 = mysql_fetch_array($rs1)) {
echo $row2[1]."€";
};
?>
</div> </td></tr>
</table>
What you can do is something like this: Store all PIDs in an array and check every time you loop through the for loop if the current PID is in the array. For example:
$checkedPIDs = array();
foreach ($data as $events) {
$currentPID = $events[12];
if (in_array($currentPID, $checkedPIDs)) {
// Output with different colour...
} else {
array_push($checkedPIDs, $currentPID);
// Output with normal colour...
}
// The rest of your code...
}

PHP script not passing url with a array variable

This code is not passing URL to make topic clickable main problem is in third line after while loop. I got this error :
Parse error: syntax error, unexpected 'id' (T_STRING), expecting ',' or ';' in D:\xmapp\htdocs\forum\main_forum.php on line 37
Code :
while($rows=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' bgcolor=#FFFFFF>",$rows['id'],"</td>";
echo "<td bgcolor='#FFFFFF'>",'$rows['topic']. ',"</td>";
echo "<td align='center' bgcolor='#FFFFFF'>",$rows['view'],"</td>";
echo "<td align='center' bgcolor='#FFFFFF'>",$rows['reply'],"</td>";
echo "<td align='center' bgcolor='#FFFFFF'>",$rows['datetime'],"</td>";
}
Please try this. there is syntax error in your code.
echo "<tr>";
echo "<td align='center' bgcolor=#FFFFFF>",$rows['id'],"</td>";
echo "<td bgcolor='#FFFFFF'>",''.$rows[topic]. ' ',"</td>";
echo "<td align='center' bgcolor='#FFFFFF'>",$rows['view'],"</td>";
echo "<td align='center' bgcolor='#FFFFFF'>",$rows['reply'],"</td>";
echo "<td align='center' bgcolor='#FFFFFF'>",$rows['datetime'],"</td>";
This should correct your problem, Use
echo "<td bgcolor='#FFFFFF'><a href='".$view_topic.".php?id=".$rows['id']."'>".$rows['topic']."</a></td>";
instead of
echo "<td bgcolor='#FFFFFF'>",'$rows['topic']. ',"</td>";
One more thing, Why dont you use php tag only when needed. If you do like this it will be clean and easy.
<?php
while($rows=mysql_fetch_array($result))
{
?>
<tr>
<td> <?php $rows['topic']; ?> </td>
.......
.......
</tr>
<?php } ?>
Try this :
while($rows = mysql_fetch_array($result)){
echo "<tr>";
echo "<td align='center' bgcolor=#FFFFFF>".$rows['id']."</td>";
echo "<td bgcolor='#FFFFFF'><a href='".$view_topic.".php?id=".$rows['id']."'>".$rows['topic']."</a></td>";
echo "<td align='center' bgcolor='#FFFFFF'>".$rows['view']."</td>";
echo "<td align='center' bgcolor='#FFFFFF'>".$rows['reply']."</td>";
echo "<td align='center' bgcolor='#FFFFFF'>".$rows['datetime']."</td>";
}

jquery percentage calcutaion from the textbox values

i have a form in which i want to take out a percentage on a amount for eg. let the amount be 100 and i will enter 10 so the desired output is 10 and i even want to add the percentage's amount with the amount so the output will be 110 (100 + 10) i am able to take out the percentage but its not being added to the amount and these fields will be the dynamically generated inputs.
Here is my code:
$(document).ready(function(){
$('#detail').on('keyup', '.rent, .stperc, .st, .stamt, .cal', calculateRow);
function calculateRow() {
var $row = $(this).closest('tr');
var value = parseFloat($row.find('.stperc').val());
var value2 = parseFloat($row.find('.rent').val());
var stamt = parseFloat($row.find('.stamt').val((value * value2) / 100));
var cost = stamt + value2;
console.log(cost);
if (isNaN(cost)) {
$row.find('.cal').val("0");
} else {
$row.find('.cal').val(cost);
}
}
});
Here is my php code which will generate the textboxes:
while ($row = mysql_fetch_object($query)){
echo "<tr>";
echo "<td align='center'>";
echo "<input type='text' class='form-input-rate' name=\"locno_$ctr\" value=\"$row->locno\" $stylen readonly>";
echo "</td>";
echo "<td align='center'>";
echo "<input type='text' class='form-input-rate' name=\"ledger_$ctr\" value=\"$row->custcode\" $stylen readonly>";
echo "</td>";
echo "<td align='center'>";
echo "<input type='text' class='form-input-rate' name=\"name_$ctr\" value=\"$row->name\" $stylev readonly>";
echo "</td>";
echo "<td align='center'>";
echo "<input type='text' class='form-input-rate' name=\"deposit_$ctr\" value=\"$row->deposit\" $stylen >";
echo "</td>";
echo "<td align='center'>";
echo "<input type='text' class='rent form-input-rate' name=\"rent_$ctr\" value=\"$row->rent\" $stylen >";
echo "</td>";
echo "<td align='center'>";
echo "<input type='text' class='stperc form-input-rate' name=\"stperc_$ctr\" value=\"$row->stperc\" $stylen >";
echo "</td>";
echo "<td align='center'>";
echo "<input type='text' class='stamt form-input-rate' name=\"st_$ctr\" value=\"$row->stamt\" $stylen >";
echo "</td>";
echo "<td align='center'>";
echo "<input type='text' class='cal form-input-rate' name=\"total_$ctr\" value=\"$row->totamt\" $stylen readonly>";
echo "<input type='hidden' name=\"accode_$ctr\" value=\"$row->accode\">";
echo "</td>";
echo "</tr>";
$ctr++;
}
try something like this FIDDLE
$(document).ready(function(){
$('#detail').on('keyup', '.rent, .stperc, .st, .stamt, .cal', function(){
var $row = $(this).closest('tr');
var value = parseFloat($row.find('.stperc').val());
var value2 = parseFloat($row.find('.rent').val());
var stamt = (value * value2) / 100;
$row.find('.stamt').val(stamt);
var cost = stamt + value2;
if (isNaN(cost)) {
$row.find('.cal').val("rasd");
} else {
$row.find('.cal').val(cost);
}
});
});

Freezing Table columns and rows using only one table

I have a dynamic table which is filled from an Application.
My problem is that I need some way to freeze the first row and the first four columns BUT using only one table, obviously because I will be posting the table back again to the application.
My Current Table Code :
....
<form name="myform" method="post" action="test123.php" onsubmit="set_value();">
//this form is used to post back the data after user updates it.
<br />
<table align="center" id="dataTable">
<thead>
<tr class="header">
...
$arratt49 = (explode(',',$response49,-1)); //sending an array from application
$dayscounter = sizeof($arratt49);
$sizemon = strlen($month1);
if($sizemon > 0)
{
echo "<th class='text ce6'>No.</th>";
echo "<th class='text ce6'>Emp code</th>";
echo "<th class='text ce6'>Name</th>";
echo "<th class='text ce6'>Department </th>";
for($kkk=0;$kkk<$dayscounter;$kkk++)
{
echo "<th class='text ce7'>";
echo $arratt49[$kkk];
echo "</br>";
echo $arratt49[$kkk+1];
echo "</th>";
$kkk = $kkk +1;
}
}
</tr>
</thead>
<tbody>
<tr>
....//Here goes the Body
for($k=0;$k<=$arrcnt;$k++)
{
echo "<td class='text ce2'>";
echo $counter;
echo "</td>";
echo "<td class='text ce2'><INPUT type='text' name='code[]' size = '8' readonly='true 'value='";
echo $arratt[$k];
echo "'";
echo "</td>";
echo "<td class='text ce2'><INPUT type='text' name='name[]' size = '40' readonly='true' value='";
echo $arratt[$k+1];
echo "'";
echo "</td>";
echo "<td class='text ce2'><INPUT type='text' name='dept[]' size = '12' readonly='true 'value='";
echo $arratt[$k+2];
echo "'>";
echo "</td>";
Already tried :
Big Four method
and also This
Any Good Ideas?
Again I CANNOT use more than one table method.
You can use datatables plugin FixedColumns, just set parameter "iLeftColumns" to the number of columns needed to be fixed from the left side of table. Header already fixed by default.

Categories