Displaying php query in column instead of row - php
Hey guys i have a postgres database to which i am connecting and fetching data from using php. I am a newbie to php. But anyway i have this as my query
$sql = "SELECT to_char (a.CALLDATE,'yyyymm') as month,min(a.calldate) as start_time,max(a.calldate) as end_time,
ceil(SUM (a.CALLDURATION::integer) / 60) AS minutes,
COUNT (DISTINCT a.IDENTIFIANT) AS distinct_callers,
a.zoneiddest as country_code,b.country
FROM cdr_data a,COUNTRY_CODES b
WHERE a.CALLSUBCLASS = '002'
AND a.CALLCLASS = '008'
and a.zoneiddest::integer > 0
AND SUBSTR (a.CALLEDNUMBER, 1, 2) NOT IN
('77', '78', '75', '70', '71', '41', '31', '39', '76','79')
and not substr(a.zoneiddest , 1 ,3) in ('254','255','256','211','257','250','256')
and trim(a.zoneiddest) = trim(b.country_code)
GROUP BY to_char (a.CALLDATE,'yyyymm') ,a.zoneiddest,b.country
ORDER BY 1";
And i have this to return the data that is queried:
// iterate over result set
// print each row
while ($row = pg_fetch_array($result)) {
echo "Month: " . $row[0] . "<br />";
echo "Start Time: " . $row[1] . "<br />";
echo "End Time: " . $row[2] . "<br />";
echo "Minutes: " . $row[3] . "<br />";
echo "Distinct Callers: " . $row[4] . "<br />";
echo "Country Code: " . $row[5] . "<br />";
echo "Country: " . $row[6] . "<br />";
}
This returns my data in this format
Month: 201212
Start Time: 2012-12-28 15:51:04
End Time: 2012-12-28 15:51:04
Minutes: 0
Distinct Callers: 1
Country Code: 44
Country: United Kingdom of Great Britain and Northern Ireland
But would love to have this data in the table where month, start time, end time, minutes, distinct callers, country code and country are the column headers in the table. How can i achieve this.
Please check the code below. Let me know if my understanding of your requirements is incorrect.
<table>
<tr>
<td>Month</td>
<td>Start Time</td>
<td>End Time</td>
<td>Minutes</td>
<td>Distinct Callers</td>
<td>Country Code/td>
<td>Country</td>
</tr>
<?php
while ($row = pg_fetch_array($result)) {
echo '<tr>';
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
echo '</tr>';
}
?>
</table>
dont forget to add loop
echo "<table cellpadding='5' cellspacing='1' style='background-color: #D7D2D2;font-size:12px;font-family:Tahoma;'>";
echo "<tr>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Month</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Start Time</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>End Time</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Minutes</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Distinct Callers</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Country Code</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Country</th>";
echo "</tr>";
echo "<tr>";
echo "<td style='background-color: #FFFFFF;'>$row[0]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[1]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[2]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[3]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[4]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[5]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[6]</td>";
echo "</tr>";
echo "</table>";
You can do it like this
$output = "<table>";
$output .= "<tr>";
$output .= "<td> Month </td>";
$output .= "<td> Start Time </td>";
$output .= "<td> End Time </td>";
$output .= "<td> Minutes </td>";
$output .= "<td> Distinct Callers </td>";
$output .= "<td> Country Codes </td>";
$output .= "<td> Country </td>";
$output .= "</tr>";
while ($row = pg_fetch_array($result)) {
$output .= "<tr>";
$output .= "<td>". $row[0] . "</td>";
$output .= "<td>". $row[1] . "</td>";
$output .= "<td>". $row[2] . "</td>";
$output .= "<td>". $row[3] . "</td>";
$output .= "<td>". $row[4] . "</td>";
$output .= "<td>". $row[5] . "</td>";
$output .= "<td>". $row[6] . "</td>";
$output .= "</tr>";
}
$output .= "</table>";
echo $output;
Hope this helps
echo "<table>";
echo "<tr>";
echo "<td>Month</td>";
echo "<td>Start Time</td>";
echo "<td>End Time</td>";
echo "<td>Minutes</td>";
echo "<td>Distinct Callers</td>";
echo "<td>Country Code</td>";
echo "<td>Country</td>";
echo "</tr>";
while ($row = pg_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "<td>".$row[5]."</td>";
echo "<td>".$row[6]."</td>";
echo "</tr>";
}
echo "</table>";
Related
I have this type of json format data
{ "fertilizer":[{"pg1":"-21.259515860749435","pg2":"24.741169305724725","lastyearlastmonth":"764.119","currentmonth":"601.671","currentyearytd":"5735.1","lastyearytd":"4597.6","pname":"Urea","mmonth":"11","period":"11","myear":"2017"},{"pg1":"-20.53085432388131","pg2":"9.258986807905458","lastyearlastmonth":"631.435","currentmonth":"501.796","currentyearytd":"2227.9","lastyearytd":"2039.1","pname":"DAP","mmonth":"11","period":"11","myear":"2017"},{"pg1":"67.37546062508531","pg2":"51.07126222636238","lastyearlastmonth":"36.635","currentmonth":"61.318","currentyearytd":"648.7","lastyearytd":"429.4","pname":"CAN","mmonth":"11","period":"11","myear":"2017"},{"pg1":"-49.542998848640515","pg2":"7.7561388653683245","lastyearlastmonth":"112.91","currentmonth":"56.971","currentyearytd":"636.3","lastyearytd":"590.5","pname":"NP","mmonth":"11","period":"11","myear":"2017"},{"pg1":"-53.39393939393939","pg2":"-2.1138211382113776","lastyearlastmonth":"4.95","currentmonth":"2.307","currentyearytd":"60.2","lastyearytd":"61.5","pname":"NPK","mmonth":"11","period":"11","myear":"2017"},{"pg1":"-14.652234166073644","pg2":"-5.41561712846349","lastyearlastmonth":"26.699","currentmonth":"22.787","currentyearytd":"75.1","lastyearytd":"79.4","pname":"SSP","mmonth":"11","period":"11","myear":"2017"},{"pg1":"123.88827636898196","pg2":"239.6551724137931","lastyearlastmonth":"2.721","currentmonth":"6.092","currentyearytd":"39.4","lastyearytd":"11.6","pname":"SOP","mmonth":"11","period":"11","myear":"2017"},{"pg1":"58.21359223300969","pg2":"134.07407407407408","lastyearlastmonth":"2.575","currentmonth":"4.074","currentyearytd":"31.6","lastyearytd":"13.5","pname":"MOP","mmonth":"11","period":"11","myear":"2017"},{"pg1":null,"pg2":null,"lastyearlastmonth":"0","currentmonth":"0","currentyearytd":"0","lastyearytd":"0","pname":"MAP","mmonth":"11","period":"11","myear":"2017"},{"pg1":null,"pg2":null,"lastyearlastmonth":"0","currentmonth":"0","currentyearytd":"0","lastyearytd":"0","pname":"TSP","mmonth":"11","period":"11","myear":"2017"},{"pg1":"-56.21508379888268","pg2":"-0.6802721088435351","lastyearlastmonth":"2.864","currentmonth":"1.254","currentyearytd":"14.6","lastyearytd":"14.7","pname":"AS","mmonth":"11","period":"11","myear":"2017"},{"pg1":"-20.609372141004858","pg2":"20.81536932387274","lastyearlastmonth":"1584.91","currentmonth":"1258.27","currentyearytd":"9468.82","lastyearytd":"7837.43","pname":"Total","mmonth":"11","period":"11","myear":"2017"}], "automobile":[{"pg1":"11.009369676320272","pg2":"22.30648472406714","lastyearlastmonth":"14088","currentmonth":"15639","currentyearytd":"50641","lastyearytd":"41405","pname":"PC","mmonth":"9","period":"3","myear":"2017"},{"pg1":"2597.0588235294117","pg2":"1874.4827586206895","lastyearlastmonth":"34","currentmonth":"917","currentyearytd":"2863","lastyearytd":"145","pname":"SUV","mmonth":"9","period":"3","myear":"2017"},{"pg1":"15.686274509803921","pg2":"14.290401968826908","lastyearlastmonth":"1938","currentmonth":"2242","currentyearytd":"6966","lastyearytd":"6095","pname":"LCV","mmonth":"9","period":"3","myear":"2017"},{"pg1":"58.64978902953587","pg2":"34.17569193742479","lastyearlastmonth":"474","currentmonth":"752","currentyearytd":"2230","lastyearytd":"1662","pname":"Truck","mmonth":"9","period":"3","myear":"2017"},{"pg1":"-60.57692307692307","pg2":"-34.74320241691843","lastyearlastmonth":"104","currentmonth":"41","currentyearytd":"216","lastyearytd":"331","pname":"Bus","mmonth":"9","period":"3","myear":"2017"},{"pg1":"74.47245017584994","pg2":"99.92364469330617","lastyearlastmonth":"3412","currentmonth":"5953","currentyearytd":"15710","lastyearytd":"7858","pname":"Tractor","mmonth":"9","period":"3","myear":"2017"},{"pg1":"14.790290676232942","pg2":"27.133399110577265","lastyearlastmonth":"119308","currentmonth":"136954","currentyearytd":"444541","lastyearytd":"349665","pname":"2 Wheel","mmonth":"9","period":"3","myear":"2017"},{"pg1":"33.41478313989004","pg2":"30.228058051140287","lastyearlastmonth":"4911","currentmonth":"6552","currentyearytd":"18844","lastyearytd":"14470","pname":"3 Wheel","mmonth":"9","period":"3","myear":"2017"},{"pg1":"17.04856787048568","pg2":"26.917829782768393","lastyearlastmonth":"16060","currentmonth":"18798","currentyearytd":"60470","lastyearytd":"47645","pname":"Total Cars","mmonth":"9","period":"3","myear":"2017"}], "oilmkting":[{"pg1":"-100","pg2":"-100.00000","lastyearlastmonth":"0.102","currentmonth":"0","currentyearytd":"0.0","lastyearytd":"0.4","pname":"100LL","mmonth":"10","period":"4","myear":"2017"},{"pg1":"-5.129426108196923","pg2":"-0.04207","lastyearlastmonth":"59.803999999999995","currentmonth":"56.73639801025391","currentyearytd":"237.6","lastyearytd":"237.7","pname":"JP-1","mmonth":"10","period":"4","myear":"2017"},{"pg1":"1194.9860724233984","pg2":"104.22535","lastyearlastmonth":"1.436","currentmonth":"18.596","currentyearytd":"58.0","lastyearytd":"28.4","pname":"JP-8","mmonth":"10","period":"4","myear":"2017"},{"pg1":"8.688345498555524","pg2":"15.40422","lastyearlastmonth":"575.4939999999999","currentmonth":"625.494907043457","currentyearytd":"2580.9","lastyearytd":"2236.4","pname":"MS","mmonth":"10","period":"4","myear":"2017"},{"pg1":"179.72428859212695","pg2":"209.85915","lastyearlastmonth":"3.8990000000000005","currentmonth":"10.906450012207031","currentyearytd":"44.0","lastyearytd":"14.2","pname":"HOBC","mmonth":"10","period":"4","myear":"2017"},{"pg1":null,"pg2":null,"lastyearlastmonth":"0","currentmonth":"0","currentyearytd":"0.0","lastyearytd":"0.0","pname":"E-10","mmonth":"10","period":"4","myear":"2017"},{"pg1":"52.374035671418994","pg2":"11.97917","lastyearlastmonth":"7.263999999999999","currentmonth":"11.068449951171875","currentyearytd":"43.0","lastyearytd":"38.4","pname":"Kerosene","mmonth":"10","period":"4","myear":"2017"},{"pg1":"1.599296460019584","pg2":"18.32285","lastyearlastmonth":"836.0709999999998","currentmonth":"849.4422539062501","currentyearytd":"3142.3","lastyearytd":"2655.7","pname":"HSD","mmonth":"10","period":"4","myear":"2017"},{"pg1":"9.73656730601859","pg2":"61.22449","lastyearlastmonth":"1.526","currentmonth":"1.6745800170898437","currentyearytd":"7.9","lastyearytd":"4.9","pname":"LDO","mmonth":"10","period":"4","myear":"2017"},{"pg1":"4.033703464379442","pg2":"-6.30181","lastyearlastmonth":"866.287","currentmonth":"901.2304487304688","currentyearytd":"3413.8","lastyearytd":"3643.4","pname":"FO","mmonth":"10","period":"4","myear":"2017"},{"pg1":"5.2411828169554875","pg2":"7.54227","lastyearlastmonth":"2351.8830000000007","currentmonth":"2475.149487670898","currentyearytd":"9527.6","lastyearytd":"8859.4","pname":"Total","mmonth":"10","period":"4","myear":"2017"}], "cement":[{"pg1":"4.941027677946538","pg2":"12.29975","lastyearlastmonth":"3554.888","currentmonth":"3730.536","currentyearytd":"22242.2","lastyearytd":"19806.1","pname":"Domestic","mmonth":"12","period":"6","myear":"2017"},{"pg1":"-11.24660806265538","pg2":"-17.33874","lastyearlastmonth":"369.258","currentmonth":"327.729","currentyearytd":"2406.6","lastyearytd":"2911.4","pname":"Exports","mmonth":"12","period":"6","myear":"2017"},{"pg1":"3.417788227043532","pg2":"8.50138","lastyearlastmonth":"3924.146","currentmonth":"4058.265","currentyearytd":"24648.8","lastyearytd":"22717.5","pname":"Total Sales","mmonth":"12","period":"6","myear":"2017"}] } I have four different table fertilizer, automobile, oil marketing and cement and I want to data display in a table using for loop but data not read one by one foreach ($data as $nt) { echo "<tr class='{$dispval} {$boldrow}' >"; echo "<td>{$nt[pname]}</td>"; echo "<td class='txtright'>" . number_format($nt[lastyearlastmonth],1) . " </td>"; echo "<td class='txtright'>" . number_format($nt[currentmonth],1) . " </td>"; echo "<td class='txtright'>" . number_format($nt[pg1],1) . " </td>"; echo "<td class='txtright'>" . number_format($nt[lastyearytd],1) . " </td>"; echo "<td class='txtright' >" . number_format($nt[currentyearytd],1) . " </td>"; echo "<td class='txtright'>" . number_format($nt[pg2],1) . " </td>"; echo "</tr>"; }
Try this $json = json_decode($jsonData, true); $fCount = count($json['fertilizer']); for( $i = 0; $i<$fCount; $i++ ) { echo "<tr >"; echo "<td>".$json['fertilizer'][$i]['pname']."</td>"; echo "<td class='txtright'>". number_format($json['fertilizer'][$i]['lastyearlastmonth'],1)." </td>"; echo "<td class='txtright'>". number_format($json['fertilizer'][$i]['currentmonth'],1). " </td>"; echo "<td class='txtright'>". number_format($json['fertilizer'][$i]['pg1'],1). " </td>"; echo "<td class='txtright'>". number_format($json['fertilizer'][$i]['lastyearytd'],1). " </td>"; echo "<td class='txtright'>". number_format($json['fertilizer'][$i]['currentyearytd'],1). " </td>"; echo "<td class='txtright'>". number_format($json['fertilizer'][$i]['pg2'],1). " </td>"; echo "</tr >"; } Store json response in $jsonData variable similarly add other loops for automobile, oilmkting & cement Enjoy...
foreach loop highlight cells from database
I'm wondering if my code could be easier with a foreach loop. my code thusfar: the purpose is to read the values in the MYSQL table, and if the anwser is "NEE" display the background color in RED. my code works but it is very long.. $connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password mysql_select_db('heijsDB'); $query = "SELECT * FROM hygieneaanvoer"; //You don't need a ; like you do in SQL $result = mysql_query($query); //List the Columns for the Report if(! $result ) { die('Could display data: ' . mysql_error()); } echo "<table border='1' class='w3-panel'> <fieldset>hygiëne/GMP aduit Aanvoer</fieldset> <tr> <th>Datum</th> <th>Controleur</th> <th>controleur</th> <th>Revisie</th> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> <th>8</th> <th>9</th> <th>10</th> <th>11</th> <th>12</th> <th>13</th> <th>14</th> <th>15</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['datum'] . "</td>"; echo "<td>" . $row['controleur'] . "</td>"; echo "<td>" . $row['codering'] . "</td>"; echo "<td>" . $row['revisie'] . "</td>"; if($row['q1']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q1']."</td>"; else echo "<td>".$row['q1']."</td>"; if($row['q2']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q2']."</td>"; else echo "<td>".$row['q2']."</td>"; if($row['q3']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q3']."</td>"; else echo "<td>".$row['q3']."</td>"; if($row['q4']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q4']."</td>"; else echo "<td>".$row['q4']."</td>"; if($row['q5']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q5']."</td>"; else echo "<td>".$row['q5']."</td>"; if($row['q6']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q6']."</td>"; else echo "<td>".$row['q6']."</td>"; if($row['q7']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q7']."</td>"; else echo "<td>".$row['q7']."</td>"; if($row['q8']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q8']."</td>"; else echo "<td>".$row['q8']."</td>"; if($row['q9']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q9']."</td>"; else echo "<td>".$row['q9']."</td>"; if($row['q10']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q10']."</td>"; else echo "<td>".$row['q10']."</td>"; if($row['q11']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q11']."</td>"; else echo "<td>".$row['q11']."</td>"; if($row['q12']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q12']."</td>"; else echo "<td>".$row['q12']."</td>"; if($row['q13']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q13']."</td>"; else echo "<td>".$row['q13']."</td>"; if($row['q14']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q14']."</td>"; else echo "<td>".$row['q14']."</td>"; if($row['q15']=='NEE') // [val1] can be 'approved' echo "<td style='background-color: #e21010;'>".$row['q15']."</td>"; else echo "<td>".$row['q15']."</td>"; echo "</tr>"; } echo "</table>"; ?>
Here is solution $result = mysql_query("SELECT * FROM hygieneaanvoer")or die('Could display data: ' . mysql_error());; echo "<table border='1' class='w3-panel'> <fieldset>hygiëne/GMP aduit Aanvoer</fieldset> <tr> <th>Datum</th> <th>Controleur</th> <th>controleur</th> <th>Revisie</th>"; for ($i=1;$i<=15;$i++){ echo '<th>'.$i.'</th>'; } echo "</tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['datum'] . "</td>"; echo "<td>" . $row['controleur'] . "</td>"; echo "<td>" . $row['codering'] . "</td>"; echo "<td>" . $row['revisie'] . "</td>"; for ($j=1;$j<=15;$j++){ echo "<td ".(($row['q'.$j] == 'NEE') ? "style='background-color: #e21010;'" : "" ).">".$row['q'.$j]."</td>"; } echo "</tr>"; } echo "</table>"; ?>
What you could do, is just create a single variable that you'd use for setting the style of all rows: while($row = mysql_fetch_array($result)) { $style_string = ""; if($row['q1']=='NEE') { $style_string = "style='background-color: #e21010;'"; } echo "<tr>"; echo "<td>" . $row['datum'] . "</td>"; echo "<td>" . $row['controleur'] . "</td>"; echo "<td>" . $row['codering'] . "</td>"; echo "<td>" . $row['revisie'] . "</td>"; echo "<td " . $style_string . ">" . $row['q1'] . "</td>"; echo "<td " . $style_string . ">" . $row['q2'] . "</td>"; echo "<td " . $style_string . ">" . $row['q3'] . "</td>"; echo "<td " . $style_string . ">" . $row['q4'] . "</td>"; echo "<td " . $style_string . ">" . $row['q5'] . "</td>"; echo "<td " . $style_string . ">" . $row['q6'] . "</td>"; echo "<td " . $style_string . ">" . $row['q7'] . "</td>"; echo "<td " . $style_string . ">" . $row['q8'] . "</td>"; echo "<td " . $style_string . ">" . $row['q9'] . "</td>"; echo "<td " . $style_string . ">" . $row['q10'] . "</td>"; echo "<td " . $style_string . ">" . $row['q11'] . "</td>"; echo "<td " . $style_string . ">" . $row['q12'] . "</td>"; echo "<td " . $style_string . ">" . $row['q13'] . "</td>"; echo "<td " . $style_string . ">" . $row['q14'] . "</td>"; echo "<td " . $style_string . ">" . $row['q15']. "</td>"; echo "</tr>"; } (Or just set the background color of the <tr>) Best of luck!
You can simply add the class to the td like this echo "<td class='". $row['q15'] . "'>".$row['q15']."</td>"; Add css like this .NEE {background-color: #e21010;}
How to get total of given colum of mysql virtual 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>";
PHP echo without a loop. SQL Query
Here is the subject matter: Link, Click a players name to see. I want to have the players name as a header so that the players name isn't repeated over and over again for each resulting row in the Roster table. I know how to loop through results for rows, but I only want the name once at the top and don't know how to do that. This is how I print the table code wise <?php $pid = $_POST['PID']; $query = "SELECT Rosters.PID, Rosters.Goals, Rosters.Assists, Rosters.PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname, (Rosters.Goals + Rosters.Assists) AS Points FROM Rosters INNER JOIN Players ON Rosters.PID = Players.pid WHERE Rosters.PID = $pid ORDER BY Points DESC, Goals DESC"; $result = $conn->query($query); $query2 = "SELECT Rosters.PID, SUM( Rosters.Goals ) Goals, SUM( Rosters.Assists ) Assists, SUM( Rosters.PIM ) PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname, SUM((Rosters.Goals + Rosters.Assists)) AS Points FROM Rosters INNER JOIN Players ON Rosters.PID = Players.pid WHERE Rosters.PID =$pid ORDER BY Points DESC , Goals DESC"; $result2 = $conn->query($query2); echo if (!empty($_POST["PID"])) { echo "<table class='stat-table-wide table sortable' align='center'>"; echo "<tr>"; echo "<th class='hover'>Season</th>"; echo "<th class='hover'>Num</th>"; echo "<th class='hover'>Player</th>"; echo "<th class='hover'>G</th>"; echo "<th class='hover'>A</th>"; echo "<th class='hover'>Pts</th>"; echo "<th class='hover'>PIM</th>"; echo "</tr>"; $i = 13; $j = 14; while ($row = $result->fetch_assoc()) { $i++; $j++; $goals = $row["Goals"]; $assists = $row["Assists"]; $points = $goals + $assists; echo "<tr><td>20" . $i . "-20" . $j . "</td>"; echo "<td>" . $row["Num"] . "</td>"; echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>"; echo "<td>" . $row["Goals"] . "</td>"; echo "<td>" . $row["Assists"] . "</td>"; echo "<td>" . $row["Points"] . "</td>"; echo "<td>" . $row["PIM"] . "</td></tr>"; } echo "</table>"; echo "<span class='style35'>Total</span><br />"; echo "<table class='stat-table-wide table sortable' align='center'>"; echo "<tr>"; echo "<th class='hover'>Player</th>"; echo "<th class='hover'>G</th>"; echo "<th class='hover'>A</th>"; echo "<th class='hover'>Pts</th>"; echo "<th class='hover'>PIM</th>"; echo "</tr>"; while ($row = $result2->fetch_assoc()) { $goals = $row["Goals"]; $assists = $row["Assists"]; $points = $goals + $assists; echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>"; echo "<td>" . $row["Goals"] . "</td>"; echo "<td>" . $row["Assists"] . "</td>"; echo "<td>" . $row["Points"] . "</td>"; echo "<td>" . $row["PIM"] . "</td></tr>"; } echo "</table>"; } ?> I just want Players.firstname and Players.lastname printed once before any of the tables are generated.
Try this: if (!empty($_POST["PID"])) { $row = $result->fetch_assoc(); echo "<span class='style35'>" . $row["firstname"] . " " . $row["lastname"] . "</span><br />"; $result->data_seek(0); echo "<table class='stat-table-wide table sortable' align='center'>"; echo "<tr>"; echo "<th class='hover'>Season</th>"; echo "<th class='hover'>Num</th>"; echo "<th class='hover'>G</th>"; echo "<th class='hover'>A</th>"; echo "<th class='hover'>Pts</th>"; echo "<th class='hover'>PIM</th>"; echo "</tr>"; $i = 13; $j = 14; while ($row = $result->fetch_assoc()) { $i++; $j++; $goals = $row["Goals"]; $assists = $row["Assists"]; $points = $goals + $assists; echo "<tr><td>20" . $i . "-20" . $j . "</td>"; echo "<td>" . $row["Num"] . "</td>"; echo "<td>" . $row["Goals"] . "</td>"; echo "<td>" . $row["Assists"] . "</td>"; echo "<td>" . $row["Points"] . "</td>"; echo "<td>" . $row["PIM"] . "</td></tr>"; } echo "</table>"; echo "<span class='style35'>Total</span><br />"; echo "<table class='stat-table-wide table sortable' align='center'>"; echo "<tr>"; echo "<th class='hover'>Player</th>"; echo "<th class='hover'>G</th>"; echo "<th class='hover'>A</th>"; echo "<th class='hover'>Pts</th>"; echo "<th class='hover'>PIM</th>"; echo "</tr>"; while ($row = $result2->fetch_assoc()) { $goals = $row["Goals"]; $assists = $row["Assists"]; $points = $goals + $assists; echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>"; echo "<td>" . $row["Goals"] . "</td>"; echo "<td>" . $row["Assists"] . "</td>"; echo "<td>" . $row["Points"] . "</td>"; echo "<td>" . $row["PIM"] . "</td></tr>"; } echo "</table>"; } ?> I don't know if you wanted the name of the player in the totals, so I left it there, but you can delete it if you want. I also applied to the player name the same style as the total.
You could get the first row of the result before your loop, then just reset the cursor, and completely ignore the players name inside your loop. $first = $result->fetch_assoc(); // print out first name echo($first['name'] . " " . $first['surname']); // reset cursor $result->data_seek(0); // do your loops and stuff. while ($row = $result->fetch_assoc()) { ... }
You can do like this.If you do not want to change the sql query. $ctr = 0; while ($row = $result->fetch_assoc()) { $ctr++; if($ctr == 1) { echo "</table>"; echo "<span class='style35'>Total</span><br />"; echo "<table class='stat-table-wide table sortable' align='center'>"; echo "<tr>"; echo "<th class='hover'>Player</th>"; echo "<th class='hover'>G</th>"; echo "<th class='hover'>A</th>"; echo "<th class='hover'>Pts</th>"; echo "<th class='hover'>PIM</th>"; echo "</tr>"; echo "<tr><td>" . $row["firstname"] . " " . $row["lastname"] . "</td> </tr>"; } $i++; $j++; $goals = $row["Goals"]; $assists = $row["Assists"]; $points = $goals + $assists; echo "<tr><td>20" . $i . "-20" . $j . "</td>"; echo "<td>" . $row["Num"] . "</td>"; echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>"; echo "<td>" . $row["Goals"] . "</td>"; echo "<td>" . $row["Assists"] . "</td>"; echo "<td>" . $row["Points"] . "</td>"; echo "<td>" . $row["PIM"] . "</td></tr>"; }
Displaying 0/1 as no/yes from MYSQL with PHP
I have a mysql table with a row that is either going to be a 0 or a 1. However, I want to be able to display the table with PHP and have 0 show up as no, and 1 show up as yes. I am still a beginner with PHP and have been searching for a way to do it, but have had no luck. The row in question is the 'masterwork' row. The line of code I thought would do it is $row['masterwork'] = ( intval( $row['masterwork']) == 1) ? "YES" : "NO"; Here is the code that displays the table: <?php $con=mysqli_connect("localhost","username","password","database"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM weapons"); $row['masterwork'] = ( intval( $row['masterwork']) == 1) ? "YES" : "NO"; while($row = mysqli_fetch_array($result)) { echo "<center>"; echo "<table border='1' class='display'>"; echo "<tr>"; echo "<td>Weapon Name: </td>"; echo "<td>" . $row['weaponName'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Creator: </td>"; echo "<td>" . $row['creator'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Weapon Category: </td>"; echo "<td>" . $row['weaponCategory'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Weapon Sub-Category: </td>"; echo "<td>" . $row['weaponSubCategory'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Cost: </td>"; echo "<td>" . $row['costAmount'] . " " . $row['costType'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Damage(S): </td>"; echo "<td>" . $row['damageS'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Damage(M): </td>"; echo "<td>" . $row['damageM'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Critical: </td>"; echo "<td>" . $row['critical'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Range Increment: </td>"; echo "<td>" . $row['rangeIncrement'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Weight: </td>"; echo "<td>" . $row['weight'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Weapon Type: </td>"; echo "<td>" . $row['weaponType'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Masterwork: </td>"; echo "<td>" . $row['masterwork'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Special Abilities: </td>"; echo "<td>" . $row['specialAbilities'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Additional Info: </td>"; echo "<td>" . $row['additionalInfo'] . "</td>"; echo "</tr>"; } echo "</table>"; echo "</center>"; mysqli_close($con); ?>
try this -- $result = mysqli_query($con,"SELECT * FROM weapons"); while($row = mysqli_fetch_array($result)) { $row['masterwork'] = ( intval( $row['masterwork']) == 1) ? "YES" : "NO"; echo "<center>"; echo "<table border='1' class='display'>"; echo "<tr>"; echo "<td>Weapon Name: </td>"; echo "<td>" . $row['weaponName'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Creator: </td>"; echo "<td>" . $row['creator'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Weapon Category: </td>"; echo "<td>" . $row['weaponCategory'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Weapon Sub-Category: </td>"; echo "<td>" . $row['weaponSubCategory'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Cost: </td>"; echo "<td>" . $row['costAmount'] . " " . $row['costType'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Damage(S): </td>"; echo "<td>" . $row['damageS'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Damage(M): </td>"; echo "<td>" . $row['damageM'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Critical: </td>"; echo "<td>" . $row['critical'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Range Increment: </td>"; echo "<td>" . $row['rangeIncrement'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Weight: </td>"; echo "<td>" . $row['weight'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Weapon Type: </td>"; echo "<td>" . $row['weaponType'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Masterwork: </td>"; echo "<td>" . $row['masterwork'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Special Abilities: </td>"; echo "<td>" . $row['specialAbilities'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>Additional Info: </td>"; echo "<td>" . $row['additionalInfo'] . "</td>"; echo "</tr>"; } echo "</table>"; echo "</center>"; mysqli_close($con); ?>