I have a problem for my program.
This my script:
$x=1;
$no=1;
$tot=0;
$bobot=0;
$jumlah=4;
for($i=1;$i<=$jumlah;$i++) {
<tr>
<th> echo $no++; </th>
for($a=$i;$a<=$jumlah;$a++) {
echo "<td>".$x/$a."</td>";
}
<td>Count</td>
</tr>
}
and this is my result
result page
I want to replace the words "count" with the sum of each column in the same row. and my question, how I can add all column in one rows.
example : row 1 = 1 + 0.5 + 0,33 + 0.25 = 2,08
Thank you for helping, I am sorry if my English so bad.
Try something like this
initialize $count as zero before the inner loop
and in inner loop increment county
<?php
$x=1;
$no=1;
$tot=0;
$bobot=0;
$jumlah=4;
for($i=1;$i<=$jumlah;$i++) {
?>
<tr>
<th> <?php echo $no++;?> </th>
<?php
$count=0;
for($a=$i;$a<=$jumlah;$a++) {
echo "<td>".$x/$a."</td>";
$count=$count+($x/$a);
}
?>
<td><?=$count;?></td>
</tr>
<?php
}
?>
Related
Currently I have a table and is currently coded as:
$min_hor = $min_ver = 1;
$max_hor = $max_ver = 3;
?>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<style>
.table{
margin: 0 auto;
}
</style>
<div class="text-center">
<table class="table" style="width:70%">
<thead>
<tr>
<th></th>
<?php for($j=$min_hor; $j<=$max_hor; $j++){ ?>
<th><?php echo str_pad($j, 2, "0", STR_PAD_LEFT); ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php for($i=$min_ver;$i<=$max_ver;$i++){ ?>
<tr>
<th><?php echo $i; ?></th>
<?php for($j=$min_hor; $j<=$max_hor; $j++){ ?>
<td><?php echo $i.'-'.str_pad($j, 2, "0", STR_PAD_LEFT) ?></td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
</div>
This display table as shown in attached screenshot.
Now, I have one additional array as $letter_array = array('A','B'); This array is could be bigger than this one and could be upto D,E,F,...
Now, I need to include this A,B into the table header something like as shown in screenshot.
This is where I became helpless. I couldn't find how could I loop this. Although, to make things easier, I have included codepen link of html code.
Codepen
How about we keep headers in 2 different Arrays and then perform a loop.following is the pseudocode which i think will work.
string a[16]={'01','02','03',......,'B'};
string b[17]={'1','2','3',....,'B'};
<table class="table" style="width:70%">
<thead>
<tr>
<th></th>
foreach(var item in a)
{
<th>{{item}}</th>
}
</tr>
</thead>
<tbody>
foreach(var item2 in b)
{
<tr>
<td>{{item2}}</td>
foreach(var item in a)
{
<td>{{item2}}_{{item}}</td>
}
</tr>
}
</tbody>
</table>
Now, before generating table we need elements at array A and B. to generate the array i.e a and b we can do following :
int min_hor=1;
int max_hor=3;
var a_output=[];
var a_inputLetter=['A','B'];
//first for number only.here let us create a string and push to a_output.
for(int i=min_hor,i<=max_hor;i++)
{
a_output.Add('0'+i);
}
//second let us use Number Letter Combination
foreach(var item in a_inputLetter)
{
for(int i=min_hor;i<=max_hor;i++)
{
a_output.Add('0'+{{i}}_{{item}});
}
}
//third for letter number combination
foreach(var item in a_inputLetter)
{
for(int i=min_hor;i<=max_hor;i++)
{
a_output.Add({{item}}_{{i}});
}
}
//and finally for letter_only
foreach(var item in a_inputLetter)
{
a_output.Add({{item}});
}
//similarly do for Array B as well to generate the second array.
I need loop td inside while fetch.
td loop depends on rowCount variable from the first query.
<?php include'../db/dbConnect.php';
$_GET['tb'];
$t=$_GET['tb'];
$q=$con->prepare("desc $t");
$q->execute();
$h=$q->rowCount(); ?>
<table>
<tr>
<?php while($r=$q->fetch(PDO::FETCH_NUM)){ ?>
<th><?php echo $r[0];?></th>
<?php } ?>
</tr>
<?php
$q=$con->prepare("select*from $t");
$q->execute();
while($r=$q->fetch(PDO::FETCH_NUM)){
$d=$r[0];
?>
<tr>
<td><?php echo $d;?></td>
</tr>
<?php } ?>
</table>
Above code give result :
col1 col2 col3
12
13
14
But i need the result with :
col1 col2 col3
12 vala x
13 valb y
14 valc z
It must give td depends on rowCount variable.
I tried before to loop td by using while, but this i'm not get the logic.
I appreciate some logic or advice of the question.
You're fetching multiple rows:
$r=$q->fetch(PDO::FETCH_NUM)
But then only displaying the first column:
$d=$r[0];
<tr>
<td><?php echo $d;?></td>
</tr>
Try something like this:
while( $r=$q->fetch(PDO::FETCH_NUM) )
{
?>
<tr>
<?php
foreach( $r as $colIdx => $column )
{
?>
<td><?php echo $column;?></td>
<?php
}
<?
</tr>
<?php
}
NB. Not tested; but you get the idea.
My code is below
<?php
foreach($query2->result() as $row2)
{
$sub_head_id = $row2->sub_head_id;
?>
<tr>
<?php
$query3 = $this->db->select('farm_id')->get('spf_farm_info'); $she_am_total = 0;
foreach($query3->result() as $row3)
{
$farm_id_inner = $row3->farm_id;
?>
<td>
<?php
$s_h_e_amount = $this->report_model->get_amount($farm_id_inner,$head_id,$sub_head_id);
?>
</td>
<?php
}
?>
<td>
<?php
$total += $s_h_e_amount;
?>
</td>
</tr>
<?php
}
}
?>
Basically i want to know how can i solve it,
<tr>
<?php
$query3 = $this->db->select('farm_id')->get('spf_farm_info'); $she_am_total = 0;
foreach($query3->result() as $row3)
{
?>
<td>
<?php
echo "Total :";
echo $total;
?>
</td>
<?php
}
?>
<td>
Grand Total:
</td>
</tr>
Here, Table column will increase dynamically, and will get different value every td(may be 2/3/4/..) from get_amount function.
Now i want to sum every tr's value according to column.
here it will be sum dynamically which will be show.
Please help me how can i solve properly
Simple SOlution is Take Four Variables
$Cola_total=0;
$Colb_total=0;
$Colc_total=0;
$Cold_total=0;
in your loop sum these Columns
foreach($array as $val){
echo "<tr><td>$val[0]</td> <td>$val[1]</td> <td>$val[2]</td><td>$val[3]</td></tr>";
$Cola_total=+$val[0];
$Colb_total=+$val[1];
$Colc_total=+$val[2];
$Cold_total=+$val[3];
}
and after the loop put the sum in last row
echo "<tr><td>$Cola_total</td> <td>$Colb_total</td> <td>$Colc_total</td><td>$Cold_total</td></tr>";
it may give you some idea
Here , since using Codeigniter, it will be used that select_sum(). then will get proper result.
I have retrieved some values from database. In my view file I have the following:
$row['fee_amount'];
Now, I want to sum up all the values inside $row['fee_amount']; and then show it.
I know I could sum up when querying the database, but I am interested to learn how to add using PHP .
Would you please kindly teach me how to do it?
EDIT
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>S.N</th>
<th>Fee Type</th>
<th>Fee Amount</th>
</tr>
</thead>
<tbody>
<?php $i = 0; foreach ($records as $row){ $i++; ?>
<tr>
<td><?php echo $i; ?>.</td>
<td><?php echo $row['fee_type'];?></td>
<td><?php echo $row['fee_amount'];?></td>
</tr>
<?php } ?>
</tbody>
<tr>
<td></td>
<td>Total</td>
<td>
I WANT TO DISPLAY THE SUMMATION RESULT HERE ADDING UP VALUES INSIDE THIS>>> <? $row['fee_amount']; ?>
</td>
</tr>
</table>
<?php } ?>
In your view file, with your foreach loop, add a $sum variable next to your $i counter and add the amount per each iteration (similar to like you increase $i):
<?php
$i = 0;
$sum = 0;
foreach ($records as $row)
{
$i++;
$sum += $row['fee_amount']; ?>
(I put this over multiple lines to make it more readable).
After the foreach has finished, $sum contains the total amount:
<td>Total: <?php echo $sum; ?></td>
That simple it is. You only need a new variable ($sum) and do the calculation.
Use a loop
$sum = 0;
while($row...){
$sum += $row['fee_amount']
}
echo $sum;
You could use;
$someValue = 0;
foreach($row["fee_amount"] as $value) {
$someValue = $someValue + $value;
}
using this php function, if $row['fee_amount'] is an array ^_^
for example:
$a = array(2, 4, 6, 8);
array_sum($a)
I use mysql_fetch_array to fetch data from a mysql results set:
while($row = mysql_fetch_array($qry_result)){
Next I would like to place the data into columns in a table, but maximum of two columns at each row.
So using a table:
<table>
<tr>
<td>Record Here</td>
<td>Record Here</td>
</tr>
<tr>
<td>Record Here</td>
<td>Record Here</td>
</tr>
<tr>
<td colspan="2">Record Here</td>
</tr>
As you see above, I want to loop the results and create table columns in the loop.
This so that the records line up two and two on a results page.
Remember, if there is an odd number of records, then the last table column would need a colspan of 2, or perhaps just use an empty column?
Anybody know how to do this?
If I use a for loop inside the while loop, I would just be lining up the same records x times for each while loop. Confusing...
Any ideas?
Thanks
Implement a (1-indexed) counter within the while loop. Then add (after the loop):
if ($counter%2)
echo '<td></td>';
This will leave an additional blank cell in your table if the last column contains only one row.
Something like this should work...
<table>
<?php
while(true) {
$row1 = mysql_fetch_array($qry_result);
if($row1 === false) break;
$row2 = mysql_fetch_array($qry_result);
if($row2 !== false) {
echo "<tr><td>$row1</td><td>$row2</td></tr>";
} else {
echo "<tr><td coslspan=\"2\">$row1</td></tr>";
break; // output the final row and then stop looping
}
}
?>
</table>
<table>
<tr>
<?
$i=1;
$num = mysql_num_rows($qry_result);
while($row = mysql_fetch_array($qry_result)){
if($i%2==1)
?>
<tr>
<?
?>
<td <? if($i==$num && $i%2==1)echo 'colspan="2"';?>><?=$row['keyName']?></td>
<?
if($i%2==0)
{
<?
</tr>
?>
}
$i++;
}
?>