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 am retrieving posts from my database but I want the <div class="welcome-text"><strong>This is my second output</strong> </div> to display below the retrieved table but its always displaying above. is there any way I can fix this:
<?php
while($posts_row = mysql_fetch_assoc($posts_result))
{
echo '
<tr class="topic-post">
<td class="user-post">' . $posts_row['user_name'] . '<br/>' . date('d-m-Y H:i', strtotime($posts_row['post_date'])) . '</td>
<td class="post-content">' . htmlentities(stripslashes($posts_row['post_content'])) . '</td>
</tr>
';
}
?>
<div class="welcome-text"><strong>This is my second output</strong> </div>
Would use mysqli or prepared statement soon as I get this fixed
You din't close table tag before open div tag.
<table>
<?php
while($posts_row = mysql_fetch_assoc($posts_result))
{
echo '
<tr class="topic-post">
<td class="user-post">' . $posts_row['user_name'] . '<br/>' . date('d-m-Y H:i', strtotime($posts_row['post_date'])) . '</td>
<td class="post-content">' . htmlentities(stripslashes($posts_row['post_content'])) . '</td>
</tr>
';
}
?>
</table>
<div class="welcome-text"><strong>This is my second output</strong> </div>
I have a php file(add_member.php). I'm creating a dynamic table by executing SQL query. The code of this file is as follows:
$sql = SELECT * FROM member_details WHERE lname='ABC';
$n = new data();
$res = $n -> querySend($sql);
?>
<table border="1" cellpadding="3" cellspacing="1">
<tr>
<td align="center"></td>
<td align="center">First Name</td>
<td align="center">Last Name</td>
<td align="center">Caste</td>
<td align="center">Residence address</td>
<td align="center">Education</td>
</tr>
<?php
$i=1;
while($row = mysql_fetch_array($res))
{
$member_no = $row['member_no'];
$total_member = "SELECT COUNT(*) AS total_member FROM family_member_details WHERE member_no =" .$member_no;
$total_res = $n -> querySend($total_member);
$row_total = mysql_fetch_array($total_res);
?>
<tr>
<td align="center" rowspan="<?php echo $row_total['total_member']+1;?>"><?php echo $i;?></td>
<td align="center"><?php echo $row['fname'];?></td>
<td align="center"><?php echo $row['lname'];?></td>
<td align="center"><?php echo $row['caste'];?></td>
<td align="center"><?php echo $row['residence_addr'];?></td>
<td align="center"><?php echo $row['education'];?></td>
</tr>
<?php
$family_sql = "SELECT * from family_member_details WHERE member_no = $member_no";
$family_res = $n -> querySend($family_sql);
while($row1 = mysql_fetch_array($family_res))
{
?>
<tr>
<td align="center"><?php echo $row1['name']?></td>
<td align="center"><?php echo $row1['name']?></td>
<td align="center"><?php echo $row1['name']?></td>
<td align="center"><?php echo $row1['name']?></td>
<td align="center"><?php echo $row1['name']?></td>
</tr>
<?php }
$i++;
} ?>
</table>
Now upon clicking on a button I want to create the same table into PDF file. For this purpose I decided to use TCPDF library. But for it I've to provide the HTML content to TCPDF file. Now my issue is how should I get the HTML of a dynamically generated table from PHP file and write this content to the text file? Can anyone please help me in this regard? Any help would be highly appreciated.
Instead of displaying your table directly to the browser page, simply store the text in a variable and echo it...then you can send the var to the TCPDF library.
$dyn_table = '<table border="1" cellpadding="3" cellspacing="1"><tr><td align="center">/td><th align="center">First Name</th><th align="center">Last Name</th><th align="center">Caste</th><th align="center">Residence address</th><th align="center">Education</th></tr>';
$i = 1;
while ($row = mysql_fetch_array($res)) {
$member_no = $row['member_no'];
$total_member = "SELECT COUNT(*) AS total_member FROM family_member_details WHERE member_no =" . $member_no;
$total_res = $n->querySend($total_member);
$row_total = mysql_fetch_array($total_res);
$dyn_table .= '<tr><td align="center" rowspan="' . $row_total['total_member'] + 1 . '">' . $i . '</td><td align="center">' . $row['fname'] . '</td><td align="center">' . $row['lname'] . '</td><td align="center">' . $row['caste'] . '</td><td align="center">' . $row['residence_addr'] . '</td><td align="center">' . $row['education'] . '</td></tr>';
$family_sql = "SELECT * from family_member_details WHERE member_no = $member_no";
$family_res = $n->querySend($family_sql);
while ($row1 = mysql_fetch_array($family_res)) {
$dyn_table .= '<tr><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td></tr>';
}
$i++;
}
$dyn_table .= '</table>';
echo $dyn_table;
EDIT
In order to post this html to your TCPDF library, I would use AJAX to prevent another page request/load. I prefer to use JQuery as it simplifies this process immensely. Here is one way you could do it:
<input type="button" name="TCPDF" id="submitToTCPDF" />
<script type="text/javascript">
var url = 'php/script/to/handle/post';
var data = {'table_html': '<? echo $dyn_table; ?>'};
$('#TCPDF').click(function(){
$.ajax({
type: "POST",
url: url,
data: data,
success: function($result){
// Do whatever after html is submitted
}
});
});
</script>
You can read more about Jquery's AJAX post method in this StackOverflow question.