How to print out mySQL database into a div table? - php

Grades table is this:
username assignment weight mark
a a1 10% 50%
b a1 10% 60%
How would I print this out using php/html?
<div class="a">
<div class="divTableHeading">
<div class="divTableRow">
<div class="divTableHead">Username</div>
<div class="divTableHead">assignment</div>
<div class="divTableHead">weight</div>
<div class="divTableHead">mark</div>
</div>
</div>
<div class="divTableBody">
<?php
include("config.php");
$sql = "SELECT username, assignment, weight, mark FROM grades";
$result = mysqli_query($db,$sql);
while($row = $result->fetch_assoc()) {
?>
<div class="divTableRow">
<div class="divTableCell">$row['username']</div>
<div class="divTableCell">$row['assignment']</div>
<div class="divTableCell">$row['weight']</div>
<div class="divTableCell">$row['mark']</div>
</div>
</div>
</div>
$db is the connected database that is from config.php. How would I keep looping through and output the grades table?
accidental edit

You're missing to echo your record values.
From just $row['username'] changed to <?php echo $row['username']; ?>
And you missed } to close your while loop. Closed it as well with - <?php } ?>
Updated code:
<div class="divTable blueTable">
<div class="divTableHeading">
<div class="divTableRow">
<div class="divTableHead">Username</div>
<div class="divTableHead">assignment</div>
<div class="divTableHead">weight</div>
<div class="divTableHead">mark</div>
</div>
</div>
<div class="divTableBody">
<?php
include("config.php");
$sql = "SELECT username, assignment, weight, mark FROM grades";
$result = mysqli_query($db,$sql);
while($row = $result->fetch_assoc()) {
?>
<div class="divTableRow">
<div class="divTableCell"><?php echo $row['username']; ?></div>
<div class="divTableCell"><?php echo $row['assignment']; ?>/div>
<div class="divTableCell"><?php echo $row['weight']; ?></div>
<div class="divTableCell"><?php echo $row['mark']; ?></div>
</div>
<?php } ?>
</div>
</div>

Don't forget to output your values
<?php echo $row['username']; ?>

Related

Connecting images from SQL

Every time I try and load the page below the images do not show. I have the image names in an SQL database and also have the image folder in the same path as my index.php.
<div class="container-fluid">
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<h2 class="text-center">Products</h2>
<?php
$query = "SELECT * FROM product";
$result = mysqli_query($connect,$query);
while ($row = mysqli_fetch_array($result)) {?>
<form method="get" action="index.php?id=<?= $row['id'] ?>">
<img src="img/<?= $row['image'] ?>" style='height: 150px;'>
<h2><?= $row['name']; ?></h2>
<h2><?= $row['price']; ?></h2>
</form>
<?php }
?>
</div>
<div class="col-md-6">
<h2 class="text-center">Shopping Cart</h2>
</div>
</div>
</div>
</div>
</body>
</html>
I tried this code on my computer and the image appears perfectly.
Perhaps, you forgot to place a connection query.
<div class="container-fluid">
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<h2 class="text-center">Products</h2>
<?php
$connect = mysqli_connect('localhost','root','','test');
$query = "SELECT * FROM product";
$result = mysqli_query($connect,$query);
while ($row = mysqli_fetch_array($result)) {?>
<form method="get" action="index.php?id=<?= $row['id'] ?>">
<img src="img/<?= $row['image'] ?>" style='height: 150px;'>
<h2><?= $row['name']; ?></h2>
<h2><?= $row['price']; ?></h2>
</form>
<?php }
?>
</div>
<div class="col-md-6">
<h2 class="text-center">Shopping Cart</h2>
</div>
</div>
</div>
</div>

PHP SUM total in the database

Hi I am new in PHP but I want to Sum my amount and display it on the page currently I am doing like this but it is not showing.
<div class="col-md-4 col-lg-4">
<div class="panel panel-bordered panel-black">
<div class="panel-heading">
<h3 class="panel-title"><?php echo translate('Total Amount ');?></h3>
</div>
<div class="panel-body">
<div class="text-center">
<h1>
<?php
echo $this->db->select('SUM(amount) as Total');
$getData = $this->db->get_where('tablename',array('amount = '!=0))->first_row();
echo $getData->Total;
?>
</h1>
</div>
</div>
</div>
</div>
try this
<?php
$data = $this->db->get_where('tablename',array('amount = '=>1))->sum(amount)->result();
foreach($data as $row):
echo $row['amount']; //if array output
echo $row->amount; //object output
endforeach;
?>
<div class="col-md-4 col-lg-4">
<div class="panel panel-bordered panel-black">
<div class="panel-heading">
<h3 class="panel-title"><?php echo translate('Total Amount ');?></h3>
</div>
<div class="panel-body">
<div class="text-center">
<h1>
<?php
echo $this->db->select('SUM(amount) as Total');
$getData = $this->db->get_where('tablename',array('amount = '!=0))->first_row();
echo $getData->Total;
?>
</h1>
</div>
</div>
</div>
</div>

Having trouble in creating a while loop

I have written CSS for two column layout and I have a "users" database with notes of the users.
So when I run a while loop like this: while($row = mysqli_fetch_array($result))
PHP should dynamically create html like this (depending on no. of users):
<div class="row">
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 1 -->
</div>
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 2 -->
</div>
</div>
<div class="row">
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 3 -->
</div>
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 4 -->
</div>
</div>
If there's is only one user, html should be like this:
<div class="row">
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 1 -->
</div>
</div>
And similarly if there are 3 users, there should one row with two columns and another row with one column.
Is there any way to do that?
Edit: Added my php code here.
<?php
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result)){
?>
<div class="row">
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
</div>
</div>
<?php } ?>
Problem with that code is it's creating a new row with "one column" for every user. My goal is to have "two columns" in the row.
you can achieve things like that with the modulo operator (%) within loops.
$i = 0;
$rowCount = mysqli_num_rows($result);
$rowsHTML = '';
while($row = mysqli_fetch_array($result)){
$notesHTML = '<div class="col">
<div class="col-content">'.$row['notes'].'</div>
<!-- Notes of user 1 -->
</div>';
if($i%2 == 0){
$notesHTML = '<div class="row">'.$notesHTML;
// check if its the last item and the row has to be closed after 1 col
if($i+1 == $rowCount){
$notesHTML = $notesHTML.'</div>';
}
} else {
$notesHTML = $notesHTML.'</div>';
}
$rowsHTML .= $notesHTML;
$i++;
}
echo $rowsHTML;
<?php
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);
$i = 0;
?>
<div class="row">
<?php
while($row = mysqli_fetch_array($result)){
if($i>1 && $i%2==0) echo '</div><div class="row">';
$i++;
?>
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
</div>
<?php } ?>
</div>

loop through Row PHP mysql

I was able to retrieve data from Mysql database however every 4 column I need to close row and start and new row
Following is my HTML code that I am trying to loop through columns and after every 4 columns close the row and keep adding columns from database.
Here is my code (I know this is not PDO and I am trying to learn so I can convert to PDO MySQLi connection)
<div class="row">
<div class="row margin-bottom-20">
<?php
include('dbconnect.php');
$query = "SELECT * FROM selection";
mysql_set_charset("UTF8");
$result = mysql_query($query) or die(mysql_error());
for($i=1; $row = mysql_fetch_array($result); $i++){
?>
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-tasks"></i> <strong><?php echo $row['Title']; ?></strong></h3>
</div>
<div class="panel-body">
<p>
<?php echo $row['ContactInfo']; ?><br/>
<img class="img-responsive" src="http://myurl.com/selections/<?php echo $row['file_url']; ?>" >
</p>
</div>
</div>
</div>
<?php
}
?>
I need to add a new for loop but I haven't been successful so far. Thank you for your help.
if($i%4==0)
{
//close the existing div and start new row div here
}
That will start a new row after every 4 iterations
Try this
<?php
include('dbconnect.php');
$query = "SELECT * FROM selection";
mysql_set_charset("UTF8");
$res = mysql_query($query) or die(mysql_error());
while($rows = mysql_fetch_array($res)){
$result[] = $rows;
}
$array = array_chunk($result, 4);
foreach ($array as $value) {
foreach ($value as $row){
?>
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-tasks"></i> <strong><?php echo $row['Title']; ?></strong></h3>
</div>
<div class="panel-body">
<p>
<?php echo $row['ContactInfo']; ?><br/>
<img class="img-responsive" src="http://myurl.com/selections/<?php echo $row['file_url']; ?>" >
</p>
</div>
</div>
</div>
<?php
}
}

How to include a filed to while loop query?

I have used foreach in my query for retrieving data but in here in the middle of the page I have a field $row['RemoteEmployeeID'] which is out of while loop and I am unable to include it to while loop, if I include it so my header is going to be repeated with each and every record.
Here is my code:
<html>
<head>
</head>
<body>
<div class="wrapper">
<div class="header">
<?php
error_reporting('0');
ob_start();
session_start();
if(!isset($_SESSION['email']))
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL= ../../spd/myaccount.php">';
exit();
}
else
{
$email = $_SESSION['email'];
require_once('../../Admin Panel/db.php');
$query = "SELECT
properties.PropertyID,
properties.PropertyType,
properties.PropertyDealType,
properties.Status,
properties.PropostedPrice,
properties.PropertyStatus,
properties.RemoteEmployeeEmail,
properties.PropertyDealerName,
properties.PropertyOwnerName,
remoteemployees.RemoteEmployeeID
FROM remoteemployees, properties
WHERE remoteemployees.RemoteEmployeeEmail = properties.RemoteEmployeeEmail
AND properties.RemoteEmployeeEmail = '".$email."'
AND properties.Status= 'Active'
AND properties.PropertyStatus= 'APPROVED'
ORDER BY properties.PropertyID
";
$query_run = $connection->query($query);
if( $connection->error ) exit( $connection->error );
while($res=$query_run->fetch_assoc()) {
$count = $query_run->num_rows; // Taking the number of queries returened by the result
$array[] = $res;
}
?>
<div style="float:left;"><font color="#000000">Welcome: <?php echo $_SESSION['email']; ?>
</font>
</div>
<div style="float:right; color:#999; width:600px; margin-left:155px;">
My Contract Copy
My Property List
My Trans.History
<a href="Remote Employee profile.php?RemoteEmployeeid=<?php echo $row['RemoteEmployeeID'] ?>" class="button" >My Profile</a>
Logout
</div>
</div>
<div class="Labelcon">
<div class="Label">Property ID</div>
<div class="Label">Property Type</div>
<div class="Label">Property Deal Type</div>
<div class="Labelowner">Property Owner</div>
<div class="Labeldealer">Property Dealer</div>
<div class="Label">Proposted Price</div>
<div class="Label">Property Status</div>
</div>
<?php
// Here is the start of the loop
foreach($array as $row) { ?>
<div class="Valuecon">
<div class="Value"><?php echo $row['PropertyID'] ?></div>
<div class="Value"><?php echo $row['PropertyType'] ?></div>
<div class="Value"><?php echo $row['PropertyDealType']?></div>
<div class="Valueowner"><?php echo $row['PropertyOwnerName'] ?></div>
<div class="Valuedealer"><?php echo $row['PropertyDealerName'] ?></div>
<div class="Value"><?php echo $row['PropostedPrice'];?></div>
<div class="Value"><?php echo $row['PropertyStatus'];?></div>
</div>
<?php
if($query_run->num_rows == 0)
{
echo 'No Records Found';
}
}}
?>
</div>
<p></p>
<center>
<?php $queryemail= "SELECT SUM(transactions.EarnAmount) AS TotalEarn, transactions.TaxDeduction,
SUM(transactions.AmountPaid) AS TotalPaid from transactions WHERE transactions.RemoteEmployeeID = ".$row['RemoteEmployeeID']." limit 1"; ?>
<div class="Amountpayablepaid">
<div class="amountpayable">
<div class="aclabel">Earn Amount:</div>
<div class="acvalue">
<?php
$queryemail_run = $connection->query($queryemail);
if( $connection->error ) exit( $connection->error );
while($row=$queryemail_run->fetch_assoc())
{
?>
<div class="USD">
<?php echo 'USD '.$row['TotalEarn']; ?>
</div>
</div>
<br>
<br>
</div>
<div class="amountpaid">
<div class="acpaidlable">Tax Deduction:</div>
<div class="acpaidvalue">
<div class="USDpaid"><?php echo $row['TaxDeduction'].' %'; ?></div>
</div>
</div>
<div class="amountpaid" style="margin-top:22px;">
<div class="acpaidlable">Amount Paid:</div>
<div class="acpaidvalue">
<div class="USDpaid"><?php echo 'USD '.$row['TotalPaid'];}?></div>
</div>
</div>
</div>
</center>
</body>
</html>
Its Simple:
$RemoteEmp = null;
$count = $query_run->num_rows;
while($res=$query_run->fetch_assoc()) {
$array[] = $res;
$RemoteEmp = $res["RemoteEmployeeID"];
}
Now :
<a href="Remote Employee profile.php?RemoteEmployeeid=<?php echo $RemoteEmp; ?>" class="button" >My Profile</a>

Categories