Im trying to display the database rows in html using php.
here is my php code thats working:
while($traveller=mysqli_fetch_assoc($records)){
echo "<tbody>";
echo "<tr>";
echo "<td border = 3px solid #555555 padding = 5px 10px >". $traveller['ticket_no'] ." </td>";
echo "<td>". $traveller['fname'] ." </td>";
echo "<td>". $traveller['lname'] ." </td>";
echo "<td>". $traveller['flight_name'] ." </td>";
echo "<td>". $traveller['d_name'] ." </td>";
echo "<td>". $traveller['plane_name'] ." </td>";
echo "<td>". $traveller['boarding_time'] ." </td>";
echo "<td>". $traveller['depart_time'] ." </td>";
echo "</tbody>";
echo "</tr>";
}
But its black and white.
im trying to add some style to it so im trying to link it between my html table but its not working and im not sure what the problem is. here is the code:
<td> <?php echo "$traveller['ticket_no']"; ?></td>
is there a way to solve this or add styles to the table.
any help is acceptable and appreaciated.
you can use style tag inside
example:
<td class='myclass'>
<style>
td.myclass
{
text-align:center;
}
try something like this because you missed style attribute.
<?php
while($traveller=mysqli_fetch_assoc($records)){ ?>
<tr>
<td style="background-color: red;"><?php echo $traveller['ticket_no']; ?></td>
<td><?php echo$traveller['fname'];?></td>
<td><?php echo $traveller['lname'];?> </td>
<td><?php echo $traveller['flight_name']; ?></td>
<td><?php echo $traveller['d_name']; ?></td>
<td><?php echo $traveller['plane_name'];?> </td>
<td><?php echo $traveller['boarding_time']; ?></td>
<td><?php echo $traveller['depart_time'];?></td>
</tr>
<?php } ?>
Try to give style to table,tr,td tags, then only you can add styles to table
like this:-
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
then when you print table it will display with specified styles,
here i'm giving styles to diect table, tr, td tags
if you want you can assign "class" to table,tr or td and give style to classes
as you want to give link in table, code like this:-
<td> <?php echo "$traveller['ticket_no']"; ?></td>
and there are fault's in the way you written the loop, just loop tr only
don't include tbody or anything inside loop,it should be outside the loop like this:-
echo "<tbody>";
while($traveller=mysqli_fetch_assoc($records)){
echo "<tr>";
echo "<td>". $traveller['ticket_no'] ." </td>";
echo "<td>". $traveller['fname'] ." </td>";
echo "<td>". $traveller['lname'] ." </td>";
echo "<td>". $traveller['flight_name'] ." </td>";
echo "<td>". $traveller['d_name'] ." </td>";
echo "<td>". $traveller['plane_name'] ." </td>";
echo "<td>". $traveller['boarding_time'] ." </td>";
echo "<td>". $traveller['depart_time'] ." </td>";
echo "</tr>";
}
echo "</tbody>";
You can try style in php code like this:
echo "<tbody>";
while($traveller=mysqli_fetch_assoc($records)){
echo "<tr style='color:white;background-color:green'>";
echo "<td style='border: 3px solid #555555;padding:5px 10px' >". $traveller['ticket_no'] ." </td>";
echo "<td>". $traveller['fname'] ." </td>";
echo "<td>". $traveller['lname'] ." </td>";
echo "<td>". $traveller['flight_name'] ." </td>";
echo "<td>". $traveller['d_name'] ." </td>";
echo "<td>". $traveller['plane_name'] ." </td>";
echo "<td>". $traveller['boarding_time'] ." </td>";
echo "<td>". $traveller['depart_time'] ." </td>";
echo "</tr>";
}
echo "</tbody>";
Or you can try style in html code :
<?php
while($traveller=mysqli_fetch_assoc($records)){ ?>
<tr>
<td style="border: 3px solid #555555;padding:5px 10px"><?php echo $traveller['ticket_no']; ?></td>
<td><?php echo$traveller['fname'];?></td>
<td><?php echo $traveller['lname'];?> </td>
<td><?php echo $traveller['flight_name']; ?></td>
<td><?php echo $traveller['d_name']; ?></td>
<td><?php echo $traveller['plane_name'];?> </td>
<td><?php echo $traveller['boarding_time']; ?></td>
<td><?php echo $traveller['depart_time'];?></td>
</tr>
Related
The below table prints data form a mysql db. Is it possible to make it responsive to adapt to even the smallest devices without losing the image. I am studying bootstrap to see how it can work.
<style>
body { margin: 0; font-family: Arial, Helvetica, sans-serif; }
.content { margin: auto; width: 99.8%; height:79.8%; border: 1px solid green; padding: 10px; background-color: magenta; }
</style>
<div class="content">
<div class="row">
<div class="col">
<?php
print "<table border=1 width=100%>";
print "<td width=10px height=auto bgcolor=#00FF33>" . $row['id'] . "</td>";
print "<td width=200px height=200px>"."<a href='".$row['image']."'><img src=".$row['image']."width=100% height=100%</a>" ."</td>";
print "<td>";
print "<table border=1 width=100%>";
print "<tr>";
print "<td>" . $row['imagename'] . "</td>";
print "</tr>";
print "<tr>";
print "<td>" . $row['name'] . "</td>";
print "</tr>";
print "<tr>";
print "<td>" . $row['username'] . "</td>";
print "</tr>";
print "<tr>";
print "<td>" . $row['useremail'] . "</td>";
print "</tr>";
print "<tr>";
print "<td>" . $row['userphone'] . "</td>";
print "</tr>";
print "<tr>";
print "<td>"."<a href='id=$uniqueid' >More Details</a>"."</td>";
print "</tr>";
print "<tr>";
print "<td>"."<a href='id=$uniqueid' >Contact Seller</a>"."</td>";
print "</tr>";
print "</table>";
print "</td>";
print "</table>";
print "<br />";
?>
</div>
Incase you're using bootstrap just add "table table-responsive" to your table class
<table class="table table-responsive">
i am trying this from 5hrs don't know i am going wrong.My table echos results but not echo total for the same.
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr><?php
(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
(I get these entreis correct )
}
}
(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
?></table>
Remove the } } after the closing tag of the tr.
if you can't close the whole PHP block then you probably did something wrong in your syntax.
I don't know if you have more code or not, if you do - post it so we will get more accurate view of your code.
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr>
<?php
(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
(I get these entreis correct )
(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
}
?>
</table>
You can remove the wrong bracket, OR check that the two bracket are opened when fetching data
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr><?php
//(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
//(I get these entreis correct )
//(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
?>
</table>
Hi I'm creating a questionnaire. The questions and their answers are stored in a table like so:
Now, I want to display the questions and their answers in a table where I get the questions and their possible answers on different rows. Like so:
In my php file, I echo the table and the rows but I cannot figure out how to put the questions and answer in different rows. here is how it looks like:
and here is my php code:
<?php
session_start();
$status=$_GET["status"];
include 'dbh.inc.php';
?>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
text-align: left;
padding: 8px;
}
#td_box{
text-align: center;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #006689;
color: white;
}
</style>
</head>
<body>
<?php
if ($status=="disp") {
$sql="SELECT * FROM questions";
$result = mysqli_query($conn,$sql);
echo "<table>";
while ($row=mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>"; echo $row["question"]; echo "</td>";
echo "<td>"; echo $row["optionA"];echo $row["optionB"];echo $row["optionC"];
echo $row["optionD"];echo $row["optionE"];echo "</td>";
}
}
echo "</table>";
?>
</body>
</html>
Try:
<?php
if ($status=="disp") {
$sql="SELECT * FROM questions";
$result = mysqli_query($conn,$sql);
echo "<table>";
while ($row=mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>"; echo $row["question"]; echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"; echo $row["optionA"];echo $row["optionB"];echo $row["optionC"];
echo $row["optionD"];echo $row["optionE"];echo "</td>";
echo "</tr>";
}
}
echo "</table>";
?>
What I did:
I added a </tr> after your question echo and opend a new one directly after. Then I closed that <tr> after your last <td> as you forgot to do that.
1st : Put all option radio buttons in another tr .
2nd : Maintain proper name attribute for radio button .name="question[$row['question_id']]"
3rd : And use colspan="4" for question td
while ($row=mysqli_fetch_array($result)) {
echo "<tr >";
echo "<td colspan='4'>"; echo $row["question"]; echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<input type=\"radio\" name=\"question[$row['question_id']]\" value=\"$row['optionA']\" >".$row['optionA'];
echo "</td>";
echo "<td>";
echo "<input type=\"radio\" name=\"question[$row['question_id']]\" value=\"$row['optionB']\" >".$row['optionB'];
echo "</td>";
echo "<td>";
echo "<input type=\"radio\" name=\"question[$row['question_id']]\" value=\"$row['optionC']\" >".$row['optionC'];
echo "</td>";
echo "<td>";
echo "<input type=\"radio\" name=\"question[$row['question_id']]\" value=\"$row['optionD']\" >".$row['optionD'];
echo "</td>";
echo "<td>";
echo "<input type=\"radio\" name=\"question[$row['question_id']]\" value=\"$row['optionE']\" >".$row['optionE'];
echo "</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan='4'>"; echo $row["question"]; echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"; echo $row["optionA"];echo "</td>";
echo "<td>"; echo $row["optionB"];echo "</td>";
echo "<td>"; echo $row["optionC"];echo "</td>";
echo "<td>"; echo $row["optionD"];echo "</td>";
echo "</tr>";
this way you will have proper answers in tabular format in aligned manner
Im having an issue with fetch_array. I keep getting the return of record not found. With every thing that I have looked at it seems to me that this code should work. Sorry im new to php web development.
$JobNumber = NULL;
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$JobID = $_POST['jobid'];
include('pmconnect.php');
$sql="SELECT * FROM tblJobMaster WHERE JobNumber=" . $JobID;
$result=$conn->query($sql);
if ($result->num_rows==0)
{
echo "Record not found.<br>";
die(0);
}
$row=$result->fetch_array();
echo '<table style="text-align: left; width: 100%;" border="1" cellpadding="2" cellspacing="2">';
echo "<tbody>";
echo "<tr>";
echo '<td style="vertical-align: top; text-align: right;">Job Number:<br>';
echo "</td>";
echo '<td style="vertical-align: top;">' .$row[0] . '<br>';
echo "</td>";
echo '<td style="vertical-align: top; text-align: right;">Engineer:<br>';
echo '</td>';
echo '<td style="vertical-align: top;">' . $row[3] .'<br>';
echo '</td>';
echo "</tr>";
echo "<form action=pmAssignEngineer2.php method=post id=usrform>";
echo "<input type=hidden name=JobID value=" . $JobID . ">";
echo "<input type=submit value=\"Update\" name=lookup>";
echo "</td>";
echo "</tr>";
echo "</form>";
echo "</tbody>";
}
else
{
echo "<form action=pmAssignEngineer.php method=post>";
echo "<table border=2>";
echo "<tr>";
echo "<td>Job Number:</td>";
echo "<td><input type=text name=JobID></td>";
echo "</tr>";
echo "</table>";
echo "<input type=submit value=\"Lookup\" name=lookupQ><br>";
echo "</form>";
}
HTML elements are posted to $_POST if form method is post.
The element name is key and value is array value.
And the keys are case-sensitive.
In your case, your element has name JobID and you are referring jobid.
Obviously, the keys of $_POST do not match as jobid and JobID are different.
<input type=text name=JobID>
So, update the following line:
$JobID = $_POST['jobid'];
To
$JobID = $_POST['JobID'];
im trying to display the picture saved in the database using Php, but what i get is Url link which was saved in the database and not the picture, i think the syntax is wrong.
<?php
echo "<table border=\"1\" align=\"center\">";
echo "<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Manufacturer</th>
<th>Image</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['Name']."</td>";
echo "<td>" .$row['Description'] ."</td>";
echo "<td>" .$row['Price'] ."</td>";
echo "<td>" .$row['Manufacturer'] ."</td>";
echo "<td>" .$row['ImageURL'] ."</td>";
echo "</tr>";
}
echo "</table>";
?>
</p>
<?php
You need to construct an <img> element, not just output the image URL.
Try replacing this: $row['ImageURL']
With this: "<img src='".$row['ImageURL']."' />
On this line:
echo "<td>" .$row['ImageURL'] ."</td>";
it looks like you'll want that to be:
echo "<td><img src='" .$row['ImageURL'] ."' /></td>";
Change
echo "<td>" .$row['ImageURL'] ."</td>";
to
echo "<td><img src='" .$row['ImageURL'] ."' style='width: 200px; height: 350px;' /></td>";
Modify the values 200px and 350px to whatever you want, just keep the 'px' suffix.