i want to calculate subtraction of row2 & row1 - php

I display mysql table data using php.
I search but find solution for column but not for row.
Below I try to show what I want...
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<table>
<tr>
<td id="<?php echo $row["id"]; ?>"><?php echo $row["total"]; ?></td>
<td id="difference"> Difference from previous row. </td>
</tr>
<?php
$i++;
}
?>
</table>

<?php
$i=0;
$oldval = 0;
while($row = mysqli_fetch_array($result)) {
?>
<table>
<tr>
<td id="<?php echo $row["id"]; ?>"><?php echo $row["total"]; ?></td>
<td id="difference"> <?php echo ($i==0) ? $oldval : $row['total']-$oldval; ?> </td>
</tr>
<?php
$oldval = $row["total"];
$i++;
}
?>
</table>
$oldval variable is used to store current row's total field data so when you go to the next row, you can get the difference for current row because you have previous row's data stored in the $oldval
And the ternary condition I put there is because if $i==0 means the first row so you don't have any data of the previous row so by default difference is 0 and you can notice that I stored the current record's total field data after printing the difference

Related

PHP INSERT It Show only one

<table border="1" width="500">
<tbody>
<tr>
<th>ProFileID</th>
<th>CogID</th>
<th>FileValue</th>
<th> Count </th>
<?php
$sql="Select * From idtable"; // Read idtable
$result=mysql_query($sql); // Query sql
$num=mysql_num_rows($result); // Check record
if($num>0){ // If record > 0
$count=1; //
while($recordset=mysql_fetch_assoc($result)){ // loop
?>
<tr>
<td align="center"><?php echo $count; ?></td>
<td><?php echo $cogid = $recordset['CogID']; ?></td>
<td><?php echo $FileValue = $recordset['FileValue']; ?></td>
<td><? $url = "https://s3-ap-southeast-1.amazonaws.com/cloudpoc2/".$cogid."/Contacts/contactsversion3/".$FileValue."";
//example "https://s3-ap-southeast-1.amazonaws.com/cloudpoc2/us-east-1%3Ab9b09e99-5921-494a-b5a7-54f289131eaa/Contacts/contactsversion3/Contacts_1437450598237.txt";
$getText = file_get_contents("$url", true);
$Contact = substr_count($getText ,"FIRSTNAME");
echo $countupdate = $Contact; ?>
</tr>
<?
$count+=1; // Increase count +1
}
}
?>
</tbody>
<?php // UPDATE TO DATABASE
$updatecount = "INSERT INTO counttable (ProfileID,Count,Timestamp)
VALUES('','$countupdate','')";
mysql_query($updatecount);
?>
**I want to update count to phpmyadmin Table name is Countable I have 2 data but it show only one data i don't know what happen ** Some on kind please help me
This my pic
You do an update after while loop is over. After loop is over $updatecount stores the last value of $Contact. If you want to update table on each iteration of while loop, move your update query in the loop:
while($recordset=mysql_fetch_assoc($result)) { // loop
// do something here
$updatecount = "INSERT INTO counttable (ProfileID,Count,Timestamp) VALUES('','$countupdate','')";
mysql_query($updatecount);
}

How do I make a MySQL infinite array table?

I am using this code to create an infinite table for my mysql queries:
<table cellspacing='0'> <!-- cellspacing='0' is important, must stay -->
<!-- Table Header -->
<thead>
<tr>
<th>User</th>
<th>SteamID</th>
<th>Banned by</th>
<th>Admin SteamID</th>
<th>Time Banned (min)</th>
<th>Reason</th>
</tr>
</thead>
<!-- Table Header -->
<!-- Table Body -->
<tbody>
<?php
echo '<tr>';
for($i = 0; $bans = mysqli_fetch_array($query2); $i = ($i+1)%3){
echo '<td>'.$bans['name'].'</td>';
echo '<td>'.$bans['steamid'].'</td>';
echo '<td>'.$bans['nameAdmin'].'</td>';
echo '<td>'.$bans['steamidAdmin'].'</td>';
echo '<td>'.$bans['time'].'</td>';
echo '<td>'.$bans['reason'].'</td>';
if($i == 2)
echo '</tr><tr>';
}
echo '</tr>';
?>
</tbody>
I got that code from Mysql fetch array, table results
It works fine, except it doesn't CORRECTLY go further down than 6 rows. The other rows for whatever reason are placed to the right of my last column as shown in this screenshot:
http://puu.sh/h0qZF/a12de1dd87.png
How can I fix this? Is there something wrong with my code? Why is it happening?
Well, your looping makes no sense. Using $i to inject new rows, like is done here, is not necessary; you can just loop over each row and then output it as a row:
<table>
<!-- <thead>...</thead> -->
<tbody>
<?php while ($bans = mysqli_fetch_array($query2)): ?>
<tr>
<td><?php echo $bans['name'] ?></td>
<td><?php echo $bans['steamid'] ?></td>
<td><?php echo $bans['nameAdmin'] ?></td>
<td><?php echo $bans['steamidAdmin'] ?></td>
<td><?php echo $bans['time'] ?></td>
<td><?php echo $bans['reason'] ?></td>
</tr>
<?php endwhile ?>
</tbody>
</table>
You are making two columns.
You have code that will print out the end of the table row every two sets of data:
if($i == 2)
echo '</tr><tr>';
It should just be echo '</tr><tr>';
Use a while loop as instructed here . So something like this:
$result = $conn->query($sql);
while($bans = $result->fetch_assoc()) {
echo '<td>'.$bans['name'].'</td>';
echo '<td>'.$bans['steamid'].'</td>';
echo '<td>'.$bans['nameAdmin'].'</td>';
}

unique id for dynamic table

I will be generating a HTML table with data pulled from MySQL.The number of rows in my MySQL table are not fixed.
<?php
while($row=mysql_fetch_assoc($result))
{ ?>
<tr>
<td><?php echo $row['col1'];?></td>
<td><?php echo $row['col2'];?></td>
</tr>
<?php } ?>
Now how do I have the table rows and table data elements assigned unique id ??
Another loop to generate them won't work as I can't set an exit condition for the new loop as number of rows are not fixed.
Please guide me as to how to go forward about it. I can only use Javascript and not JQUERY.
Why can't you do something like this ?
<?php
$i = 1;
while($row=mysql_fetch_assoc($result))
{ ?>
<tr id="row<?php echo $i;?>">
<td id="cell-left-<?php echo $i;?>"><?php echo $row['col1'];?></td>
<td id="cell-right-<?php echo $i;?>"><?php echo $row['col2'];?></td>
</tr>
<?php
$i++;
} ?>
Please note, I have added ids row, cell-left- and cell-right- by myself. You may change them as per your requirements.
You can use a counter when iterating through the rows, maybe something like this:
<?php
$rowCount = 0;
while($row=mysql_fetch_assoc($result))
{
$rowCount++;
?>
<tr id="<?php echo 'row' . $rowCount;?>">
<td><?php echo $row['col1'];?></td>
<td><?php echo $row['col2'];?></td>
</tr>
<?php
}
?>
You can now select an element with
var rowID = 1;
document.getElementById("row" + rowID);
Hope this helps.

Empty table row in while loop from query

I have a while loop which displays records in a table. Works OK, but there is an extra table row at the top of the table which is empty.
<table width="100%" border="0" cellpadding="5">
<?php do {
$count++;
?>
<tr bgcolor=<?php echo processRow($count); ?>>
<td><?php echo $row['title']; ?></td>
<td width="8%">edit</td>
<td width="12%">delete</td>
</tr>
<?php
if($count == 2){
$count = 0;
}
} while ($row = mysql_fetch_assoc($result));
?>
</table>
The first time the loop starts $row = mysql_fetch_assoc($result) is not yet executed therefore no result is being fetched.
Add $row = mysql_fetch_assoc($result); at the beginning of the file (before the loop) or convert the do-while loop into a while to solve the problem.
the do {} while() construct goes over the contents of the block first, before evaluating what is passed to while. if you write it like this, is should get rid of the empty row:
<?php while ($row = mysql_fetch_assoc($result)): ?>
<? $count++; ?>
<tr bgcolor=<?php echo processRow($count); ?>>
<td><?php echo $row['title']; ?></td>
<td width="8%">edit</td>
<td width="12%">delete</td>
</tr>
<?php
if($count == 2){ $count = 0; }
endwhile;
?>

Odd and Even Rows for a table

I have a table that get its rows from a MYSQL database
<table id="table1">
<?php
// Connect to database server
mysql_connect("localhost", "root", "asnaeb") or die (mysql_error ());
// Select database
mysql_query("SET NAMES `utf8`"); // UTF 8 support!!
mysql_select_db("scores") or die(mysql_error());
// SQL query
$strSQL = "SELECT * FROM latest";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {
// Write the value of the column FirstName (which is now in the array $row)
?>
<?php echo $row['Header'].""; ?>
<tr>
<td id='date'><?php echo $row['Date'].""; ?></td>
<td id='time'><?php echo $row['Time'].""; ?></td>
<td id='hometeam'><?php echo $row['HomeTeam'].""; ?></td>
<td id='score'><?php echo $row['Score'].""; ?></td>
<td id='awayteam'><?php echo $row['AwayTeam'].""; ?></td>
<td id='other'><?php echo $row['Other'].""; ?></td>
</tr>
<?php } mysql_close(); ?>
</table>
i have 2 css class called "A" and "B" for Odd Rows and Even Rows
i currently getting this done by replacing <tr> with <tr class='<?php echo $row['Row'].""; ?>'> and i have in my Database table a column "Row" which i add in A or B for even or odd row... the problem is if i wanna delete or add a new row between one of these i will have to change all the A and B in the other.
I have seen in another questions many way to do that in javascript or jquery but for a normal table with TR's which is not my case...(tried some of these scripts but couldn't get it fixed)
So what i want an easier way to do that Even and Odd rows, Thanks!
do it in CSS way (no inline class) once and for all:
in CSS:
#table1 tr:nth-child(odd) td { background-color:#ebebeb }
#table1 tr:nth-child(even) td { background-color:#0000ff }
in your HTML:
<table id="table1">
thats it, no matter if your table rows are removed/or not.
You can add those classes using jQuery easily like this
$(function(){
$('#table1 tr:odd').addClass('A');
// for even
$('#table1 tr:even').addClass('B');
});
Why didn't you use modulo in your while loop ? It's a better way than store your class in your database... :
$i = 0;
while($row = mysql_fetch_array($rs)) {
// Write the value of the column FirstName (which is now in the array $row)
?>
<?php echo $row['Header'].""; ?>
<tr class="<?php echo $i%2 == 0 ? "class_A" : "class_B" ; $i++;?>" >
<td id='date'><?php echo $row['Date'].""; ?></td>
<td id='time'><?php echo $row['Time'].""; ?></td>
<td id='hometeam'><?php echo $row['HomeTeam'].""; ?></td>
<td id='score'><?php echo $row['Score'].""; ?></td>
<td id='awayteam'><?php echo $row['AwayTeam'].""; ?></td>
<td id='other'><?php echo $row['Other'].""; ?></td>
</tr>
<?php } mysql_close(); ?>
<?php
$class="odd"
while($row = mysql_fetch_array($rs)) {
$class = ($class=='even' ? 'odd' : 'even');
?>
<tr class="<?php echo $class">
...
</tr>
<?php } ?>
There are many ways to do this, PHP, Javascript and even pure CSS. Here's the PHP way to add a class to every other row:
while($row = mysql_fetch_blahblah()) {
$i = 0; ?>
<tr class="<?php echo $i % 2 == 0 ? 'class1' : 'class2';?>">
<td>....</td>
</tr>
<?php
$i++; // increment our counter
}
Basically the modulus operator returns the remainder of dividing the nubmers either side of it, so for example 3 % 2 == 1, 4 % 2 == 0, 5 % 2 == 1, so we can tell if $i is odd or even and alternate the classes added to the <tr>.
IMHO you want to either do it this way for 100% guarantee it will work (no browser dependencies) or if you design your app for modern browsers go for the CSS route.

Categories