I have two columns with different values:
And I need to echo those two values into one column, like that:
Printing method:
if($result->num_rows > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '<tr>
<td scope="row">' . $row["id"].'</td>
<td>' . $row["data"] .'</td>
<td>' . $row["ip"] .'</td>
</tr>';
}
}
Any suggestions on how I could do that?
Do you mean like this
echo '<tr>
<td scope="row">' . $row["id"].'</td>
<td>' . $row["data"] . '(' . $row["ip"] . ')</td>
</tr>';
Merge the two cells:
<td>' . $row["data"] .'</td>
<td>' . $row["ip"] .'</td>
Into one:
<td>' . $row["data"] . '(' . $row["ip"] . ')</td>
Related
When my data is outputted from my server onto a website it outputs it onto 1 row instead of going down a row and outputting the next row of data
!
session_start();
$view = new stdClass();
$view->pageTitle = 'Camp Sites';
require_once('views/CampSites.phtml');
require_once('Models/campData.php');
require_once('Models/campDataSet.php');
$campDataSet = new campDataSet();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$search = $_POST['search'];
$_SESSION['search'] = $search;
$sqlQuery = "SELECT * FROM campsites ='$search'";
$result = $campDataSet->fetchAllCamps($sqlQuery);
if (count($result) > 0) {
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
foreach ($result as $row) {
echo '<td>' . $row->id_campsite. '</td> <td>' . $row->campsite_name . '</td> <td>' . $row->address . '</td> <td>' . $row->postcode . '</td> <td>' . $row->country. '</td> <td>' . $row->lattitude . '</td> <td>' . $row->longitude . '</td> <td>' . $row->email . '</td> <td>' . $row->phone_number . '</td> </td>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
}
if (count($result) > 0) {
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
foreach ($result as $row) {
echo '<tr><td>' . $row->id_campsite. '</td> <td>' . $row->campsite_name . '</td> <td>' . $row->address . '</td> <td>' . $row->postcode . '</td> <td>' . $row->country. '</td> <td>' . $row->lattitude . '</td> <td>' . $row->longitude . '</td> <td>' . $row->email . '</td> <td>' . $row->phone_number . '</td> </td></tr>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
You need to wrap tags around the code in your foreach like above.
i'm trying to retrieve information from a database and put it into a table in PHP and HTML however, i've encountered a problem where it says ive got an error:
Fatal error: Uncaught Error: Function name must be a string in
F:\Client Server\Website\campsites.php:41 Stack trace: #0 {main}
thrown
when I check the line that the error is in it says :
Method 'fetch_assoc' not found in array
How can I fix this?
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$search = $_POST['search'];
$_SESSION['search'] = $search;
$sqlQuery = "SELECT * FROM campsites ='$search'";
$result = $campDataSet->fetchAllCamps($sqlQuery);
if($result > 0){
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
while($row = $result->fetch_assoc()){
echo '<td>' . $row['id_campsite']. '</td> <td>' . $row['campsite_name'] . '</td> <td>' . $row['address'] . '</td> <td>' . $row['postcode']. '</td> <td>' . $row['country']. '</td> <td>' . $row['lattitude']. '</td> <td>' . $row['longitude']. '</td> <td>' . $row['email']. '</td> <td>' . $row['phone_number']. '</td> </td>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
}
EDIT :
var_dump($result);
array(1) { [0]=> object(DBdata)#6 (11) { ["db"]=> NULL ["conn"]=> object(PDO)#7 (0) { }
["id_campsite"]=> string(1) "1" ["campsite_name"]=> string(13) "lake district" ["address"]=> string(32) "lake distrcit greater manchester"
["postcode"]=> string(7) "m21 8fx" ["country"]=> string(7) "England" ["lattitude"]=> string(13) "1 0800 243230" ["longitude"]=> string(12) "120312310978"
["email"]=> string(14) "lake#gmail.com" ["phone_number"]=> string(12) "059874327823" } }
One possible reason for this error is that $campDataSet->fetchAllCamps($sqlQuery) returns an array. In this case your code should look like this:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$search = $_POST['search'];
$_SESSION['search'] = $search;
$sqlQuery = "SELECT * FROM campsites ='$search'";
$result = $campDataSet->fetchAllCamps($sqlQuery);
if (count($result) > 0) {
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
foreach ($result as $row) {
echo '<td>' . $row['id_campsite']. '</td> <td>' . $row['campsite_name'] . '</td> <td>' . $row['address'] . '</td> <td>' . $row['postcode']. '</td> <td>' . $row['country']. '</td> <td>' . $row['lattitude']. '</td> <td>' . $row['longitude']. '</td> <td>' . $row['email']. '</td> <td>' . $row['phone_number']. '</td> </td>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
}
?>
Update:
Your $campDataSet->fetchAllCamps returns an array of objects. This is more appropriate in this case:
<?php
...
foreach ($result as $row) {
echo '<td>' . $row->id_campsite. '</td> <td>' . $row->campsite_name . '</td> <td>' . $row->address . '</td> <td>' . $row->postcode . '</td> <td>' . $row->country. '</td> <td>' . $row->lattitude . '</td> <td>' . $row->longitude . '</td> <td>' . $row->email . '</td> <td>' . $row->phone_number . '</td> </td>';
}
...
?>
Assuming that fetchAllCamps() does the fetching for you, this is why you have an array rather than a result set, so instead of the line...
while($row = $result->fetch_assoc()){
replace it with
foreach ( $result as $row ) {
I am presently working on a project in php mysql to display content from db with edit option against each row. In the below is the code, I am not able to return any value only for the first row, Rest of the row buttons are working fine. I am not able to figure out what is issue. It would be great if you could help me out.
Code snippet of the table display page
$sql1 = "SELECT * FROM `computation` WHERE `compute_month` = '$month' and `compensation_type` = '$allowance' and order by `emp_id` ASC";
}
$result1 = mysqli_query($con, $sql1);
echo "
<form action='confirmallowance.php' method='POST'>
<table class=data-table border='1' overflow-x=auto>
<caption class=title><b>Allowance Sheet<b></caption>
<thead>
<tr>
<th>Date</th>
<th>Allowance Month</th>
<th>Attendance Date</th>
<th>Day</th>
<th>Employee Code</th>
<th>Employee Name</th>
<th>InTime</th>
<th>OutTime</th>
<th>Work Duration</th>
<th>Team</th>
<th>Approver</th>
<th>Allowance type</th>
<th>Eligibility</th>
<th>Approved_amount</th>
<th>Status</th>
</tr>
</thead>
<tbody>";
if (mysqli_num_rows($result1) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result1)) {
echo '
<tr id="' . $row['id'] . '">
<td>' . $row['computation_date'] . '</td>
<td>' . $row['compute_month'] . '</td>
<td>' . $row['attendance_date'] . '</td>
<td>' . $row['attend_day'] . '</td>
<td>' . $row['emp_id'] . '</td>
<td>' . $row['emp_name'] . '</td>
<td>' . $row['time_in'] . '</td>
<td>' . $row['time_out'] . '</td>
<td>' . $row['duration'] . '</td>
<td>' . $row['team'] . '</td>
<td>' . $row['manager'] . '</td>
<td>' . $row['compensation_type'] . '</td>
<td>' . $row['eligibility'] . '</td>
<td>' . $row['amount'] . '</td>
<td>' . $row['status'] . '</td>
<td><input type="checkbox" name="checkbox[]" value="' . $row['id'] . '" checked="checked"></td>
<td>
<form action="editallowance.php" method="POST"><input type="hidden" name="tempid"
value="' . $row['id'] . '"/><input type="submit"
name="submit"
value="Edit"/>
</form>
</td>
</tr>
';
}
}
// Code snippet of the table value acceptance page on edit button click
if (isset($_POST["submit"]) && $_POST["submit"]) {
$editrow = $_POST['tempid'];
$sql = "SELECT * FROM `computation` WHERE `id` ='$editrow'";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$att_date = $row['attendance_date'];
$att_day = $row['attend_day'];
$emp_id = $row['emp_id'];
$emp_name = $row['emp_name'];
$comptype = $row['compensation_type'];
$eligibility = $row['eligibility'];
$comment = $row['comment'];
$status = $row['status'];
echo "<P>Emp ID : " . $emp_id . "</p> <p>Emp Name :" . $emp_name . "</p><p>Attendance Date : " . $att_date . "</p><p>Day : " . $att_day . "</p>";
}
}
} else {
echo "No data";
If you need any addition info, please let me know.
Your html is not well formed. You are adding unused form open tag before table opening tag (there is not closing tag of it as well) in the first file:
<form action='confirmallowance.php' method='POST'>
<table class=data-table border='1' overflow-x=auto>
Please remove this form open tag html and it will work fine. It is interfering with the html forms that come later.
I have a page which displays some data imported in MySQL database from Excel file
I want to change background color of rows with same values (grouped) on col "Billing Doc" like this picture:
Here is a part of my php code:
<form action="uploadExcel.php" method="POST" enctype="multipart/form-data">
<div>
<input type="file" name="file">
<input type= "submit" value ="Upload" >
</div>
</form>
<table width="100%" border="1" cellpadding="5">
<tr>
<th>ID</th>
<th>Billing Doc</th>
<th>Invoice Date</th>
<th>Ordered Parts</th>
<th>Shipped Parts</th>
<th>Qty</th>
<th>F</th>
<th>G</th>
<th>Amount</th>
<th>Delivery No</th>
<th>D/O Creation Date</th>
<th>Description</th>
<th>P/O No</th>
<th>Ship-to</th>
<th>Tracking No</th>
<th>Obs</th>
</tr>
<?php
while ($row = $result->fetch_object()) {
if (substr( $row->tracking_no, 0, 3 ) === "534") {
$row->tracking_no = "<a href='http://www.dhl.be/en/express/tracking.html?pageToInclude=RESULTS&AWB=$row->tracking_no&type=fasttrack' title='DHL'>$row->tracking_no</a>";
}
if (substr( $row->tracking_no, 0, 3 ) === "730") {
$row->tracking_no = "<a href='http://www.tnt.com/webtracker/tracker.do?navigation=1&respLang=en&respCountry=gb&cons=$row->tracking_no' title='TNT'>$row->tracking_no</a>";
}
$colour = '';
echo '
<tr style="background-color:' .$colour. '">
<td>' . $row->id_factura . '</td>
<td>' . $row->billing_doc . '</td>
<td>' . $row->invoice_date . '</td>
<td>' . $row->ordered_parts . '</td>
<td>' . $row->shipped_parts . '</td>
<td>' . $row->qty . '</td>
<td>' . number_format($row->F, 2) . '</td>
<td>' . number_format($row->G, 2) . '</td>
<td>' . number_format($row->amount, 2) . '</td>
<td>' . $row->delivery_no . '</td>
<td>' . $row->d_o_creation_date . '</td>
<td>' . $row->description . '</td>
<td>' . $row->po_no . '</td>
<td>' . $row->ship_to . '</td>
<td>' . $row->tracking_no . '</td>
<td>' . $row->obs . '</td>
</tr>
';
}
?>
</table>
It seems that the only variable to consider is Billing Doc, if so, inside your while loop use an array to store the billing id as a key and then check every value to see if the key already exists then print the color you want to:
$billing_docs = [];
while ($row = $result->fetch_object()) {
echo '<tr style="background-color:' . (array_key_exists($row->id_factura, $billing_docs) ? '#666' : '#DDD') . '">...';
$billing_docs[$row->id_factura] = '';
}
You could create a variable with the first billing_doc value: $billing = $row->billing_doc and use an if else to echo the same color row if the $billing_doc value is the same. The else would declare the new color variable and echo rows of that color.
This is error appears when I try to open the exported file, when I click yes the file opens but has error above the table:
<?php require '../backend/dbcon.php';
$query=mysql_query( "SELECT * FROM membership");
$total=mysql_num_rows($query);
if ($total>0) { $output .= '
<table border="1">
<tr>
<th>NO</th>
<th>Names</th>
<th>Email</th>
<th>Phone</th>
<th>Amount(N$)</th>
<th>Payment</th>
</tr>'; while ($row = mysql_fetch_array($query)) { $output .= '
<tr>
<td>' . $row["id"] . '</td>
<td>' . $row["names"] . '</td>
<td>' . $row["personal_email"] . '</td>
<td>' . $row["personal_phone"] . '</td>
<td>' . 110 . '</td>
<td>' . $row["payment"] . '</td>
</tr>
'; } $output .= '
</table>';
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=Membe.xls"); echo $output; }