I am trying to loop through MySQL results and return same dates & agents in a separate . The table containing this data has the number of tickets each agent works on a specific day. Each group of dates should be separated by a blank row in the table. Below is the code I am working with. I believe I have to do a foreach, but not sure how to get it working.
Here is a screenshot to a final table layout I am looking to achieve.
if($res && mysql_num_rows($res))
{
while($row = mysql_fetch_array($res))
{
if ($row['total_updated'] > 0) {
print "<tr>";
print "<td align=center>" . $row['date_added'] . "</td>";
print "<td nowrap>" . $row['agent'] . "</td>";
print "<td nowrap>" . $row['agent_location'] . "</td>";
print "<td align=center>" . number_format($row['total_updated']) . "</td>";
print "<td align=center>" . number_format($row['total_notes']) . "</td>";
print "<td align=center>" . number_format($row['total_closed']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app1_updated']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app1_notes']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app1_closed']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app2_updated']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app2_notes']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app2_closed']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app3_updated']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app3_notes']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app3_closed']) . "</td>";
print "</tr>";
}
}
}
print "</table>";
If you sort your results by date_added you don't need any foreach, just compare previous date with current one:
while($row = mysql_fetch_array($res))
{
if (!isset($lastdate))
$lastdate = $row['date_added'];
if ($lastdate != $row['date_added']) {
?><tr><td colspan="15">---blank line---</td></tr> <?php
}
//paste all of your prints here
$lastdate = $row['date_added'];
}
Related
I want to change the color of the entire cell according to a specific value. But what should I do when the grammar is wrong and it doesn't work?
in this my code
echo "<tr if( $row['treatment_fees_check_division'] == 'error') : style='yellow' >";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['chart_num'] . "</td>";
echo "<td>" . $row['chart_name'] . "</td>";
echo "<td>" . substr($row['visit'], 0,10) . "</td>";
echo "<td >" . number_format($row['total_medical_bills']) . "</td>";
echo "<td >" . number_format($row['total_amount']) . "</td>";
echo "<td>" . number_format($row['amount_asked']) . "</td>";
echo "<td>" . number_format($row['medical_bills_payment']) . "</td>";
echo "<td>" . number_format($row['personal_liability_amount']) . "</td>";
echo "<td >" . number_format($row['non_payment']) . "</td>";
echo "<td >" . number_format($row['non_payment_sales']) . "</td>";
echo "<td>" . $row['insurance_division'] . "</td>";
echo "<td>" . $row['division'] . "</td>";
echo "<td>" . number_format($row['cash_amount_received']) . "</td>";
echo "<td>" . number_format($row['cash_receipt']) . "</td>";
echo "<td>" . number_format($row['cash_receipt_non_payment']) . "</td>";
echo "<td>" . number_format($row['cash_receipt_payment']) . "</td>";
echo "<td>" . number_format($row['card_amount_received']) . "</td>";
echo "<td>" . number_format($row['card_non_payment']) . "</td>";
echo "<td>" . number_format($row['card_payment']) . "</td>";
echo "<td>" . number_format($row['treatment_fees_difference']) . "</td>";
echo "<td>" . $row['treatment_fees_check_division'] . "</td>";
echo "</tr>";
Try replacing your first line with something like this.
if ($row['treatment_fees_check_division'] == 'error') {
echo "<tr style='yellow' >";
} else {
echo "<tr >";
}
Here's my code:
foreach ($grabdata as $row) {
echo "<tr>";
echo "<td class='editableColumns'>" . $row['EmployeeName'] . "</td>";
echo "<td class='editableColumns'>" . $row['EmployeeID'] . "</td>";
echo "<td class='editableColumns'>" . $row['ContactNumber'] . "</td>";
echo "<td class='editableColumns'>" . $row['Gender'] . "</td>";
echo "<td class='editableColumns'>" . $row['Email'] . "</td>";
echo "<td class='editableColumns'>" . $row['DOB'] . "</td>";
echo "<td class='editableColumns'>" . $row['AreaOfExpertise'] . "</td>";
echo "<td class='editableColumns'>" . $row['State'] . "</td>";
echo "<td class='editableColumns'>" . $row['Status'] . "</td>";
echo "<td><input class='editValues' type='button' value='Edit'></td>";
echo "</tr>";
echo "</table>";
}
I expected all data in the db to be displayed in a table. Unfortunately only the first result appears in the table and the following appear without the table. What am I doing wrong?
You need to put the </table>-Tag outside of the foreach loop:
echo "<table>";
foreach ($grabdata as $row) {
echo "<tr>";
echo "<td class='editableColumns'>" . $row['EmployeeName'] . "</td>";
echo "<td class='editableColumns'>" . $row['EmployeeID'] . "</td>";
echo "<td class='editableColumns'>" . $row['ContactNumber'] . "</td>";
echo "<td class='editableColumns'>" . $row['Gender'] . "</td>";
echo "<td class='editableColumns'>" . $row['Email'] . "</td>";
echo "<td class='editableColumns'>" . $row['DOB'] . "</td>";
echo "<td class='editableColumns'>" . $row['AreaOfExpertise'] . "</td>";
echo "<td class='editableColumns'>" . $row['State'] . "</td>";
echo "<td class='editableColumns'>" . $row['Status'] . "</td>";
echo "<td><input class='editValues' type='button' value='Edit'></td>";
echo "</tr>";
}
echo "</table>";
<?php
{
echo "</br>";
echo "</br>";
echo "<table border='1'> <tr>";
while ($row1=mysql_fetch_array($result1)){
echo "<td><strong>" . $_POST['card_no']. "</strong></td>";
echo "<td><strong>" . $row1['lname']. "</strong></td>";
}
"</tr>";
echo "<tr><td><strong> Total Incentive: </strong></td><td><strong> Rs." $TotalIncentive " </strong> </td></tr>";
//i want to show Total Incentive Value above table "$TotalIncentive". pls see attached image for more clarification.
echo "<table border='1' width='auto'>
<tr>
<th>Tid</th>
<th>Depart</th>
<th>Sub Depart</th>
<th>EARN</th>
<th>Eff</th>
<th> Group Eff </th>
<th>Date</th>
<th>Incentive</th>
</tr>";
//$sql=Query not show here becz its very large Query
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td bgcolor='#FFFFF'>" . $row['t_id'] . "</td>";
echo "<td bgcolor='#FFFFF'>" . $row['dep_code'] . "</td>";
echo "<td bgcolor='#FFFFF'>" . $row['subdep_code']." ".$row['type_code'] . "</td>";
echo "<td bgcolor='#FFFFF'>" . $row['TER']. "</td>";
echo "<td bgcolor='#00FF33'>" . round(($row['TER']/570)*$pre). "%</td>";
echo "<td bgcolor='#FFFF00'>" . round($row['geff1']) . "%</td>";
echo "<td bgcolor='#FFFFF'>" . $row['tdate'] . "</td>";
if($row['geff1']>=105){
$inc=(300*($row['TER']/570)*$pre)/105;
}elseif($row['geff1']>=100){
$inc=(275*($row['TER']/570)*$pre)/100;
}elseif($row['geff1']>=95){
$inc=(240*($row['TER']/570)*$pre)/95;
}elseif($row['geff1']>=90){
$inc=(200*($row['TER']/570)*$pre)/90;
}elseif($row['geff1']>=85){
$inc=(160*($row['TER']/570)*$pre)/85;
}elseif($row['geff1']>=80){
$inc=(120*($row['TER']/570)*$pre)/80;
}elseif($row['geff1']>=75){
$inc=(90*($row['TER']/570)*$pre)/75;
}elseif($row['geff1']>=70){
$inc=(60*($row['TER']/570)*$pre)/70;
}elseif($row['geff1']>=65){
$inc=(30*($row['TER']/570)*$pre)/65;
}elseif($row['geff1']<65){
$inc=0;
}
echo "<td ><strong>Rs.".round($inc,2);"</strong></td>";
echo "</tr>";
//Total Incentive Should be SUM(round($inc,2))
}
echo "</table>";
mysql_close($con);
}
//I want to Show "Total Incentive" Value in the place that i mention in my Image
?>
update your while loop as below & echo $totalInc at the end of this while() loop
<?php
$totalInc = 0; //added this variable
while ($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td bgcolor='#FFFFF'>" . $row['t_id'] . "</td>";
echo "<td bgcolor='#FFFFF'>" . $row['dep_code'] . "</td>";
echo "<td bgcolor='#FFFFF'>" . $row['subdep_code']." ".$row['type_code'] . "</td>";
echo "<td bgcolor='#FFFFF'>" . $row['TER']. "</td>";
echo "<td bgcolor='#00FF33'>" . round(($row['TER']/570)*$pre). "%</td>";
echo "<td bgcolor='#FFFF00'>" . round($row['geff1']) . "%</td>";
echo "<td bgcolor='#FFFFF'>" . $row['tdate'] . "</td>";
if($row['geff1']>=105){
$inc=(300*($row['TER']/570)*$pre)/105;
}elseif($row['geff1']>=100){
$inc=(275*($row['TER']/570)*$pre)/100;
}elseif($row['geff1']>=95){
$inc=(240*($row['TER']/570)*$pre)/95;
}elseif($row['geff1']>=90){
$inc=(200*($row['TER']/570)*$pre)/90;
}elseif($row['geff1']>=85){
$inc=(160*($row['TER']/570)*$pre)/85;
}elseif($row['geff1']>=80){
$inc=(120*($row['TER']/570)*$pre)/80;
}elseif($row['geff1']>=75){
$inc=(90*($row['TER']/570)*$pre)/75;
}elseif($row['geff1']>=70){
$inc=(60*($row['TER']/570)*$pre)/70;
}elseif($row['geff1']>=65){
$inc=(30*($row['TER']/570)*$pre)/65;
}elseif($row['geff1']<65){
$inc=0;
}
$totalInc = $totalInc + $inc; //set value to this variable
echo "<td ><strong>Rs.".round($inc,2);"</strong></td>";
echo "</tr>";
I am trying to add a column before my first column that starts numbering each record, starting from 1. I was trying to add autoincrement to this line echo "" . $row['ss'] . "";, but that does not seem to want to work. Any ideas how to do this?
My code looks like this:
$result = mysqli_query($con,"SELECT * FROM `results` WHERE Event='100' AND Gender='M' ORDER BY Performance ASC");
echo "<table border='0'>
<tr>
<th align='left'>Pos</th>
<th align='left'>Event</th>
<th>Performance</th>
<th>Wind</th>
<th>Place</th>
<th align='left'>Name</th>
<th align='left'>Surname</th>
<th>Age</th>
<th>Team</th>
<th>Meet</th>
<th>Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ss'] . "</td>";
echo "<td>" . $row['Event'] . "</td>";
echo "<td>" . $row['Performance'] . "</td>";
echo "<td>" . $row['Wind'] . "</td>";
echo "<td>" . $row['Pos'] . "</td>";
echo "<td width='100' align='left'>" . $row['Surname'] . "</td>";
echo "<td Width='100'>" . $row['Name'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
echo "<td>" . $row['Meet'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Add this line in header
<th align='left'>#</th>
And here php code
$count = 1;
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $count . "</td>";
echo "<td>" . $row['ss'] . "</td>";
echo "<td>" . $row['Event'] . "</td>";
echo "<td>" . $row['Performance'] . "</td>";
echo "<td>" . $row['Wind'] . "</td>";
echo "<td>" . $row['Pos'] . "</td>";
echo "<td width='100' align='left'>" . $row['Surname'] . "</td>";
echo "<td Width='100'>" . $row['Name'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
echo "<td>" . $row['Meet'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
//other code
$count=$count+1;
}
A better solution would be to use a variable counter to "number" your rows:
$counter = 1;
while($row = mysqli_fetch_array($result))
echo "<td>" . $counter . "</td>";
$counter++;
}
This is the right way to do it since if that ss column is the mysql auto increment, then your rows will not be numbered by the SORT but rather from the order in which they were inserted into the database regardless of any sorting you apply.
Try to change fetch_array with fetch_assoc
I have one while loop which displays data from the database. Now I want multiply two values in one row and display the result in the same row, the same way multiply the values and display the result in every row. I am not getting the result. Can anyone help me? I am new to PHP.
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class='alt'>" . $row['id'] . "</td>";
echo "<td>" . $row['item'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
$ss=$row['amount'];
echo '<td >'.'<input type="checkbox" name="status" value="" >'.'</td>';
echo '<td >'.'<input type="text" name="qty">'.'</td>';
echo "<td>" . $rr1 . "</td>";
echo "</tr>";
}
From Where you are getting $rr1??
You can have
echo "<td>" . $row['item'] * $row['amount'] . "</td>";
Hope this is what you want...
you can try
printf('amount is %d',$row['item'] * $row['amount']);
reference
Try below :
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class='alt'>" . $row['id'] . "</td>";
echo "<td>" . $row['item'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" .($row['item'] * $row['amount']). "</td>";
echo "</tr>";
}
Change the query as
$result= mysql_query("Select id, item, amount, (item * amount) total
from table_name");
then in while loop
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td >" . $row['id'] . "</td>";
echo "<td>" . $row['item'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" .($row['total'] "</td>";
echo "</tr>";
}