I have a Table which Input all the necessary data to do statistics. i now face the issue that i dont know how to add up the values for each colum but that it only adds the values up for each day. and then Displays it in a row or another Table.
this Code is what i use to call out all of the values and insert them into a table
<html>
<head>
<title>Your Home Page</title>
</head><body>
<table border="1">
<tr>
<th>Date</th>
<th>Participant ID</th>
<th>Gender</th>
<th>Hand</th>
<th>Udnder 18</th>
<th>Adult</th>
<th>R</th>
<th>C</th>
<th>L</th>
<th>Cash</th>
<th>Card</th>
<th>Ammount</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "***";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$sql = "SELECT * FROM table ORDER BY Date ASC";
$result = mysqli_query($mysqli, $sql) or die(mysqli_error());
$datum = '';
while($set = mysqli_fetch_assoc($result)) {
?>
<tr>
<td>
<?php
if ($datum == $set["Date"]) {
echo ' ';
} else {
echo $set["Date"];
}
?>
</td>
<td><?php echo $set["ParticipantID"]; ?></td>
<td><?php echo $set["Gender"]; ?></td>
<td><?php echo $set["Hand"]; ?></td>
<td><?php echo $set["Under18"]; ?></td>
<td><?php echo $set["Adult"]; ?></td>
<td><?php echo $set["r"]; ?></td>
<td><?php echo $set["c"]; ?></td>
<td><?php echo $set["l"]; ?></td>
<td><?php echo $set["Cash"]; ?></td>
<td><?php echo $set["Card"]; ?></td>
<td><?php echo $set["Ammount"]; ?></td>
</tr>
<?php $datum = $set["Date"]; ?>
<?php
}
?>
</table> </body></html>
So for 04-01-2018 for the colum R the total summed up value should be 2. etc.
Thanks for the help.
Kind Regards
Mark
The first way of solving this problem is to use the second MySQL query to get total values for each date. After that create a function printDateTableRow to print each row of the table. Using the mysqli_fetch_assoc($totalMysqliResult) parameter of this function, you can pass values of a row with total values for each new date.
Another way is to use the the WITH ROLLUP group modifier (you can try to write such query by yourself).
Here is the working code for the first way:
<html>
<head>
<title>Your Home Page</title>
</head>
<body>
<table border="1">
<tr>
<th>Date</th>
<th>Participant ID</th>
<th>Gender</th>
<th>Hand</th>
<th>Udnder 18</th>
<th>Adult</th>
<th>R</th>
<th>C</th>
<th>L</th>
<th>Cash</th>
<th>Card</th>
<th>Ammount</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "***";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$sql = "SELECT * FROM `table` ORDER BY Date ASC";
$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$totalMysqliResult = mysqli_query($mysqli, '
SELECT
Date, "" AS ParticipantID, "" AS Gender, "" AS Hand,
SUM(Under18) AS date_total_Under18,
SUM(Adult) AS date_total_Adult, SUM(r) AS date_total_r,
SUM(c) AS date_total_с, SUM(l) AS date_total_l,
SUM(Cash) AS date_total_Cash, SUM(Card) AS date_total_Card,
SUM(Amount) AS date_total_Amount
FROM `table`
GROUP BY Date
ORDER BY Date ASC') or die(mysqli_error($mysqli));
/**
* #param $row
* #param null $dateFieldHeader
*/
function printDateTableRow($row, $dateFieldHeader = null) {
?><tr><?
foreach ($row as $key => $curVal) {
?><td><?
if ($key == 'Date') {
echo $dateFieldHeader == null ? $curVal : $dateFieldHeader;
} else {
echo $curVal;
}
?></td><?
}
?></tr><?
}
$previousDate = null;
$oneDateRowsCount = 0;
while($row = mysqli_fetch_assoc($result)) {
if ($row['Date'] == $previousDate) {
$oneDateRowsCount++;
printDateTableRow($row, ' ');
} else {
if ($previousDate !== null) {
printDateTableRow(mysqli_fetch_assoc($totalMysqliResult), 'Date total');
}
printDateTableRow($row);
$oneDateRowsCount = 1;
}
$previousDate = $row['Date'];
}
// add total for the last rows with the same date
if ($oneDateRowsCount > 1) {
printDateTableRow(mysqli_fetch_assoc($totalMysqliResult), 'Date total');
}
?>
</body>
</html>
You should refactor this code in OOP style and use PDO instead of mysqli.
Related
I have a table and each , I want to select a data from the same table in my database.
For example, first <td> is first name, then the second <td> is phone number.
I got the command, but only the first command is showing output.
This is my php codes to open and connect to the database :
<?php
include("./inc/db_connect.php");
$conn = OpenCon();
?>
This is the php codes for the table including <th> and <td> :
<div class="layer55">
<h3>
<table class="flat-table">
<tbody>
<tr>
<th>
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if ($result = $conn->query($sql)) {
if ($result->num_rows > 0) {
echo "Name";
}
}
?>
</th>
<th>
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if ($result = $conn->query($sql)) {
if ($result->num_rows > 0) {
echo "Phone Number";
}
}
?>
</th>
</tr>
<tr>
<td>
<?php
$sql = "SELECT first_name FROM sharp_emp WHERE employee_id = 'AA170336'";
while ($row = $result->fetch_array()) {
echo "" . $row['first_name'] . "";
}
?>
</td>
<td>
<?php
$sql = "SELECT phone FROM sharp_emp WHERE employee_id = 'AA170336'";
while ($row = $result->fetch_array()) {
echo "" . $row['phone'] . "";
}
?>
</td>
</tr>
</tbody>
</table>
</h3>
</div>
This is the php codes for db_connect.php :
<?php
function OpenCon()
{
$dbhost = "localhost";
$dbuser = // Hidden;
$dbpass = // Hidden;
$db = "sharp_db";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
return $conn;
}
function CloseCon($conn)
{
$conn -> close();
}
?>
The expected output :
|----------|----------|
|Name |Phone Number|
|----------|----------|
|John |179898765 |
The current output :
|----------|----------|
|Name |Phone Number|
|----------|----------|
|John |Null (empty) |
You are running the same query multiple times, overwriting the $result variable for no reason, having useless $sql for the later 2 fetch without using them, and fetching a single $result twice by mistake.
So there are multiple concept problem with your code. I think your current code is something equivalant to this:
<div class="layer55">
<h3>
<table class="flat-table">
<tbody>
<tr>
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if ($result = $conn->query($sql)) {
if ($result->num_rows > 0) {
?>
<th>Name</th>
<th>Phone Number</th>
<?php } else { ?>
<th></th>
<th></th>
<?php } ?>
<?php } ?>
</tr>
<tr>
<?php if ($row = $result->fetch_array()) { ?>
<td><?php echo "" . $row['first_name'] . ""; ?></td>
<td><?php echo "" . $row['phone'] . ""; ?></td>
<?php } else { ?>
<td></td>
<td></td>
<?php } ?>
</tr>
</tbody>
</table>
</h3>
</div>
But frankly, it makes no sense to me to print an empty table when there is no result. So what you need is probably something like this.
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if (
($result = $conn->query($sql))
&& ($result->num_rows > 0)
&& ($row = $result->fetch_array())
):
?>
<div class="layer55">
<h3>
<table class="flat-table">
<tbody>
<tr>
<th>Name</th>
<th>Phone Number</th>
</tr>
<tr>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['phone']; ?></td>
</tr>
</tbody>
</table>
</h3>
</div>
<?php endif; ?>
I'm new to php and sql.
The codes below are written in php and working fine when connecting to a mysql database. SELECT query is working and UPDATE query is working.
But when connecting to an mssql database, the codes don't work well.
I need to convert them to connect to a similar mssql database.
Thank you.
<table id="data_table" class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Designation</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
$sql_query = "SELECT id, firstname, lastname, address, email FROM
myguests LIMIT 10";
$resultset = mysqli_query($conn, $sql_query) or die("database
error:". mysqli_error($conn));
while( $developer = mysqli_fetch_assoc($resultset) ) {
?>
<tr id="<?php echo $developer ['id']; ?>">
<td><?php echo $developer ['id']; ?></td>
<td><?php echo $developer ['firstname']; ?></td>
<td><?php echo $developer ['lastname']; ?></td>
<td><?php echo $developer ['address']; ?></td>
<td><?php echo $developer ['email']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
/* Database connection start */
$servername = "*****";
$username = "*****";
$password = "*****";
$dbname = "*****";
$conn = mysqli_connect($servername, $username, $password, $dbname) or
die("Connection failed: " . mysqli_connect_error());
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
<?php
include_once("db_connect.php");
$input = filter_input_array(INPUT_POST);
if ($input['action'] == 'edit') {
$update_field='';
if(isset($input['firstname'])) {
$update_field.= "firstname='".$input['firstname']."'";
} else if(isset($input['lastname'])) {
$update_field.= "lastname='".$input['lastname']."'";
} else if(isset($input['address'])) {
$update_field.= "address='".$input['address']."'";
} else if(isset($input['email'])) {
$update_field.= "email='".$input['email']."'";
}
if($update_field && $input['id']) {
$sql_query = "UPDATE myguests SET $update_field WHERE id='" .
$input['id'] . "'";
mysqli_query($conn, $sql_query) or die("database error:".
mysqli_error($conn));
}
}
The rough equivalent of MySQL LIMIT in SQL Server is TOP, so you may try something like:
SELECT TOP 10 id, firstname, lastname, address, email
FROM myguests
ORDER BY <some_column>;
Note carefully that I added an ORDER BY clause to your query. Using LIMIT (or TOP) without ORDER BY is fairly meaningless, because you haven't told SQL which 10 rows you want, relative to some ordering.
I am trying to retrieve all data from my database and displaying it in a table. But i could not do that, am facing some problem.I am getting error as,
This page isn’t working.
localhost is currently unable to handle this request.
Here is my code,
<html>
<body>
<table style="width:100%">
<tr>
<th>Driverid</th>
<th>Truckid</th>
<th>Imagecount</th>
<th>Trainingstatus</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "IDdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT DriverID, TruckID, Imagecount, Trainingstatus FROM IDs";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];?>
<tr>
<td><?php echo $driverid; ?></td>
<td><?php echo $truckid; ?></td>
<td><?php echo $imagecount; ?></td>
<td><?php echo $trainingstatus; ?></td>
</tr>
</table>
<?php}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
please replace your code
from
driverid = $row["Driverid"];
truckid = $row["Truckid"];
imagecount = $row["Imagecount"];
trainingstatus = $row["Trainingstatus"];
to
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];
please reader the section about how to define variables in php on the PHP Manual ,after that, check the variables defined in your code.
you have not started table tag, also not in loop, and main part is sdd space <?php} to <?php }
if ($result->num_rows > 0) {
echo '<table>';
// output data of each row
while($row = $result->fetch_assoc()) {
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];?>
<tr>
<td><?php echo $driverid; ?></td>
<td><?php echo $truckid; ?></td>
<td><?php echo $imagecount; ?></td>
<td><?php echo $trainingstatus; ?></td>
</tr>
<?php }
echo '</table>';
} else {
echo "0 results";
}
I have two pages, first is a page with table and the second page shows details from the table, for ex. Table shows No, Subject, Location, Date, And Piority but when i click one each row it pass the value from No Column to the second page and write it in No place. what i want in second page is to get details from database where No Column is the value i pass it from first page.
Here is my first page how i get the value to GET:
<?php
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) { ?>
<tr>
<td><?= $row['No'] ?></td>
<td><?= $row['subject'] ?></td>
<td><?= $row['location'] ?></td>
<td><?= $row['geo'] ?></td>
<td><?= $row['date'] ?></td>
<td><?= $row['piority'] ?></td>
</tr>
<?php
}
}
?>
here is my second page that i want to get data from database with No variable from first page:
<?php
$varPage = $_GET['No'];
$servername = "localhost";
$username = "bayansh_r";
$password = "u)nHf,Amo)";
$dbname = "bayansh_c";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn,"SELECT `news` FROM `editor3` WHERE No = '".$varPage."'");
while($row = mysqli_fetch_array($result))
?>
and now i want to write the News here is my code:
<p style="font-family:B Zar; direction:rtl; font-size:165%;"> <?= $row['news'] ?> </p>
but i get NULL result in Paragraph. is there any thing wrong with my code????
I have tried. I'ts working...
<?php
while( $row=mysqli_fetch_array($result)){
?>
<p style="font-family:B Zar; direction:rtl; font-size:165%;"><?php echo $row['news'];?></p>
<?php
}
?>
Here is yourfirst page how i get the value to GET:
<?php
if($result->num_rows > 0) {
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><? echo $row['No']; ?></td>
<td><? echo $row['subject']; ?></td>
<td><? echo $row['location']; ?></td>
<td><? echo $row['geo']; ?></td>
<td><? echo $row['date']; ?></td>
<td><? echo $row['piority']; ?></td>
</tr>
<?php
}
}
?>
here is your second page that you want to get data from database with No variable from first page:
<?php
$varPage = $_GET['No'];
$servername = "localhost";
$username = "bayansh_r";
$password = "u)nHf,Amo)";
$dbname = "bayansh_c";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn,"SELECT news FROM editor3 WHERE No = '".$varPage."'");
while($row = mysqli_fetch_assoc($result)){?>
<p style="font-family:B Zar; direction:rtl; font-size:165%;"><?php echo $row['news'];?></p>
<?php } ?>
Hope it will works if any problem please comment.
SO i have form which consist of "Event Name" "Event Description" "Event Date" and checkbox "is important". When i check checkbox value "yes" its important, it sends to the sql value = "1" to table "is_important". Everything is all right, but i give the bootstrap style "bg-danger" for that "is_important" = 1 table and it doesnt show up. What's the problem?
You can see in the code:
<?php
if (isset($_POST['important'])) {
$error = array();
$success = array();
$eventTime = time();
$important = $_POST['important'];
$eventName = trim(mysql_real_escape_string($_POST['EventName']));
$eventDesc = htmlentities(trim(mysql_real_escape_string($_POST['EventDesc'])), ENT_QUOTES);
if (!isset($eventName) || empty($eventName)) {
$error['eventName'] = "Prasome ivesti ivykio varda";
} else if (strlen($eventName) > 32 || strlen($eventName) < 3) {
$error['eventName'] = "Ivykio pavadinimas turi buti tarp 3 ir 32 simboliu";
}
if (!isset($eventDesc) || empty($eventDesc)) {
$error['eventDesc'] = "Prasome ivesti ivykio aprasyma";
}
if (empty($error)) {
$sql = "INSERT INTO notes_list (title, description, timestamp,is_important) VALUES ('$eventName', '$eventDesc','$eventTime','$important')";
$result = mysqli_query($con, $sql);
$success[] = "SEKME !";
} else {
}
}
?>
<table class="table table-striped">
<thead>
<tr>
<th>Event name</th>
<th>Event description</th>
<th>Event date</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM notes_list ORDER BY id DESC LIMIT 10";
$result2 = mysqli_query($con, $query);
print_r($_POST);
if ($result2) {
while ($note = mysqli_fetch_assoc($result2)) {
?>
<tr<?php echo (($note['is_important'] == 1) ? "class='bg-danger'" : ""); ?>>
<td><?php echo $note['title']; ?></td>
<td><?php echo $note['description'] ?></td>
<td><?php echo date('l M jS', $note['timestamp']); ?></td>
</tr>
<?php
}
mysqli_free_result($result2);
}
/* close connection */
mysqli_close($con);
?>
</tbody>
</table>
Full Example in this picture:
https://www.dropbox.com/s/h650h2spy2487dm/chechbox.jpg?dl=0
This:
<tr<?php echo (($note['is_important'] == 1) ? "class='bg-danger'" : ""); ?>>
would render this:
<trclass='bg-danger'>
in case is_important is 1. You need a space there, before the class.