php-mysql carriage returns - php
I am relatively new to php/mysql. I am trying to do carriage returns for this part:
echo "<td colspan='5'>" . nl2br($row['dish_description']) . "</td>";
"dish_description" contains some text which includes "\n"
e.g. "Served with salad and mint sauce. \n Contains beef." In the MySQL database
however I cannot display the carriage return.
<?php
require_once 'login.php';
$con = mysql_connect($db_hostname, $db_username, $db_password);
mysql_select_db($db_database) or die("Unable to select database". mysql_error());
$result = mysql_query("SELECT * FROM `dishes` ORDER BY `dishes`.`dish_id` ASC LIMIT 0 , 200");
echo '<table align="center">
';
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td style='width: 60px'></td>";
echo "<th style='width: 100px'>Dish No</th>";
echo "<td style='width: 70px'>" . $row['dish_id'] . "</td>";
echo "<th style='width: 100px'>Dish Name</th>";
echo "<td style='width: 120px'>" . $row['dish_name'] . "</td>";
echo "<th style='width: 120px'>Dish Price</th>";
echo "<td>£" . $row['dish_price'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 75px'></td>";
echo "<th style='width: 100px'>Vegetarian</th>";
echo "<td style='width: 70px'>" . $row['dish_vegetarian'] ."</td>";
echo "<td style='width: 100px'></td>";
echo "<td style='width: 70px'></td>";
echo "<th style='width: 120px'>Contains Nuts</th>";
echo "<td style='width: 120px'>" . $row['dish_nuts'] ."</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 75px'></td>";
echo "<th style='width: 100px'>Information</th>";
***echo "<td colspan='5'>" . nl2br($row['dish_description']) . "</td>";***
echo "</tr>";
echo "<tr height='10px'>";
echo "<td style='width: 75px'></td>";
echo "<td style='width: 100px'></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
In my opinion if you echo it as HTML you can just add <br> tags to your records in database.
Related
PHP / JSON: How do I know if JSON data is empty or not?
Currently, I create a table that displays data from the database (JSON). In that table, I want to check is the data exists or not. Let's say if data is empty, I want to display "No booking data available at these moments". The problem is, I don't know how to check the existing data. For now, if the data is empty, the table appears with <th> only and no words "No booking data available at these moments". Below is my current code: <?php //retrieve json $url = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayAdminBookingDashboard?adminEmail=$Email"; $data = file_get_contents($url); $json = json_decode($data); if(empty($json)){ echo "<div class='card bg-light'>"; echo "<div class='card-body double' style='height: 400px;>"; echo "<h4 class='card-title'><i>No booking data available at this moments</i></h4>"; }else{ echo "<div class='card bg-light'>"; echo "<div class='card-body double' style='height: 400px; overflow-y: scroll;'>"; echo "<h4 class='card-title'>All Booking</h4>"; echo "<table>"; echo "<thead>"; echo "<tr>"; echo "<th>#</th> <th>Requester</th> <th>Factory</th> <th>Room</th> <th>Purpose</th> <th>Status</th> <th>Action</th> </tr> </thead> <tbody >"; foreach ($json->bookingList as $row) { $status=$row->status; if($status=="Approve"){ $color="color:green"; }else if($status=="Pending"){ $color="color:blue"; }else{ $color="color:red"; } echo "<tr>"; echo "<td>" . $row->bookNo. "</td>"; echo "<td>" . $row->requestedBy. "</td>"; echo "<td>" . $row->facID. "</td>"; echo "<td>" . $row->roomName. "</td>"; echo "<td>" . $row->desc. "</td>"; echo "<td style='$color'><strong>" . $status ."</strong></td>"; echo "<td>"; echo "<a class='btn-view btn-primary btn-sm' href='../../view_booking/admin/view_booking_admin.php?Book_No=". $row->bookNo ."' data-toggle='tooltip'>View</a>"; echo "</td>"; echo "</tr>"; } echo "</tbody>"; echo "</table><br>"; echo "</div>"; echo "<div>"; } ?>
Try the following code: <?php //retrieve json $url = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayAdminBookingDashboard?adminEmail=$Email"; $data = #file_get_contents($url); $json = json_decode($data); if($json === FALSE && empty($json)){ echo "<div class='card bg-light'>"; echo "<div class='card-body double' style='height: 400px;>"; echo "<h4 class='card-title'><i>No booking data available at this moments</i></h4>"; }else{ echo "<div class='card bg-light'>"; echo "<div class='card-body double' style='height: 400px; overflow-y: scroll;'>"; echo "<h4 class='card-title'>All Booking</h4>"; echo "<table>"; echo "<thead>"; echo "<tr>"; echo "<th>#</th> <th>Requester</th> <th>Factory</th> <th>Room</th> <th>Purpose</th> <th>Status</th> <th>Action</th> </tr> </thead> <tbody >"; foreach ($json->bookingList as $row) { $status=$row->status; if($status=="Approve"){ $color="color:green"; }else if($status=="Pending"){ $color="color:blue"; }else{ $color="color:red"; } echo "<tr>"; echo "<td>" . $row->bookNo. "</td>"; echo "<td>" . $row->requestedBy. "</td>"; echo "<td>" . $row->facID. "</td>"; echo "<td>" . $row->roomName. "</td>"; echo "<td>" . $row->desc. "</td>"; echo "<td style='$color'><strong>" . $status ."</strong></td>"; echo "<td>"; echo "<a class='btn-view btn-primary btn-sm' href='../../view_booking/admin/view_booking_admin.php?Book_No=". $row->bookNo ."' data-toggle='tooltip'>View</a>"; echo "</td>"; echo "</tr>"; } echo "</tbody>"; echo "</table><br>"; echo "</div>"; echo "<div>"; } ?>
I already solved my question. I just add "bookingList" at the empty json <?php //retrieve json $url = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayAdminBookingDashboard?adminEmail=$Email"; $data = file_get_contents($url); $json = json_decode($data); if(empty($json->bookingList)){ echo "<div class='card bg-light'>"; echo "<div class='card-body double' style='height: 400px;>"; echo "<h4 class='card-title'><i>No booking data available at this moments</i></h4>"; }else{ echo "<div class='card bg-light'>"; echo "<div class='card-body double' style='height: 400px; overflow-y: scroll;'>"; echo "<h4 class='card-title'>All Booking</h4>"; echo "<table>"; echo "<thead>"; echo "<tr>"; echo "<th>#</th> <th>Requester</th> <th>Factory</th> <th>Room</th> <th>Purpose</th> <th>Status</th> <th>Action</th> </tr> </thead> <tbody >"; foreach ($json->bookingList as $row) { $status=$row->status; if($status=="Approve"){ $color="color:green"; }else if($status=="Pending"){ $color="color:blue"; }else{ $color="color:red"; } echo "<tr>"; echo "<td>" . $row->bookNo. "</td>"; echo "<td>" . $row->requestedBy. "</td>"; echo "<td>" . $row->facID. "</td>"; echo "<td>" . $row->roomName. "</td>"; echo "<td>" . $row->desc. "</td>"; echo "<td style='$color'><strong>" . $status ."</strong></td>"; echo "<td>"; echo "<a class='btn-view btn-primary btn-sm' href='../../view_booking/admin/view_booking_admin.php?Book_No=". $row->bookNo ."' data-toggle='tooltip'>View</a>"; echo "</td>"; echo "</tr>"; } echo "</tbody>"; echo "</table><br>"; echo "</div>"; echo "<div>"; } ?>
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...
Delete Row Using PHP Postgres
could someone help on below code? I have table data on that can allow user to click button to delete the row. I got error said " Undefined index: tenant_id " , Undefined variable: row. Below are delete.php and pg-t-payment-view.php script : <?php $db = pg_connect("host=10.0.32.204 port=5432 dbname=postgres user=postgres password=postgres"); $id = $_POST['tenant_id']; $sql2 ="DELETE FROM payment_ref_tenancy WHERE tenant_name = '$id'"; $result = pg_query($sql2); $cmdtuples = pg_affected_rows($result); echo $cmdtuples . " record affected.\n"; if (!$result) { $errormessage = pg_last_error(); echo "Error with query: " . $errormessage; exit(); } pg_close(); header('location:pg-t-payment-view.php'); ?> -------------pg-t-payment-view.php script <div class="box-body table-responsive no-padding"> <?php $db = pg_connect("host=10.0.32.204 port=5432 dbname=postgres user=postgres password=postgres"); $sql2 ="select tenant_id,to_char(last_update_time, 'MM-dd-yyyy HH24:MI') as last_update_time , tenant_name, tenant_cost_category, invoice_no,tenant_agreed_cost, to_char(submission_date, 'MM-dd-yyyy') as submission_date,to_char(cpr_submission_to_finance, 'MM-dd-yyyy') as cpr_submission_to_finance,io,cheque_no FROM payment_ref_tenancy order by insert_datetime desc limit 10"; $result = pg_query($db,$sql2); if (!$result) { $errormessage = pg_last_error(); echo "Error with query: " . $errormessage; exit(); } pg_close(); echo "<table class='table table-hover table-striped'>"; echo "<th align='center' >Date</th>"; echo "<th align='center' >Payee</th>"; echo "<th align='center' >Category</th>"; echo "<th align='center' >Cost (RM)</th>"; echo "<th align='center' >Invoice No</th>"; echo "<th align='center' >Payment Submission Date</th>"; echo "<th align='center'>CPR Submission to Finance</th>"; echo "<th align='center'>IO</th>"; echo "<th align='center'>Cheque No</th>"; echo "<th align='center' div style ='color:#ff0000'>Action</th>"; echo "<th align='center'></th>"; echo "<th align='center'></th>"; while($row=pg_fetch_assoc($result)) { $id = $row['tenant_id']; echo "<tr>"; echo "<td>" . $row['last_update_time'] . "</td>"; echo "<td>" . $row['tenant_name'] . "</td>"; echo "<td>" . $row['tenant_cost_category'] . "</td>"; echo "<td>" . $row['tenant_agreed_cost'] . "</td>"; echo "<td>" . $row['invoice_no'] . "</td>"; echo "<td>" . $row['submission_date'] . "</td>"; echo "<td>" . $row['cpr_submission_to_finance'] . "</td>"; echo "<td>" . $row['io'] . "</td>"; echo "<td>" . $row['cheque_no'] . "</td>"; echo "<td><a href='pg-t-payment-edit.php'>Edit</a></td>"; echo "<td><a href='delete.php?id=$id'><input type='hidden' name='id' value=$id>Delete</a></td>"; echo "</tr>";} echo "</table>"; ?>
Try to move pg_close(); in pg-t-payment-view.php somewhere after you have already worked with data fetched from database. So basically after this loop executes: while($row=pg_fetch_assoc($result)) Change name of argument you pass in your delete button to reflect column name or change in delete.php how you get said ID. <div class="box-body table-responsive no-padding"> <?php $db = pg_connect("host=10.0.32.204 port=5432 dbname=postgres user=postgres password=postgres"); $sql2 ="select tenant_id,to_char(last_update_time, 'MM-dd-yyyy HH24:MI') as last_update_time , tenant_name, tenant_cost_category, invoice_no,tenant_agreed_cost, to_char(submission_date, 'MM-dd-yyyy') as submission_date,to_char(cpr_submission_to_finance, 'MM-dd-yyyy') as cpr_submission_to_finance,io,cheque_no FROM payment_ref_tenancy order by insert_datetime desc limit 10"; $result = pg_query($db,$sql2); if (!$result) { $errormessage = pg_last_error(); echo "Error with query: " . $errormessage; pg_close(); exit(); } echo "<table class='table table-hover table-striped'>"; echo "<th align='center' >Date</th>"; echo "<th align='center' >Payee</th>"; echo "<th align='center' >Category</th>"; echo "<th align='center' >Cost (RM)</th>"; echo "<th align='center' >Invoice No</th>"; echo "<th align='center' >Payment Submission Date</th>"; echo "<th align='center'>CPR Submission to Finance</th>"; echo "<th align='center'>IO</th>"; echo "<th align='center'>Cheque No</th>"; echo "<th align='center' div style ='color:#ff0000'>Action</th>"; echo "<th align='center'></th>"; echo "<th align='center'></th>"; while($row=pg_fetch_assoc($result)) { $id = $row['tenant_id']; echo "<tr>"; echo "<td>" . $row['last_update_time'] . "</td>"; echo "<td>" . $row['tenant_name'] . "</td>"; echo "<td>" . $row['tenant_cost_category'] . "</td>"; echo "<td>" . $row['tenant_agreed_cost'] . "</td>"; echo "<td>" . $row['invoice_no'] . "</td>"; echo "<td>" . $row['submission_date'] . "</td>"; echo "<td>" . $row['cpr_submission_to_finance'] . "</td>"; echo "<td>" . $row['io'] . "</td>"; echo "<td>" . $row['cheque_no'] . "</td>"; echo "<td><a href='pg-t-payment-edit.php'>Edit</a></td>"; echo "<td><a href='delete.php?tenant_id=$id'><input type='hidden' name='id' value=$id>Delete</a></td>"; echo "</tr>";} pg_close(); echo "</table>"; ?> Change in your delete.php how you get and use ID. If your tenant_id is integer, you can omit quotes from DELETE query. <?php $db = pg_connect("host=10.0.32.204 port=5432 dbname=postgres user=postgres password=postgres"); $id = $_GET['tenant_id']; $sql2 ="DELETE FROM payment_ref_tenancy WHERE tenant_id = '$id'"; $result = pg_query($sql2); $cmdtuples = pg_affected_rows($result); echo $cmdtuples . " record affected.\n"; if (!$result) { $errormessage = pg_last_error(); echo "Error with query: " . $errormessage; exit(); } pg_close(); header('location:pg-t-payment-view.php'); ?> Just so you know, this code can be SQL Injected, this is security issue if untrusted people will use it.
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>";
Displaying php query in column instead of row
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>";