Why is formatting broken when using bootstrap in PHP? - php

I want to generate Bootstrap 5 table .table-striped using data from MySQL, table: [users]
The table in plain HTML looks fine, but as soon as I generate it using PHP all styling disappears
PHP Code:
<?php
$result = mysqli_query($link,"SELECT * FROM users ORDER BY id ASC");
echo "<table class='table table-striped'>
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Rank</th>
<th>Created at</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['rank'] . "</td>";
echo "<td>" . $row['created_at'] . "</td>";
echo "</tr>";
echo " </tbody>";
}
echo "</table>";
mysqli_close($link);
?>
and I have <div class='table-responsive'> before <?php and </div> after ?>
I would be glad if you could explain me where I am making a mistake and I would like the table to have the same styling as in pure HTML
EDIT:
rendered HTML
<div class='table-responsive'>
<table class='table table-striped'>
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Rank</th>
<th>Created at</th>
</tr>
</thead><tbody><tr><td>39</td><td></td><td>julia</td><td>2021-12-20 00:42:14</td></tr> </tbody><tbody><tr><td>40</td><td>test</td><td>user</td><td>2021-12-20 02:35:37</td></tr> </tbody><tbody><tr><td>41</td><td>testt</td><td>user</td><td>2021-12-20 17:22:43</td></tr> </tbody></table></div>

You are building a new tbody for every row. You should move the tbody creation outside the while loop. You also only need 1 echo, rather than echoing every line. I would do:
<?php
$result = mysqli_query($link,"SELECT * FROM users ORDER BY id ASC");
$output = "<div class='table-responsive'>
<table class='table table-striped'>
<thead>
<tr>
<th style='background-color:#ad8c70'>#</th>
<th style='background-color:#ad8c70'>Username</th>
<th style='background-color:#ad8c70'>Rank</th>
<th style='background-color:#ad8c70'>Created at</th>
</tr>
</thead>
<tbdoy>";
while($row = mysqli_fetch_array($result))
{
$output .= "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['username'] . "</td>
<td>" . $row['rank'] . "</td>
<td>" . $row['created_at'] . "</td>
</tr>";
}
mysqli_close($link);
$output .= '</tbody></table>';
echo $output;
?>

Related

How do you copy imported sql values in a table into a text box?

I have created a table and am pulling data from mySQL database. I have created 3 columns and would like to take the 3rd column (suggested quantity) and copy those values into a row of text boxes in the 4th column, (order quantity) so they could be edited by a user. How do I do this?
$sql = "SELECT item_price.item_id, item_price.ITEM_NAME,suggested_qty,Price_item
FROM item_price JOIN suggested_item
ON item_price.ITEM_NAME = suggested_item.ITEM_NAME";
$result = $conn->query($sql);
?>
<tr>
<th> ID</th>
<th>Item Name</th>
<th>Suggested Quantity</th>
<th>Order Quantity</th>
<th>Total Cost ($)</th>
</tr>
<?php
while ($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['item_id'] ."</td>";
echo "<td>" . $row['ITEM_NAME'] . "</td>";
echo "<td>" . $row['suggested_qty'] . "</td>";
}
?>
</table>
You should start by wrapping the table in a form and add a column with a input text field with the suggested quantity. Something like this should get you started:
$sql = "SELECT item_price.item_id, item_price.ITEM_NAME,suggested_qty,Price_item
FROM item_price JOIN suggested_item
ON item_price.ITEM_NAME = suggested_item.ITEM_NAME";
$result = $conn->query($sql);
?>
<form action="#" method="post">
<table>
<tr>
<th> ID</th>
<th>Item Name</th>
<th>Suggested Quantity</th>
<th>Order Quantity</th>
<th>Total Cost ($)</th>
</tr>
<?php
while ($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['item_id'] ."</td>";
echo "<td>" . $row['ITEM_NAME'] . "</td>";
echo "<td>" . $row['suggested_qty'] . "</td>";
echo "<td><input type='text' name='editedvalues[]' value='" . $row['suggested_qty'] . "' /></td>";
echo "<td>total</td>";
echo "</tr>";
}
?>
</table>
</form>
You should add a javascript onchange listener to the input fields to calculate a total price for each row and show it to the user.

Undefined href in a html table

I've got a problem with a html table in a php code.
I try this code but I have a problem with href in a tag. In fact when I click on the href it post undefined while DetailsAnnonces.php exist.
Thanks for your response.
<?php
$conn=#mysqli_connect($_SESSION['servername'],$_SESSION['username'],
$_SESSION['password'], $_SESSION['database']) or die(mysql_error());
$verifExistence1 = "SELECT * FROM `annonces`;";
$result = mysqli_query($conn, $verifExistence1);
echo "<table border = 2>";
echo "<table align='center'> <thead>
<tr>
<th>Titre</th>
<th>Description</th>
<th>Image</th>
</tr>
</thead>
<tbody>";
while($row = #mysqli_fetch_assoc($result)){
$idbis=$row['id'];
$titrebis=$row['titre'];
echo "<tr>
<td>" . $row['titre'] . "</td><td>" . $row['description'] . "</td>
<td> $titrebis
</tr>";
}
echo "</table>";
//echo $html_table;
?>
The problem was caused by a javascript left on this page.
The problem is in the $idbis
<?php
echo "
<td>
'".$titrebis."
</td>
</tr>";
?>

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']."

Can't get multiple result in td

I have this problem in displaying the result in td inside table. I want to display all rows coming from the database.
I have code but it results only one row.
<table class="table table-striped table-bordered table-hover" id="dataTables">
<thead>
<tr>
<th>ID</th>
<th>Role</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
$con=mysqli_connect("localhost", "root", "", "trackingsys");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query="SELECT * from role" ;
$result = mysqli_query($con,$query);
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['RoleName'] . "</td>";
echo "<td>" . $row['RoleDesc'] . "</td>";
echo '</tr>';
}
mysqli_close($con); ?>
</tbody>
</table>
I edited this code and put the ending tag </td>. But still it results only one row.

drop down menu not appearing in the following php code, but every other element is in the table

I'm using php to create a table from the data extracted from MySQL database. I'm using SELECT * From table_name and all data is appearing as I wish. But I want to create a Drop Down menu in the last column for that I'm using the function displayDMenu. But the drop down menu is still not appearing in the table. Can you please suggest what is causing the problem and solution? Or alternate solutions? Here's the code:
<div id="main">
<?php
include("config.php");
$result = mysql_query("SELECT * FROM schedule");
echo "<table border='1'>
<tr>
<th>Appoint No.</th>
<th>CR number</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>Year</th>
<th>Department</th>
<th>Status</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td style='padding:3px'>" . $row['S_ID'] . "</td>";
echo "<td style='padding:3px'>" . $row['crnumber'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdTime'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdDate'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdMonth'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdYear'] . "</td>";
echo "<td style='padding:3px'>" . $row['DName'] . "</td>";
echo "<td 'style='padding:3px'>" . displayDMenu() . "</td>";
echo "</tr>";
}
echo "</table>";
function displayDMenu() {
$r = '';
$r .='<form method="post" action="ScdApproval.php">';
$r .='<select name="Status">';
$r .='<option value="approved" selected>Approve</option>';
$r .='<option value="disapproved">Disapprove</option>';
$r .='</select>';
$r .='</form>';
}
?>
</div> <!--main ends here -->
Not sure why you need the user function its not doing anything logical its just spitting back html which there is no reason not to put that html in the while loop.
<?php
include("config.php");
$result = mysql_query("SELECT * FROM schedule");
?>
<style>#main table tr td{padding:3px;}</style>
<div id="main">
<table border="1">
<tr>
<th>Appoint No.</th>
<th>CR number</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>Year</th>
<th>Department</th>
<th>Status</th>
</tr>
<?php
if(mysql_num_rows($result) > 0):
while($row = mysql_fetch_array($result)): ?>
<tr>
<td><?php echo $row['S_ID']?></td>
<td><?php echo $row['crnumber']?></td>
<td><?php echo $row['ScdTime']?></td>
<td><?php echo $row['ScdDate']?></td>
<td><?php echo $row['ScdMonth']?></td>
<td><?php echo $row['ScdYear']?></td>
<td><?php echo $row['DName']?></td>
<td><form method="post" action="ScdApproval.php">
<select name="Status">
<option value="approved" selected>Approve</option>
<option value="disapproved">Disapprove</option>
</select>
</form>
</td>
</tr>
<?php
endwhile;
else: ?>
<tr>
<td rowspan="8">No results</td>
</tr>
<?php endif;?>
</table>
</div> <!--main ends here -->
Also:
Really you should also switch to using PDO or mysqli prepared querys for new code.
You need to return the value $r in the function displayDMenu()
ie
function displayDMenu()
{
.........
return $r;
}

Categories