PHP get database data into HTML table - php

I want to put my database data into HTML table and its not so easy as it seems, Look at my photographs and code to get better understanding on what my problem is
My Result:
Desired Result:
Basically i want to separate the results as the red line indicates so each item becomes seperate.
PHP code:
$sql = mysql_query("SELECT * FROM products"); ?>
<table id='display'>
<?php
while($rows = mysql_fetch_array($sql))
{
?>
<tr><td><?php echo "<img src=$rows[$product_image] height='200px' width='200px'>" ?></td></tr>
<tr>
<td><b><?php echo "$rows[$product_name]" ?></td>
<td><b><?php echo "Avalible: $rows[$product_qua]" ?></td>
<td><b><?php echo "Price: £ $rows[$product_price]" ?></td>
<td><b><?php echo "Description: $rows[$product_des]" ?></td>
</tr>
<tr>
<td><strong><p>Please Login To purchase this item </p></strong>Login</td>
</tr>
<?php
}
?>
</table>
CSS code:
table#display{
float:left;
border: 5px solid black;
margin-top:50px;
margin-left:10px;
}
table#display td{
border: 1px solid black;
padding:0 8px;
}
table#display tr{
border: 1px solid black;
padding:0 8px;
}

My solution:
PHP Code:
$sql = mysql_query("SELECT * FROM products"); ?>
<?php
while($rows = mysql_fetch_array($sql))
{
?>
<table class='display'>
<tr><td><?php echo "<img src=$rows[$product_image] height='200px' width='200px'>" ?></td></tr>
<tr>
<td><b><?php echo "$rows[$product_name]" ?></td>
<td><b><?php echo "Avalible: $rows[$product_qua]" ?></td>
<td><b><?php echo "Price: £ $rows[$product_price]" ?></td>
<td><b><?php echo "Description: $rows[$product_des]" ?></td>
</tr>
<tr>
<td><strong><p>Please Login To purchase this item </p></strong>Login</td>
</tr>
</table>
<?php
}
?>
The CSS:
table.display{
float:left;
border: 5px solid black;
margin-top:50px;
margin-left:10px;
margin-bottom: 10px;
}

Related

How to change background color of table row if quantity column has numeric value less than or equal to 10

I am implementing cloth shopping website in which stock is added to the database by admin and admin can view, update and delete stock as well. while displaying record in table from the database I want that the item from stock that has quantity becomes 10 or less than 10 after customer purchasing that row color becomes red so that it should be alert for the admin that particular stock quantity is low.
Here is my code:
<table>
<tr>
<th>Sr.No</th>
<th>Product ID</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th>Image</th>
</tr>
<?php
$query = "SELECT * FROM add_stock ORDER BY id DESC";
$rs_result = mysqli_query ($query);
while ($result=mysqli_fetch_array($rs_result) )
{
?>
<?php $qty =$result['dress_quantity']; ?>
<tr <?php if($qty<=10){echo 'style="background:red"';} ?> >
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['brand_name'];</td>
<td><?php echo $result['price']; ?></td>
<td><?php echo $result['gender_name']; ?></td>
<td><?php echo $result['category_name']; ?></td>
<td><?php echo $result['material_name']; ?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description']; ?></td>
<td><?php echo $result['dress_quantity']; ?></td>
<td><a href="javascript:window.open('<?php echo $result['image'] ?>','mypopuptitle', '_parent')" >View Image</a></td>
</tr>
</table>
<?php
}
?>
CSS CODE:
table {
color: #333;
font-family: Helvetica, Arial, sans-serif;
border-collapse:
collapse; border-spacing: 0;
}
td, th {
border: 1px solid; /* No more visible border */
height: 30px;
transition: all 0.3s; /* Simple transition for hover effect */
}
th {
background: #DFDFDF; /* Darken header a bit */
font-weight: bold;
text-align: center;
height: 50px;
}
td {
background: #FAFAFA;
height: 40px;
}
/* Cells in even rows (2,4,6...) are one color */
tr:nth-child(even) td { background: #F1F1F1; }
/* Cells in odd rows (1,3,5...) are another (excludes header cells) */
tr:nth-child(odd) td { background: #FEFEFE; }
Create a class in CSS like .isLess
.isLess { background-color:red;}
Then do something like this:
a ternary operator echoing the class, if qty is < 10, add the class, if not don't output anything.
<tr <?php echo ($result['dress_quantity'] < 10 ? "class='isLess'" : ""); ?> >
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['brand_name'];</td>
<td><?php echo $result['price']; ?></td>
<td><?php echo $result['gender_name']; ?></td>
<td><?php echo $result['category_name']; ?></td>
<td><?php echo $result['material_name']; ?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description']; ?></td>
<td><?php echo $result['dress_quantity']; ?></td>
<td><a href="javascript:window.open('<?php echo $result['image'] ?>','mypopuptitle', '_parent')" >View Image</a></td>
</tr>
Alternatively, you could do something like
$class = "";
if($result['dress_quantity'] < 10) { $class='isLess'; }
<tr <?php echo $class; ?> >

update multi row php

i try to update a multi row like this
$ID = $_POST['ID'];
$size = count($_POST['compo']);
while ($i < $size) {
$compo = $_POST['compo'][$i];
$fill = $_POST['fill'][$i];
$query = "UPDATE Invantsion Set compon='".$compo."',Num='".$fill."' where ID='".$ID."'";
++$i;
}
the code on post is bulit from to parts..
<tr>
<td align="center"><span lang="he"><font size="2" face="Arial">
<?php
echo "<input type='hidden' name='compo[$i]' value='".$row['compon']."'>".$row['compon']."";
?>
</font></span>
</td>
<td>
<select name='fill[$i]' style="border: 1px solid #000000; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; background-color: #FFFFFF; font-family:Arial; font-size:8pt; font-weight:bold">
<?php
$Uid = $row['id'];
require( dirname( __FILE__ ) . '/com.php' );
echo "</select>";
}
?>
who getting the new data for mysql and the sec part who get the original data for MYSQL
while ($row = mysqli_fetch_array($result)) {
echo "<input type='hidden' name='ID' value='".$row['ID']."'>";
?>
<tr>
<td align="center"><span lang="he"><font size="2" face="Arial">
<?php
echo $row['compon'];
?>
</font></span>
</td>
<td align="center"><span lang="he"><font size="2" face="Arial">
<?php
echo $row['Num'];
?>
</font></span>
</td>
</tr>
what should be worng if i get error 500 or just the last result
UPDATE Invantsion Set compon='DECODERS',Num='' where ID='30'
and if i try to make the code looping only the words after set it no doing nothing at all..
any one?

While loop not displaying all results from query

I'm having problems showing all the results in my modal. There are three rows I want to display in the modal: Billing months for June 2016, May 2016, and April 2016, but only the latest(June 2016) will show. Here's the part of my code:
if(isset($_GET['id']))
{
$page_id = $_GET['id'];
$select_query = "SELECT * FROM billing_info INNER JOIN customer_login ON billing_info.account_no=customer_login.account_no WHERE billing_info.account_no='$page_id' ";
$run_query = mysqli_query($conn,$select_query);
while($row = mysqli_fetch_array($run_query))
{
$id = $row['user_id'];
$cust_fname = $row['cust_fname'];
$cust_mname = $row['cust_mname'];
$cust_lname = $row['cust_lname'];
$branch = $row['branch'];
$account_no = $row['account_no'];
$billing_month = $row['billing_month'];
$pres_reading = $row['pres_reading'];
$prev_reading = $row['prev_reading'];
$payment_status = $row['payment_status'];
$due_date = $row['due_date'];
$int_pres = intval($row["pres_reading"]);
$int_prev = intval($row["prev_reading"]);
$difference = $int_pres-$int_prev;
$payment = $difference*26.678;
$cust_fname = ucfirst($cust_fname);
$cust_mname = ucfirst($cust_mname);
$cust_lname = ucfirst($cust_lname);
$branch = ucfirst($branch);
?>
<div id="openModal" class="modalDialog" style="width:100%;">
<div id="form-sms" style="height: 500px; overflow: scroll;">
<div class="close"> X</div>
<form method="get" action="customer_view.php?id=<?php echo $id; ?>" name="id" enctype="multipart/form-data">
<table width="100%" bgcolor="white" align="center">
<style type="text/css">
td.hundred{
width: 80%;
text-align: center;
}
input {
width: 100%;
}
input[type="text"]{
width: 90%;
text-align: left;
}
input[type="submit"]{
width: 100%;
text-align: center;
}
textarea{
width: 90%;
text-align: left;
margin-left: 10%;
}
td {
padding: 10px;
}
.tdhover:hover {
color: #AFEEEE;
}
</style>
<tr >
<td align="center" colspan="10"><h1><?php echo $cust_fname." ".$cust_mname." ".$cust_lname." - ".$account_no." - ".$branch." Details" ?></h1></td>
</tr>
<tr bgcolor="#20B2AA">
<th>Billing Month</th>
<th>Present Reading</th>
<th>Previous Reading</th>
<th>Payment</th>
<th>Status</th>
<th>Due Date</th>
<th>Water Usage (in m<sup>3</sup>)</th>
<th>Edit</th>
</tr>
<tr bgcolor="#AFEEEE">
<td><?php echo $billing_month; ?></td>
<td><?php echo $pres_reading; ?></td>
<td><?php echo $prev_reading; ?></td>
<td><?php echo "Php " . $payment; ?></td>
<td><?php echo $payment_status; ?></td>
<td><?php echo $due_date; ?></td>
<td><?php echo $difference; ?></td>
<td>Edit</td>
</tr>
<tr style="margin-top: 10px;">
<td bgcolor="#20B2AA" align="center" class="tdhover" colspan="10">Add Billing Information</td>
</tr>
</table>
</form>
</style>
</table>
</form>
</div>
</div>
<?php } }?>
You should loop/parse the data only for <tr> and <td> Not for all tag like <table> and <div> outer the table. Html will loop all of inside { ... } sign.
Then, your style code should be placed the upper (not fix with html/php). This your code should be:
<style type="text/css">
td.hundred{
width: 80%;
text-align: center;
}
input {
width: 100%;
}
input[type="text"]{
width: 90%;
text-align: left;
}
input[type="submit"]{
width: 100%;
text-align: center;
}
textarea{
width: 90%;
text-align: left;
margin-left: 10%;
}
td {
padding: 10px;
}
.tdhover:hover {
color: #AFEEEE;
}
</style>
<?php
if (isset($_GET['id'])) {//open the isset
$page_id = $_GET['id'];
$select_query = "SELECT * FROM billing_info INNER JOIN customer_login ON billing_info.account_no=customer_login.account_no WHERE billing_info.account_no='$page_id' ";
$run_query = mysqli_query($conn, $select_query);
?>
<div id="openModal" class="modalDialog" style="width:100%;">
<div id="form-sms" style="height: 500px; overflow: scroll;">
<div class="close"> X</div>
<form method="get" action="customer_view.php?id=<?php echo $id; ?>" name="id" enctype="multipart/form-data">
<table width="100%" bgcolor="white" align="center">
<?php
while ($row = mysqli_fetch_array($run_query)) { //open the while
$id = $row['user_id'];
$cust_fname = $row['cust_fname'];
$cust_mname = $row['cust_mname'];
$cust_lname = $row['cust_lname'];
$branch = $row['branch'];
$account_no = $row['account_no'];
$billing_month = $row['billing_month'];
$pres_reading = $row['pres_reading'];
$prev_reading = $row['prev_reading'];
$payment_status = $row['payment_status'];
$due_date = $row['due_date'];
$int_pres = intval($row["pres_reading"]);
$int_prev = intval($row["prev_reading"]);
$difference = $int_pres - $int_prev;
$payment = $difference * 26.678;
$cust_fname = ucfirst($cust_fname);
$cust_mname = ucfirst($cust_mname);
$cust_lname = ucfirst($cust_lname);
$branch = ucfirst($branch);
?>
<tr >
<td align="center" colspan="10"><h1><?php echo $cust_fname . " " . $cust_mname . " " . $cust_lname . " - " . $account_no . " - " . $branch . " Details" ?></h1></td>
</tr>
<tr bgcolor="#20B2AA">
<th>Billing Month</th>
<th>Present Reading</th>
<th>Previous Reading</th>
<th>Payment</th>
<th>Status</th>
<th>Due Date</th>
<th>Water Usage (in m<sup>3</sup>)</th>
<th>Edit</th>
</tr>
<tr bgcolor="#AFEEEE">
<td><?php echo $billing_month; ?></td>
<td><?php echo $pres_reading; ?></td>
<td><?php echo $prev_reading; ?></td>
<td><?php echo "Php " . $payment; ?></td>
<td><?php echo $payment_status; ?></td>
<td><?php echo $due_date; ?></td>
<td><?php echo $difference; ?></td>
<td>Edit</td>
</tr>
<tr style="margin-top: 10px;">
<td bgcolor="#20B2AA" align="center" class="tdhover" colspan="10">Add Billing Information</td>
</tr>
<?php } //while closing
?>
</table>
</form>
</style>
</table>
</form>
</div>
</div>
Hopefully it will be success for you ;)

HTML table not displaying with a php while loop inside of it

I am creating a table that loops information from my database. I created the table with html and added styling via an external css sheet. The table format is not displaying in any way. None of the borders are displaying and the 100% width is not working. Could my while loop be causing this affect?
<table class="tableproduct">
<tr>
<th class="thproduct">Product ID</th>
<th class="thproduct">Product Name</th>
<th class="thproduct">Price</th>
<th class="thproduct">Category</th>
<th class="thproduct">Description</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($q)) : ?>
<tr>
<td class="tdproduct"><?php echo $row['product_id']; ?> </td>
<td class="tdproduct"><?php echo $row['name']; ?> </td>
<td class="tdproduct"><?php echo $row['price']; ?> </td>
<td class="tdproduct"><?php echo $row['category']; ?> </td>
<td class="tdproduct"><?php echo $row['description']; ?> </td>
</tr>
</table>
<?php endwhile; ?>
.tableproduct {
width: 100%;
border: 1px solid black;
}
.tdproduct {
border: 1px solid black;
}
.thproduct {
height: 100px;
border: 1px solid black;
}
I ended up resolving this.
I had to put an if statement with the while loops and it created the results I was looking for.
I added this to the while loop:
if( $result ){
while($row = mysqli_fetch_assoc($result)) :

PHP, HTML table create

I have little problem with tables when u echo my results.
and this is my code
<?php
//return data if any
while ($client = mysqli_fetch_assoc($result)) {
//output data form each client
//var_dump($client);
?>
<table id="t01">
<tr>
<th>ID</th>
<th>name</th>
<th>lastname</th>
<th>age</th>
<th>Username</th>
<th>Password</th>
</tr>
<tr>
<td><?php echo $client['id'] ?></td>
<td><?php echo $client['emri'] ?></td>
<td><?php echo $client['mbiemri'] ?></td>
<td><?php echo $client['mosha'] ?></td>
<td><?php echo $client['username'] ?></td>
<td><?php echo $client['password'] ?></td>
</tr>
<style>
table {
width:100%;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
table#t01 tr:nth-child(even) {
background-color: #eee;
}
table#t01 tr:nth-child(odd) {
background-color:#fff;
}
table#t01 th {
background-color: black;
color: white;
}
</style>
</table>
this is my code and the list its echo the first line like id, name ,lastname... for every result. so i want attributes on the top and then down results (i cant post a photo)
You just Need to reorder your code:
<table id="t01">
<tr>
<th>ID</th>
<th>name</th>
<th>lastname</th>
<th>age</th>
<th>Username</th>
<th>Password</th>
</tr>
<?php
//return data if any
while ($client = mysqli_fetch_assoc($result)) {
//output data form each client
//var_dump($client);
?>
<tr>
<td><?php echo $client['id'] ?></td>
<td><?php echo $client['emri'] ?></td>
<td><?php echo $client['mbiemri'] ?></td>
<td><?php echo $client['mosha'] ?></td>
<td><?php echo $client['username'] ?></td>
<td><?php echo $client['password'] ?></td>
</tr>
><?php } ?>
</table>
You had Everything in youre While so for every Entry everything got printed.
And put your Style Tag in the Head Section of your HTML DOC

Categories