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 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 2 table in my db one the details and result. I display all the data from details table and do some calculation of data and displayed it.
I set a button to add the value(calculation value)into the result table and need to give a name of a result(i use popup window) But it not work.
My problem is how can i detect the name in php and insert it in by using php?
Besides that i also cannot add result in result table
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>
function enterMonth()
{
window.prompt("Please enter month and year of result","(mm-yyyy)");
}
</script>
</head>
<body>
<p>
<?php
$con = new PDO("mysql:host=localhost;dbname=gcmes", "root", "");
$sql = $con->query("SELECT * FROM details");
echo"<table class='info' align='center'>";
echo"<tr><td width='5'><b>Item</b></td>
<td width='75'><b>Category</b></td>
<td width='160'><b>Job</b></td>
<td width='45'><b>Frequency</b></td>
<td width='4' align='center'><b>H1</b></td>
<td width='4'><b>H2</b></td>
<td width='4'><b>H3</b></td>
<td width='4'><b>H4</b></td>
<td width='4'><b>H5</b></td>
<td width='4'><b>H6</b></td>
<td width='4'><b>H7</b></td>
<td width='4'><b>H8</b></td>
<td width='4'><b>H9</b></td>
<td width='4'><b>H10</b></td>
<td width='4'><b>H11</b></td>
<td width='4'><b>H12</b></td>
<td width='4'><b>H13</b></td>
<td width='4'><b>H14</b></td>
<td width='4'><b>H15</b></td>
<td width='4'><b>H16</b></td>
<td width='4'><b>H17</b></td>
<td width='4'><b>H18</b></td>
<tr>";
foreach($sql as $row) {
$Item = $row["Item"];
$Category = $row["Category"];
$Job = $row["Job"];
$Frequency = $row["Frequency"];
$Hole1 = $row["Hole1"];
$Hole2 = $row["Hole2"];
$Hole3 = $row["Hole3"];
$Hole4 = $row["Hole4"];
$Hole5 = $row["Hole5"];
$Hole6= $row["Hole6"];
$Hole7 = $row["Hole7"];
$Hole8 = $row["Hole8"];
$Hole9 = $row["Hole9"];
$Hole10 = $row["Hole10"];
$Hole11 = $row["Hole11"];
$Hole12 = $row["Hole12"];
$Hole13 = $row["Hole13"];
$Hole14 = $row["Hole14"];
$Hole15 = $row["Hole15"];
$Hole16 = $row["Hole16"];
$Hole17 = $row["Hole17"];
$Hole18 = $row["Hole18"];
echo'
<tr>
<td>' . $Item . '</td>
<td>' . $Category . '</td>
<td>' . $Job . '</td>
<td>' . $Frequency . '</td>
<td align=center>' . $Hole1 . '</td>
<td align=center>' . $Hole2 . '</td>
<td align=center>' . $Hole3 . '</td>
<td align=center>' . $Hole4 . '</td>
<td align=center>' . $Hole5 . '</td>
<td align=center>' . $Hole6 . '</td>
<td align=center>' . $Hole7 . '</td>
<td align=center>' . $Hole8 . '</td>
<td align=center>' . $Hole9 . '</td>
<td align=center>' . $Hole10 . '</td>
<td align=center>' . $Hole11 . '</td>
<td align=center>' . $Hole12 . '</td>
<td align=center>' . $Hole13 . '</td>
<td align=center>' . $Hole14 . '</td>
<td align=center>' . $Hole15 . '</td>
<td align=center>' . $Hole16 . '</td>
<td align=center>' . $Hole17 . '</td>
<td align=center>' . $Hole18 . '</td>
</tr>
';
}
?>
<?php
$conn=mysqli_connect('localhost','root','','gcmes');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Job FROM details";
if ($result=mysqli_query($conn,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
$totalmarks= ($rowcount*5);
}
$add = mysqli_query($conn,'SELECT SUM(Hole1),SUM(Hole2),SUM(Hole3),SUM(Hole4),SUM(Hole5),SUM(Hole6),SUM(Hole7),SUM(Hole8),SUM(Hole9),SUM(Hole10),SUM(Hole11),SUM(Hole12),SUM(Hole13),SUM(Hole14),SUM(Hole15),SUM(Hole16),SUM(Hole17),SUM(Hole18) from `details`');
while($row1=mysqli_fetch_array($add))
{
$mark1=number_format(((($row1['SUM(Hole1)'])/$totalmarks)*100), 2, '.', ' ');
$mark2=number_format(((($row1['SUM(Hole2)'])/$totalmarks)*100), 2, '.', ' ');
$mark3=number_format(((($row1['SUM(Hole3)'])/$totalmarks)*100), 2, '.', ' ');
$mark4=number_format(((($row1['SUM(Hole4)'])/$totalmarks)*100), 2, '.', ' ');
$mark5=number_format(((($row1['SUM(Hole5)'])/$totalmarks)*100), 2, '.', ' ');
$mark6=number_format(((($row1['SUM(Hole6)'])/$totalmarks)*100), 2, '.', ' ');
$mark7=number_format(((($row1['SUM(Hole7)'])/$totalmarks)*100), 2, '.', ' ');
$mark8=number_format(((($row1['SUM(Hole8)'])/$totalmarks)*100), 2, '.', ' ');
$mark9=number_format(((($row1['SUM(Hole9)'])/$totalmarks)*100), 2, '.', ' ');
$mark10=number_format(((($row1['SUM(Hole10)'])/$totalmarks)*100), 2, '.', ' ');
$mark11=number_format(((($row1['SUM(Hole11)'])/$totalmarks)*100), 2, '.', ' ');
$mark12=number_format(((($row1['SUM(Hole12)'])/$totalmarks)*100), 2, '.', ' ');
$mark13=number_format(((($row1['SUM(Hole13)'])/$totalmarks)*100), 2, '.', ' ');
$mark14=number_format(((($row1['SUM(Hole14)'])/$totalmarks)*100), 2, '.', ' ');
$mark15=number_format(((($row1['SUM(Hole15)'])/$totalmarks)*100), 2, '.', ' ');
$mark16=number_format(((($row1['SUM(Hole16)'])/$totalmarks)*100), 2, '.', ' ');
$mark17=number_format(((($row1['SUM(Hole17)'])/$totalmarks)*100), 2, '.', ' ');
$mark18=number_format(((($row1['SUM(Hole18)'])/$totalmarks)*100), 2, '.', ' ');
?>
<tr>
<th colspan="4">Total (%)</th>
<th><?php echo $mark1 ?></th>
<th><?php echo $mark2 ?></th>
<th><?php echo $mark3 ?></th>
<th><?php echo $mark4 ?></th>
<th><?php echo $mark5 ?></th>
<th><?php echo $mark6 ?></th>
<th><?php echo $mark7 ?></th>
<th><?php echo $mark8 ?></th>
<th><?php echo $mark9 ?></th>
<th><?php echo $mark10 ?></th>
<th><?php echo $mark11 ?></th>
<th><?php echo $mark12 ?></th>
<th><?php echo $mark13 ?></th>
<th><?php echo $mark14 ?></th>
<th><?php echo $mark15 ?></th>
<th><?php echo $mark16 ?></th>
<th><?php echo $mark17 ?></th>
<th><?php echo $mark18 ?></th>
</tr>
<?php }
echo"</table>";
?>
</p>
<p><div align="center"><form action="ResultHole.php" method="post"><input type="submit" onClick="enterMonth()" value="Submit" name="Submit_btn" id="Submit_btn"></form></div></p>
<?php
// Create connection
$conn=mysqli_connect('localhost','root','','gcmes');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (mysqli_query($conn, $sql)) {
if(isset($_POST['Submit_btn'])){
$sql = "INSERT INTO result (date,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15,h16,h17,h18) VALUES(?,'$mark1','$mark2','$mark3','$mark4','$mark5','$mark6','$mark7','$mark8','$mark9','$mark10','$mark11','$mark12','$mark13','$mark14','$mark15','$mark16','$mark17','$mark18')";
echo "<script type='text/javascript'>alert('New record created successfully');
window.location.href = 'ResultHole.php';
</script>";
}
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
</body>
</html>
What is the problem? Anyone can help? Thank you!
I create a table and display entries from MySQL in a brief table report format. Each of the entries in the table have further properties that I would like to display on a separate page when I click on the link in their names.
<?php
require_once 'includes/db.php';
connect();
$query = "SELECT * FROM persons order by date_entered DESC LIMIT 5";
$response = mysqli_query($con, $query);
if($response){
echo '<div id="frm"> <h2> Last 5 candidates entered </h2> <table align="left">
<tr>
<td align="left"><b>First Name</b></td>
<td align="left"><b>Email</b></td>
<td align="left"><b>Sex</b></td>
<td align="left"><b>City</b></td>
<td align="left"><b>Phone Number</b></td>
<td align="left"><b>Education</b></td>
<td align="left"><b>Salary</b></td>
</tr>';
while($row = mysqli_fetch_array($response)){
echo '<tr>
<td align="left">' . '' . $row["first_name"] . " " .$row['last_name'] . '' . '</td>
<td align="left">' . $row['email'] . '</td>
<td align="left">' . $row['sex'] . '</td>
<td align="left">' . $row['city'] . '</td>
<td align="left">' . $row['phone'] . '</td>
<td align="left">' . $row['education'] . '</td>
<td align="left">' . $row['salary'] . '</td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "Couldn't issue database query<br />";
echo mysqli_error($con);
}
mysqli_close($con);
?>
This gives me a table report with links in the names:
brief report with link in names
My question is: if I click Mary Swanson in the table, what variables do I use in person.php so I can retrieve the MySQL data for that record?
Thanks.
You could pass this id trough the link as a GET variable and use it on the other side to query the database for that specific record like this:
<?php
require_once 'includes/db.php';
connect();
$query = "SELECT * FROM persons order by date_entered DESC LIMIT 5";
$response = mysqli_query($con, $query);
if($response){
echo '<div id="frm"> <h2> Last 5 candidates entered </h2> <table align="left">
<tr>
<td align="left"><b>First Name</b></td>
<td align="left"><b>Email</b></td>
<td align="left"><b>Sex</b></td>
<td align="left"><b>City</b></td>
<td align="left"><b>Phone Number</b></td>
<td align="left"><b>Education</b></td>
<td align="left"><b>Salary</b></td>
</tr>';
while($row = mysqli_fetch_array($response)){
echo '<tr>
<td align="left">' . '' . $row["first_name"] . " " .$row['last_name'] . '' . '</td>
<td align="left">' . $row['email'] . '</td>
<td align="left">' . $row['sex'] . '</td>
<td align="left">' . $row['city'] . '</td>
<td align="left">' . $row['phone'] . '</td>
<td align="left">' . $row['education'] . '</td>
<td align="left">' . $row['salary'] . '</td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "Couldn't issue database query<br />";
echo mysqli_error($con);
}
mysqli_close($con);
?>
Then you can access the variable on the other side like this $_GET["id"])
<?php
require_once 'includes/db.php';
connect();
$query = "SELECT * FROM persons where id = " . $_GET["id"]);
$response = mysqli_query($con, $query)
After this you can print the values returned by the query as if where a multiple row select, but it will only come back with one row, you still need to do the while since what it returns is an array.
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.