How to display data in tables in PHP - php

I have a problem with showing tables in PHP. I have installed shortcode exec php plugin.
This is the code I have done:
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo "<p><h3>".$results['id']."</h3>"."Student Name :".$results['name']." </p>" . "</br>";
echo "Age : ".$results['age']." Years old.</br>";
echo "Course : ".$results['course']."</br>";
echo "Gender : ".$results['gender']."</br></br>";
echo "<HR>";
}
}
May I know how I can sort the data out in tables? Any table codes I try doesn't seem to display at all..

<table>
<tr>
<td>Student name</td>
<td>Age</td>
<td>Course</td>
<td>Gender</td>
</tr>
<?php
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo "<tr><td>".$results['id']."</td>";
echo "<td>".$results['age']."</td>";
echo "<td>".$results['course']."</td>";
echo "<td>".$results['gender']."</td></tr>";
}
}
?>
</table>
Array sorting in php
http://us1.php.net/manual/en/function.sort.php

Simply echo the html table tags like so:
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Age</th>
<th>Course</th>
<th>Gender</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['course'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "</tr>";
}
echo "</table>";

<table>
<tr>
<th>ID</th>
<th>Student Name</th>
<th>Age</th>
<th>Course</th>
<th>Gender</th>
</tr>
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['id']."</td>";
echo "<td>".$results['name']."</td>";
echo "<td>".$results['age']." Years old.</td>";
echo "<td>".$results['course']."</td>";
echo "<td>".$results['gender']."</td>";
echo "</tr>";
}
echo "</table>";

you need to connect databse and then fetch data to database
while($results = mysql_fetch_array($row)){
echo "<tr>";
echo "<td>".$results['id']."</td>";
echo "<td>".$results['name']."</td>";
echo "<td>".$results['age'].".</td>";
echo "<td>".$results['course']."</td>";
echo "<td>".$results['gender']."</td>";
echo "</tr>";

Related

error 403 ACCESS FORBIDDEN! You don't have permission to access the requested object. It is either read-protected or not readable by the server

I am trying to open a file that is stored in my local storage. But everytime I clicked OPEN, this error 403 comes up. I think this happened when I tried adding a delete button and created complications with my php code.
Please check this code:
<?php
$con=mysqli_connect("localhost","root","","annualdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM asean_japan WHERE agency='Rail'");
?>
<table class='page'>
<tr>
<th>Select</th>
<th>Agency</th>
<th>FileName</th>
<th>FileType</th>
<th>Date Received</th>
<th>Action</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td align='center' bgcolor='#FFFFFF'><input name='checkbox[]' type='checkbox' value='<?php echo $row['id']; ?>'></td>
<td><?php echo $row['agency']; ?></td>
<td><?php echo $row['filename']; ?></td>
<td><?php echo $row['filetype']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><a target='_blank' href='../annual/indicators/" <?php echo $row['filename']; ?>"'>OPEN</a></td>
</tr>
<?php
}
?>
</table>
<?php
mysqli_close($con);
?>
I tried putting my table inside the php tag and it suddenly works. What could be the problem with this?
echo "<table class='page'>
<tr>
<th>ID</th>
<th>Agency</th>
<th>FileName</th>
<th>FileType</th>
<th>Date Received</th>
<th>Action</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['agency'] . "</td>";
echo "<td>" . $row['filename'] . "</td>";
echo "<td>" . $row['filetype'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a target='_blank' href='../annual/CPA/" . $row['filename'] . "'>OPEN</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<td><a target='_blank' href='../annual/indicators/" <?php echo $row['filename']; ?>"'>OPEN</a></td>
That line was invalid. It would create a link like this:
http://myhost.com/annual/indicators/%22%20somefile.pdf%22 where %22 is urlencoded value for " and %20 is urlencoded value for whitespace.
You can use it this way:
<td><a target='_blank' href='../annual/indicators/<?php echo $row['filename']; ?>'>OPEN</a></td>

PHP - Code is create individual tables for each record from database

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>";

table class not working after inserting sql database inside

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>

view detail of single row of MySQL table in new page after click

I have a page that display all job from MySQL database.
this is the code:
<?php
$result = mysqli_query($conn,"SELECT * FROM job ORDER BY `CreatedTime` DESC");
echo "<table border='0' cellpadding='0' cellspacing='0' class='table-fill'>
<tr>
<th width='250px' position='fixed'>Job Title</th>
<th width='150px'>Company Name</th>
<th width='100px'>Location</th>
<th>Closing Date</th>
</tr>";
while($row = mysqli_fetch_array($result) ) {
echo "<tr>";
echo "<td>" . $row['positiontitle'] . "</td>";
echo "<td>" . $row['companyname'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['closingdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
I want when i click on one of the job from the table then it should display the whole information in new page.
thanks
Add one more row to your table
<th>View Job</th>
Your code :
$result = mysqli_query($conn,"SELECT * FROM job ORDER BY `CreatedTime` DESC");
echo "<table border='0' cellpadding='0' cellspacing='0' class='table-fill'>
<tr>
<th width='250px' position='fixed'>Job Title</th>
<th width='150px'>Company Name</th>
<th width='100px'>Location</th>
<th>Closing Date</th>
<th>View Job</th>
</tr>";
while($row = mysqli_fetch_array($result) ) {
echo "<tr>";
echo "<td>" . $row['positiontitle'] . "</td>";
echo "<td>" . $row['companyname'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['closingdate'] . "</td>";
echo "<td><a href='job_details.php?id=".$row['job_id']."'>View Job</td>";
echo "</tr>";
}
And in job_details.php fetch the details against the id and display the result on that page.
jobdetails.php :
<?php
$result = mysqli_query($conn,"SELECT * FROM job WHERE job_id = '".$_GET['id']."' ORDER BY `CreatedTime` DESC");
$jobdetails = mysqli_fetch_assoc($result);
echo 'Position : '.$jobdetails['positiontitle'].'<br>';
echo 'Company Name: '.$jobdetails['companyname'].'<br>';
echo 'Location: '.$jobdetails['location'].'<br>';
echo 'Closing Date: '.$jobdetails['closingdate'].'<br>';
?>
you can also use this id= instead of id=".$row['job_id']."

Retrieving Mysql data fails... Why?

So this is my code but it does not work, I just get this
Title Article "; while($row = mysqli_fetch_array($result)) { echo ""; echo "" . $row['Title'] . ""; echo "" . $row['Article'] . ""; echo ""; } echo ""; mysqli_close($con); ?>
Any ideas?
<?php
$con=mysqli_connect("localhost","____","____","test_database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM written");
echo "<table border='1'>
<tr>
<th>Title</th>
<th>Article</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Title'] . "</td>";
echo "<td>" . $row['Article'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Tripple check your quotation marks are set correctly. Either
echo "<table border='1'>
or
</tr>";
has an error in it.
You may also try using echo as a function (bracelets), it should make spotting the error easier :-)
It would be helpfull to see the resulting markup instead of the text ;-)
You have to do this:
echo "<table border='1'><tr><th>Title</th><th>Article</th></tr>";
or
echo "<table border='1'>";
echo "<tr>";
echo "<th>Title</th>";
echo "<th>Article</th>";
echo "</tr>";
instead of
echo "<table border='1'>
<tr>
<th>Title</th>
<th>Article</th>
</tr>";

Categories