Having an issue with the output of records from a query run to display records.... It only shows the first row as the code specifies and then the next results all in.. paragraphs? I don't know if it has something to do
<?php
include 'core/init.php';
include 'includes/overall/header.php';
?>
<div class="article" style="width:900px !important">
<?php
$result = $sql = mysql_query("SELECT * FROM ref_employees WHERE employerid={$user_data['user_id']} ")
or die('Error in query : $sql. ' .mysql_error());
echo "<table border='0' class='table'>
<tr>
<th>ID Number</th>
<th>Employee Number</th>
<th>FirstName</th>
<th>LastName</th>
<th>MiddleName</th>
<th>Job Title</th>
<th>Employement Status</th>
<th>Contact</th>
<th>Email</th>
<th>Edit</th>
</tr>";
if (mysql_num_rows($sql) > 0)
{
while ($row = mysql_fetch_array($sql)){
if ($row['employed'] == '1'){
echo "<tr>";
echo "<td>" . $row['idnumber'] . "</td>";
echo "<td>" . $row['empnumber'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['middlename'] . "</td>";
echo "<td>" . $row['jobtitle'] . "</td>";
echo "<td>" . $row['employed'] . "</td>";
echo "<td>" . $row['contactnum'] . "</td>";
echo "<td>" . $row['contactemail'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "</tr>";
echo "</tr>";
echo "</table>";
}
}
}
?>
</div>
<?php include 'includes/overall/footer.php';
?>
You are using closing table tag into loop as
while ($row = mysql_fetch_array($sql)){
....
....
...
echo "</table>";
}
use table closing tag out of loop as
while ($row = mysql_fetch_array($sql)){
....
....
...
}
echo "</table>";
Related
I'm trying to create one table that will loop through all the records from my database but it seems to be create individual tables for each record instead.
$query = 'select * from images';
$result = mysqli_query($connection,$query);
$row_count = mysqli_num_rows($result);
for($i=0; $i<$row_count; $i++){
$row[] = mysqli_fetch_array($result);
}
echo '<header><h1 class="gallerytitle">Photo Gallery</h1></header>';
foreach($row as $next) {
{
echo "<table border='1'>
<tr>
<th>Imageid</th>
<th>Image name</th>
<th>Description</th>
<th>Image</th>
<th>Caption</th>
</tr>";
echo "<tr>";
echo "<td>" . $next['imageid'] . "</td>";
echo "<td>" . $next['imagename'] . "</td>";
echo "<td>" . $next['description'] . "</td>";
echo "<td><img src='".$next['image']."' width='20%' height='auto'></td>";
echo "<td>" . $next['caption'] . "</td>";
echo "<td> <a class='readmore' href='delete_confirm.php?imageid={$next['imageid']}'>Delete</a></td>";
echo "<td> <a class='readmore' href='update_form.php?imageid={$next['imageid']}'>Update</a></td>";
echo "</tr>";
}
echo "</table>";
echo '</section>';
}
Ideally, you would loop around the tr
echo "<table border='1'>
<tr>
<th>Imageid</th>
<th>Image name</th>
<th>Description</th>
<th>Image</th>
<th>Caption</th>
</tr>";
foreach($row as $next) {
{
echo "<tr>";
echo "<td>" . $next['imageid'] . "</td>";
echo "<td>" . $next['imagename'] . "</td>";
echo "<td>" . $next['description'] . "</td>";
// all the other columns
echo "</tr>";
}
echo "</table>";
Here is some short Code of table.I am using echo in table.
The data is not printing as first three fields.
echo "<table>";
echo "<tr>
<th>ID</th>
<th>Date</th>
<th>Reference</th>
</tr>" ;
while ($row = $result->fetch_object())
{
echo "<td>" . $row->id . "</td>";
echo "<td>" . $row->date . "</td>";
echo "<td>" . $row->ref . "</td>";
}
echo " <tr>
<th>First Name</th>
<th>Father Name</th>
<th>Phone</th>
</tr>";
while ($row = $result->fetch_object())
{
echo "<td>" . $row->name . "</td>";
echo "<td>" . $row->fname . "</td>";
echo "<td>" . $row->cell . "</td>";
}
echo "<tr>
<th>District</th>
<th>Address</th>
<th>Gender</th>
</tr>";
while ($row = $result->fetch_object())
{
echo "<td>" . $row->district . "</td>";
echo "<td>" . $row->address . "</td>";
echo "<td>" . $row->gender . "</td>";
}
echo "</table>";
Table Output Image
You have missed tr in every dynamic data. change your code as below:
echo "<table>";
echo "<tr>
<th>ID</th>
<th>Date</th>
<th>Reference</th>
</tr>" ;
while ($row = $result->fetch_object())
{
echo "<tr><td>" . $row->id . "</td>";
echo "<td>" . $row->date . "</td>";
echo "<td>" . $row->ref . "</td></tr>";
}
echo " <tr>
<th>First Name</th>
<th>Father Name</th>
<th>Phone</th>
</tr>";
while ($row = $result->fetch_object())
{
echo "<tr><td>" . $row->name . "</td>";
echo "<td>" . $row->fname . "</td>";
echo "<td>" . $row->cell . "</td></tr>";
}
echo "<tr>
<th>District</th>
<th>Address</th>
<th>Gender</th>
</tr>";
while ($row = $result->fetch_object())
{
echo "<tr><td>" . $row->district . "</td>";
echo "<td>" . $row->address . "</td>";
echo "<td>" . $row->gender . "</td></tr>";
}
echo "</table>";
I firstly did the table layout and made sure everything is working, after connecting the table to a database and trying to put the records inside it, everything worked perfectly, but the class didnt, i have now the boring table without the layout made.
<body>
<h1>Employees</h1>
<table class="responstable">
<?php
require 'connection.php';
$conn = Connect();
$result = mysqli_query($conn,"SELECT * FROM employee");
echo "<table border='1'>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Salary</th>
<th>Start Date</th>
<th>Department</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
<script src='http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script>
</body>
</html>
the table class is "responstable"
table tag is defined in 2 places.try removing this.
echo "<table border='1'>
or remove the table tag at the top after h1 tag and add the class to the table tag defined in the echo as,
complete code
<body>
<h1>Employees</h1>
<?php
require 'connection.php';
$conn = Connect();
$result = mysqli_query($conn,"SELECT * FROM employee");
echo "<table border='1' class='responstable'>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Salary</th>
<th>Start Date</th>
<th>Department</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
<script src='http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script>
</body>
</html>
The following code adds a Accept/Decline Button per row. But for some reason, the decline button does not appear. I dont know whats wrong. Been stuck for a while now. Help would be appreciated. Thanks!!
<?php
$con=mysqli_connect("localhost","root","","rabco");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM service_request");
echo "<table border='1'>
<tr>
<th>Service ID</th>
<th>Service Type</th>
<th>Schuduled Date</th>
<th>Scheduled Time</th>
<th>Client Reference
<th>Client ID</th>
<th>Admin ID</th>
<th>Special Instructions</th>
<th>Status</th>
<th>Approve</th>
<th>Decline</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Service_ID'] . "</td>";
echo "<td>" . $row['Service_type'] . "</td>";
echo "<td>" . $row['Sched_date'] . "</td>";
echo "<td>" . $row['Sched_time'] . "</td>";
echo "<td>" . $row['Client_reference'] . "</td>";
echo "<td>" . $row['Client_IDN'] . "</td>";
echo "<td>" . $row['Admin_IDN'] . "</td>";
echo "<td>" . $row['Special_instructions'] . "</td>";
echo "<td>" . $row['Request_status'] . "</td>";
echo "<td>Approve</td>";
echo "<td>Decline</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
You are missing the closing quotes on your href attributes, which is creating malformed HTML output.
You need to change:
echo "<td>Approve</td>";
echo "<td>Decline</td>";
to
echo "<td><a href='approve.php?Service_ID=".$row['Service_ID']."'>Approve</a></td>";
echo "<td><a href='decline.php?Service_ID=".$row['Service_ID']."'>Decline</a></td>";
There is a missing closing quote here:
echo "<td>Approve</td>";
Change to:
echo "<td><a href='approve.php?Service_ID=".$row['Service_ID']."'>Approve</a></td>";
I was hoping to get anyone's opinion on why this certain variable won't print out. Variable $sum from the $qsl_ query is not printing along with the other table row data.
<?php
$sql_ = "SELECT SUM(`points`) AS value_sum FROM `history` WHERE `userid` = '$id'";
$result = mysql_query($sql_);
#echo mysql_error();
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
$sql = "SELECT * FROM users";
$myData = mysql_query($sql);
echo "<table id=\"table\" class=\"table table-striped\">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Model</th>
<th>Year</th>
<th>Plate Number</th>
<th>City</th>
<th>Country</th>
<th>Points</th>
</tr></thead>";
while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['email'] . "</td>";
echo "<td>" . $record['firstName'] . "</td>";
echo "<td>" . $record['lastName'] . "</td>";
echo "<td>" . $record['model'] . "</td>";
echo "<td>" . $record['Year'] . "</td>";
echo "<td>" . $record['plateNumber'] . "</td>";
echo "<td>" . $record['city'] . "</td>";
echo "<td>" . $record['country'] . "</td>";
echo "<td>" . $sum . "</td>";
}
echo "</table>";
?>
This is my current output where the points column won't produce:
I apologize if my code appears inefficient, I am a struggling student fanatic of PHP. I appreciate direct answers; however, learning is my key here. Thanks in advance!
$sql = "SELECT users.*, sum(history.points) as points FROM users left join history on users.id=history.userid group by users.id";
$myData = mysql_query($sql);
echo "<table id=\"table\" class=\"table table-striped\">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Model</th>
<th>Year</th>
<th>Plate Number</th>
<th>City</th>
<th>Country</th>
<th>Points</th>
</tr></thead>";
while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['email'] . "</td>";
echo "<td>" . $record['firstName'] . "</td>";
echo "<td>" . $record['lastName'] . "</td>";
echo "<td>" . $record['model'] . "</td>";
echo "<td>" . $record['Year'] . "</td>";
echo "<td>" . $record['plateNumber'] . "</td>";
echo "<td>" . $record['city'] . "</td>";
echo "<td>" . $record['country'] . "</td>";
echo "<td>" . $record['points'] . "</td>";
}
echo "</table>";
It seems that I might have had my code jungled, thank you #abhi and #msfoster for your healthy inputs.
<?php
$sql = "SELECT * FROM users";
$myData = mysql_query($sql);
echo "<table id=\"table\" class=\"table table-striped\">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Model</th>
<th>Year</th>
<th>Plate Number</th>
<th>City</th>
<th>Country</th>
<th>Points</th>
</tr></thead>";
while($record = mysql_fetch_array($myData)){
$id = $record['id'];
$sql_ = "SELECT SUM(`points`) AS value_sum FROM `history` WHERE `userid` = '$id'";
$result = mysql_query($sql_);
#echo mysql_error();
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
echo "<tr>";
echo "<td>" . $id . "</td>";
echo "<td>" . $record['email'] . "</td>";
echo "<td>" . $record['firstName'] . "</td>";
echo "<td>" . $record['lastName'] . "</td>";
echo "<td>" . $record['model'] . "</td>";
echo "<td>" . $record['Year'] . "</td>";
echo "<td>" . $record['plateNumber'] . "</td>";
echo "<td>" . $record['city'] . "</td>";
echo "<td>" . $record['country'] . "</td>";
echo "<td>" . $sum . "</td>";
echo "</tr>";
}
echo "</table>";
?>
My output is finally:
query is correct but as you shown
var_dump($row) = "array (size=1) 'value_sum' => null "
so it have 'null' because of that its showing blank;
but for this you can use join also .
$row = mysql_fetch_assoc($result); returns an array of "rows". Since you want the first row you need to access it $sum = $row[0]['value_sum'];