I have a grouped collection of Items and Prices from my database in Laravel, I want to sum the total price in my blade template after each group. thus, to display the total sum after each group. Am unable to sum after the grouping. is there a different way to do that? please assist, thank you
my Code below:
my Controller
public function departmentSummary($id)
{
$items = \DB::table('invoices')
->where('user_id', $id)
->where('invoice_status', 1)
->whereDate('created_at', Carbon::today())
->get();
$grouped = $items->groupBy('invoice_category');
return view('inc.department-summary', [
'group' => $grouped->all()
]);
}
my view Template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Department Report</title>
</head>
<body>
<style>
body{
font-size:15px;
margin: auto;
padding: 15px;
}
table, th, td {
border: 1px solid black;
}
</style>
<?php
$department = $group;
foreach ($department as $category => $group) {
echo "<h2>$category</h2>";
echo "<table>";
foreach ($group as $group) {
echo "<tr>";
echo "<td>";
echo "Name of Item";
echo "</td>";
echo "<td>";
echo "Qty";
echo "</td>";
echo "<td>";
echo "Amount";
echo "</td>";
echo "</tr>";
echo "<td>".$group->invoice_product_name ."</td>";
echo "<td>".$group->invoice_quantity."</td>";
echo "<td>".$group->invoice_total_price."</td>";
}
echo "</table>";
echo "Total:" . $group->sum('invoice_total_price') ;
}
?>
</body>
</html>
echo "Total:" . $group->sum('invoice_total_price') ;
is not able to sum, How Can I properly do this?
Related
I wanted to create a multiplication table with a custom function and also get the number of rows and columns from the user, and I want if each row was an even number then its field would be red (background) and if it was odd number it would have a green background and i also write some codes but i'm not sure if it's true or not:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<form method="post">
<input type="number" name="rows" placeholder="Rows">
<input type="number" name="columns" placeholder="Columns">
<button type="submit" name="button">
Create
</button>
</form>
<table border="1px">
<?php
if (isset($_POST["button"])) {
$userRows = $_POST["rows"];
$userColumns = $_POST["columns"];
function multiplicationTable($rows, $columns)
{
$zarb = $rows * $columns;
return $zarb;
}
$x = 1;
while ($x <= $userRows) {
echo "<tr>";
$y = 1;
while ($y <= $userColumns) {
if ($x % 2 == 0) {
echo "<td style='background-color: red;'>" . multiplicationTable($x, $y) . "</td>";
} else {
echo "<td style='background-color: green;'>" . multiplicationTable($x, $y) . "</td>";
}
$y++;
}
$x++;
echo "</tr>";
}
}
?>
</table>
</body>
</html>
How about changing the while loops to this:
while ($x <= $userRows) {
echo "<tr>";
$y = 1;
while ($y <= $userColumns) {
$val = multiplicationTable($x, $y);
if ($val % 2 == 0) {
echo "<td style='background-color: red;'>" . $val . "</td>";
} else {
echo "<td style='background-color: green;'>" . $val . "</td>";
}
$y++;
}
$x++;
echo "</tr>";
}
Well, your code looks good. Click here to see how it looks like...
the final result is in this link, and even if some of the numbers are even, they're still displayed in green and i don't know what should i do
https://imgur.com/D1Sweee
So I have to roll 6 six sided die and display their corresponding images. I also have to save up to 10 previous roles and append the new ones each time I roll. I'm so far able to create the array of 6 random numbers and link them to their images. I just need to save that role on page refresh. I've just started learning PHP but from what I've gathered I know sessions are needed. All the examples I looked at didn't help.
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dice Roller</title>
</head>
<style>
h1, input {
margin-left: auto;
margin-right: auto;
}
img {
width: 70px;
height: 70px;
}
</style>
<body>
<h1>Dice Roller</h1>
<input type="button" value="Roll">
<?php
$newRoll = array(rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6));
$oldRoll = $newRoll;
foreach($newRoll as $num) {
echo '<img src="img/dicefaces/dice0' . $num . '.png" alt="">';
}
echo "<br>";
foreach($oldRoll as $num) {
echo '<img src="img/dicefaces/dice0' . $num . '.png" alt="">';
}
?>
</body>
</html>
You can use $_SESSION to 'store' the old rolls, following logic might help you on your way:
We store the history in $_SESSION['oldrolls'] allowing us to display up to 10 previous rolls (you can set the number you want to show with $max)
Also created a button to allow for session clear, to start with a clean slate.
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dice Roller</title>
</head>
<style>
table, td, th {
table-layout: fixed;
width: 100%;
border-collapse: collapse;
border: 3px solid black;
text-align: center;
}
h1, input {
margin-left: auto;
margin-right: auto;
}
img {
width: 70px;
height: 70px;
}
</style>
<body>
<h1>Dice Roller</h1>
<!-- allow user to clear the session -->
<form action='' method ='POST'>
<input type="submit" name='clear' value="clear session">
</form>
<!-- submit a roll -->
<br />
<form action='' method ='POST'>
<input type="submit" name='submit' value="Roll">
</form>
<?php
if(isset($_POST['clear'])){
$_SESSION = []; // clear session array
}
if(isset($_POST['submit'])) {
$newRoll = array(rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6), rand(1, 6));
$max = 10; // store up to 10 previous rolls
$count = isset($_SESSION['oldrolls']) ? count($_SESSION['oldrolls']) : 0;
if($count <= $max) {
$_SESSION['oldrolls'][] = $newRoll; // add new
} else {
array_shift($_SESSION['oldrolls']); // remove oldest
$_SESSION['oldrolls'][] = $newRoll; // add new
}
$_SESSION['oldrolls-reverse'] = array_reverse($_SESSION['oldrolls']); // show last first
echo 'Current roll...';
echo '<br />';
foreach ($newRoll as $num) {
echo $num;
echo '<br />';
}
echo "<br>";
echo 'previous rolls...';
echo '<br />';
echo '<table>';
foreach ($_SESSION['oldrolls-reverse'] as $key => $num) {
if($key === 0) continue;
echo '<tr>';
foreach($num as $roll) {
echo '<td>' . $roll . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
?>
</body>
</html>
I'm creating a calculator using PHP and would like to show what has been calculated (list the calculated history) but I have no idea how to store and display the information.
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic PHP Calculator</title>
</head>
<body>
<?php
//var_export($_POST);
//echo "<br>";
$buttons=[1,3,5,'+',7,9,0,'/','C','='];
$pressed='';
if(isset($_POST['pressed']) && in_array($_POST['pressed'],$buttons)){
$pressed=$_POST['pressed'];
}
$stored='';
if(isset($_POST['stored']) && preg_match('~^(?:[\d.]+[*/+-]?)+$~',$_POST['stored'],$out)){
$stored=$out[0];
}
$display=$stored.$pressed;
//echo "$pressed & $stored & $display<br>";
if($pressed=='C'){
$display='';
}elseif($pressed=='=' && preg_match('~^\d*\.?\d+(?:[*/+-]\d*\.?\d+)*$~',$stored)){
$display.=eval("return $stored;");
}
echo "<form action=\"\" method=\"POST\">";
echo "<table style=\"width:300px;border:solid thick black;\">";
echo "<tr>";
echo "<td colspan=\"4\">$display</td>";
echo "</tr>";
foreach(array_chunk($buttons,4) as $chunk){
echo "<tr>";
foreach($chunk as $button){
echo "<td",(sizeof($chunk)!=4?" colspan=\"4\"":""),"><button name=\"pressed\" value=\"$button\">$button</button></td>";
}
echo "</tr>";
}
echo "</table>";
echo "<input type=\"hidden\" name=\"stored\" value=\"$display\">";
echo "</form>";
?>
</body>
</html>
Expected to get a list of what has been calculated like:
Calculate history:
2+2=4
3+4=7
Append results in SESSION only when isequalto(=) button pressed:
session_start();
elseif($pressed=='=' && preg_match('~^\d*\.?\d+(?:[*/+-]\d*\.?\d+)*$~',$stored)){
$display.=eval("return $stored;");
$_SESSION["list"].=$display."<br>";
}
And finally display the history:
<?php echo $_SESSION["list"];?>
Unfortunately my code isn't working quite well.
The source code:
<!DOCTYPE html>
<html>
<head>
<title>Query data from News database and display in table</title>
<meta charset="UTF-8">
<meta name="description" content="" />
<meta name="author" content="WRBikAir" />
<meta name="keywords" content="" />
<style>
table, th, td{
border: 1px solid black;
}
</style>
</head>
<body>
<?php
error_reporting(E_ALL);
echo "Test 1";
$con = mysqli_connect("mysql.hostinger.com","u441817146_admin","CBGApp","u441817146_cbg");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM News";
if ($result = mysqli_query($con, $sql)) {
echo "<table>";
while($row = $result->fetch_assoc()) {
$r = json_encode($row);
echo "<tr><td>" . $r['NID'] . "</td><td>" . $r['headline'] . "</td><td>" . $row['text'] . "</td><td>" . $r['timestamp'] . "</td></tr>";
}
echo "</table>";
} else {
echo "no result.";
}
mysqli_close($con);
echo "2";
?>
</body>
</html>
Everything works fine, except of the output of the NID, headline and timestamp. There are all '{'. Does it mean, that there is now value? because if I simply print them out (encoded of course) there are values e.g.:
{"NID":"1","headline":"Testartikel 2","text":"test test test","timestamp":"15.11.2017, 18:13"}
Does somebody knows a solution?
You are using $result 2 times on $result = mysqli_query($con, $sql) and in your foreach loop. I changed the one in the foreach loop to $results instead of $result.
Also try turning on error reporting using:
error_reporting(E_ALL);
Try using:
<!DOCTYPE html>
<html>
<head>
<title>Query data from News database and display in table</title>
<meta charset="UTF-8">
<meta name="description" content="" />
<meta name="author" content="WRBikAir" />
<meta name="keywords" content="" />
<style>
table, th, td{
border: 1px solid black;
}
</style>
</head>
<body>
<?php
echo "Test 1";
$con = mysqli_connect(CENSORED (but working in other classes fine);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM News";
if ($result = mysqli_query($con, $sql)) {
$resultArray = array();
$tempArray = array();
while($row = $result->fetch_object()) {
$tempArray = $row;
array_push($resultArray, $tempArray);
}
echo "<table>";
foreach ($resultArray as $results) {
$r = json_encode($results);
echo "<tr><td>" . $results['headline'] . "</td><td>" . $results['text'] . "</td></tr>";
}
echo "</table>"
}
mysqli_close($con);
echo "2";
?>
</body>
</html>
For everybody who need the working answer.
Thank you to MasterOfCoding, GrumpyCrouton, Paul Spiegel and prodigitalson.
Special thanks to tadman for the insider information.
Now the code:
<!DOCTYPE html>
<html>
<head>
<title>Query data from News database and display in table</title>
<meta charset="UTF-8">
<meta name="description" content="" />
<meta name="author" content="WRBikAir" />
<meta name="keywords" content="" />
<style>
table, th, td{
border: 1px solid black;
}
</style>
</head>
<body>
<?php
error_reporting(E_ALL);
echo "Test 1";
$con = mysqli_connect("...","...","...","...");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM News";
if ($result = mysqli_query($con, $sql)) {
echo "<table>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row['NID'] . "</td><td>" . $row['headline'] . "</td><td>" . $row['text'] . "</td><td>" . $row['timestamp'] . "</td></tr>";
}
echo "</table>";
} else {
echo "no result.";
}
mysqli_close($con);
echo "2";
?>
</body>
</html>
Good Day to you.This is what I have been trying to do.
I have a php page where I have drop down list with options as lecturers and students.
Once I select any option, details regarding lecturers or students will be displayed in a div using ajax.
Code is as follows.(This will be called in onchange event of the drop down list with a javascript.
<?php
session_start();
require "configuration.php";
$q = intval($_GET['q']);
if($q==1)
{
$sql="SELECT * FROM temp_instructor_log";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th></th>
<th><u><B>NIC</u></B></th>
<th><u><B>Name</u></B></th>
<th><u><B>Email</u></B></th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>"?>
<input type="checkbox" name="emplist[]" value="<?php echo $row['nic']?>">
<?php echo "</td>";
echo "<td>" . $row['nic'] . "</td>";
echo "<td>" . $row['firstName'] ." ".$row['lastName']. "</td>";
echo "<td>" . $row['email']."</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
}
?>
Its working and giving me the display with checkboxes at the very beginning.
<?php session_start();
require "configuration.php";
?>
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Manage courses</title>
<script type="text/javascript">
</script>
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
if (isset($_POST['btnAccept']))
{
$utype=$_POST['category'];
$arr = $_POST['emplist'];
foreach( $arr as $item)
{
echo $item . "</br>"; /* Will print 1,2 and 3 (mind newlines)*/
}
First I tried to echo the values after checking some of the checkboxes.But it didnt work out.Your kind consideration given on this is highly appreciated.
Thanks in advance.