Multiplication Table PHP - php

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

Related

How do I sum a group collection in Laravel?

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?

How to have data allow decimals

I made a program that lets 6 grades be entered into a form, and then calculates the letter grade and the average. The only problem I'm having is that I can't figure out how to get the form to accept decimals. I tried changing my filter_input statement to sanitize floats, but that doesn't seem to fix the problem. Any thoughts would be appreciated.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Grades</title>
<link rel="stylesheet" href="bootstrap.min.css">
<script>
window.onload = () => {
document.getElementById('quiz1').focus();
document.getElementById('quiz1').select();
}
</script>
<style>
input[type="text"] {width: 4em !important; text-align: right; margin-left: .5em; }
ul { list-style-type: none; padding-left: 0; }
</style>
</head>
<body>
<div class="container">
<div class="col-lg-6 mx-auto text-center">
<h1>Enter Quiz Grades</h1>
<form action="." method="post">
<?php
// declare an array to store bowling scores
$quizScores = array();
// declare a variable to be used for the natural counting of the number of games
$numberOfScores = 6;
// declare a variable to be used for array elements - subtract 1 since array elements start at 0
$gamesArrayElements = $numberOfScores - 1;
for ($i = 0; $i <= $gamesArrayElements; $i++){
$quizNumber = $i + 1;
// load values from POST into array elements
if (strlen($_POST['quiz' . $quizNumber]) > 0){
$quizScores[$i] = filter_input(INPUT_POST, 'quiz' . $quizNumber, FILTER_SANITIZE_NUMBER_FLOAT);
}
echo "<div class='form-group form-inline justify-content-center'>\n";
echo "<label for='quiz$quizNumber'>Quiz $quizNumber: </label>\n";
echo "<input type='text' class='form-control' name='quiz$quizNumber' id='quiz$quizNumber' value='$quizScores[$i]'>%\n";
echo "</div>\n";
}
?>
<div class="form-group">
<input type="submit" class="btn btn-secondary" value="Find Average of <?php echo $numberOfScores; ?> Highest Quiz Grades">
Clear
</div>
</form>
<?php
if (count($quizScores) === $numberOfScores){
$total = 0;
echo "<h2>Scores:</h2>\n";
echo "<ul>\n";
// for loop to print array elements and add each score to the total
for ($i = 0; $i <= $gamesArrayElements; $i++){
echo "<li>$quizScores[$i] " . getLetterGrade($quizScores[$i]) . "</li>\n";
$total += $quizScores[$i];
}
echo "</ul>\n";
$average = $total / $numberOfScores;
echo "<h2>Average Score:</h2>\n";
echo "<p>" . number_format($average, 1, .2) . " - " . getLetterGrade($average) . "</p>\n";
} else {
if(count($quizScores) > 0) {
echo "<h2 class='text-danger'>All textboxes should contain grades.</h2>\n";
}
}
?>
</div>
</div>
</body>
</html> ```
I changed number_format($average, 1, .2) to number_format($average, 1) and then I do get the average rounded to one decimal. I also removed the getLetterGrade() function because it is not defined in your code. Finally I change the form action to "", instead of ".", because it didn't work in my test environment.
Perhaps you need to switch on error reporting in PHP? That way you can see what's wrong with your code.

create border around icon depending on variable

What i currently have
what im trying to make
I need it when i throw for example 2 and 3 it goes down 2 and marks it from left to right using black borders, and then it goes 3 to the right and mark from top to bottem using black borders, at the point they cross i need it to be a red border.
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="form.css">
</head>
<body>
<div class="wrapper">
<?php
if(isset($_POST['submit'])) {
$dice1 = mt_rand(1,6);
$dice2 = mt_rand(1,6);
echo "<table class='table'>";
for ($i=0; $i < 6 ; $i++) {
echo "<tr>";
for ($j=0; $j < 6 ; $j++) {
echo "<td> <img class='icon cell color".mt_rand(1,4)."' src='img/icon".mt_rand(1,4).".svg ' </td>";
}
echo "</tr>";
}
echo "</table>";
echo "<img class='dice1' src='img/dice".$dice1.".gif'>";
echo "<img class='dice2' src='img/dice".$dice2.".gif'>";
}
?>
<form method="get" action="/php1/eindopdracht/cal.php">
<button type="submit" class="button1">renew</button>
</form>
<form method="POST">
<button type='submit' class='button2' name='submit' value='submit'>throw</button>
</form>
</div>
</body>
</html>```
You can check if dice1 value is equal to $i you can add a certain class that makes the color black. Same if dice2 value is equal to $i. If then dice1 is equal to $i and dice2 value is equal to $j choose a different class to make the border red.
function chooseBorder($i,$j,$dice1,$dice2) {
$class = "";
if ($dice1 == $i && $dice2 == $j) {
$class = "border-red";
return $class;
}
if ($dice1 == $i || $dice2 == $j) {
$class = "border-black";
return $class;
}
return $class;
}
for ($i=0; $i < 6 ; $i++) {
echo "<tr>";
for ($j=0; $j < 6 ; $j++) {
$class = chooseBorder($i,$j,$dice1,$dice2);
echo "<td> <img class='icon cell color".mt_rand(1,4). " " . $class . "' src='img/icon".mt_rand(1,4).".svg ' </td>";
}
echo "</tr>";
}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="form.css">
</head>
<body>
<div class="wrapper">
<?php
if(isset($_POST['submit'])) {
$dice1 = mt_rand(1,6);
$dice2 = mt_rand(1,6);
function chooseBorder($i,$j,$dice1,$dice2) {
$class = "";
if ($dice1 == $i && $dice2 == $j) {
$class = "border-red";
return $class;
}
if ($dice1 == $i || $dice2 == $j) {
$class = "border-black";
return $class;
}
return $class;
}
for ($i=1; $i < 7; $i++) {
echo "<tr>";
for ($j=1; $j < 7 ; $j++) {
$class = chooseBorder($i,$j,$dice1,$dice2);
echo "<td> <img class='icon cell color".mt_rand(1,4). " " . $class . "' src='img/icon".mt_rand(1,4).".svg ' </td>";
}
echo "</tr>";
}
echo "</table>";
echo "<img class='dice1' src='img/dice".$dice1.".gif'>";
echo "<img class='dice2' src='img/dice".$dice2.".gif'>";
}
?>
<form method="get" action="/php1/eindopdracht/cal.php">
<button type="submit" class="button1">renew</button>
</form>
<form method="POST">
<button type='submit' class='button2' name='submit' value='submit'>throw</button>
</form>
</div>
</body>
</html>

PHP - linking a table to HTML and doing multiplication

I have the following php code that generates a 10X10 table:
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 10; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 10; $col++) {
$a = "$row * $col";
echo "<td><a href = '$a'>$a</a></td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
How to recreate this table in HTML, such that links will work?
Every field in a table needs to do a multiplication, e.g. field '5*6' gives result '30'. How to write a php class that will do this operation? So, for row*column, return variable result.
in your Calculator.php
<?php
Class Calculator {
public function calculate($row, $col){
return $result = $row * $col;
}
}
Change index.php as follow.
<?php include_once('Calculator.php'); ?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
a{
cursor: pointer;
}
</style>
</head>
<body>
<div>
<input type="text" value="<?php if( (isset($_GET['col']) && $_GET['row'])){ echo Calculator::calculate($_GET['row'],$_GET['col']); } ?>">
</div>
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 10; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 10; $col++) {
$a = "$row * $col";
echo "<td><a href=?row=$row&col=$col>".$a;
//if( (isset($_GET['col']) && $_GET['row'] && $row==$_GET['row'] && $col==$_GET['col'])){ echo Calculator::calculate($row,$col); }else{ echo $a ;}
echo "</a></td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
$a = $row * $col;
Just remove the double quotes so it will multiple the numbers and not consider it a string.

create table of checkboxes with select all option

I am creating a table of checkboxes, where the first column will contain checkboxes that can check / uncheck the current row.
This is my code so far, which is not working.
<!DOCTYPE html>
<html>
<head>
<script language="JavaScript">
function toggle(source, $id) {
checkboxes = document.getElementsByName($id);
for (var i = 0, n = checkboxes.length; i < n; i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
</head>
<body>
<?php
echo "<table>";
for ($x = 0; $x < 3; $x++) {
$id = $x;
echo "
<tr>";
echo "
<td>";
echo '<input type="checkbox" onClick="toggle(this, $id)"/> Toggle All';
echo '
</td>
';
for ($y = 0; $y < 7; $y++){
echo "
<td>";
echo '<input type="checkbox" name=$id value="bar1"> Bar 1<br/>';
echo '
</td>
';
}
echo "
</tr>
";
}
echo "</table>";
?>
</body>
</html>
I am aware that the code might contain several errors and am trying to find them. In the meantime, i would appreciate any suggestions.
try this code
<!DOCTYPE html>
<html>
<head>
// your java script code for toggle is working only change is used id not $id.
<script language="JavaScript">
function toggle(source, id) {
checkboxes = document.getElementsByName(id);
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
</head>
<body>
<?php
echo "<table>";
for ($x = 0; $x < 3; $x++) {
$id = $x;
echo "<tr>";
echo "<td>";
echo '<input type="checkbox" onClick="toggle(this, '.$id.')" /> Toggle All';
// change here onClick="toggle(this, $id)" to onClick="toggle(this, '.$id.')"
echo '</td>';
for ($y = 0; $y < 7; $y++){
echo "<td>";
echo '<input type="checkbox" name="'.$id.'" value="bar1"> Bar 1<br/>';
// also change the name=$id to name="'.$id.'"
echo '</td>';
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
I am not sure about your javascript, but it for sure needs the change I made to it.
<!DOCTYPE html>
<html>
<head>
<script language="JavaScript">
// change to-> Id from-> $id
function toggle(source, Id) {
checkboxes = document.getElementsByName(Id);
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
</head>
<body>
<table>
<?php
for ($x = 0; $x < 3; $x++) {
$id = $x; ?>
<tr>
<td>
<!--$id won't work in single quotes like that so you have to break it out.
See comment below, but you may need to base the toggle on the class-->
<input type="checkbox" onClick="toggle(this, '<?php echo $id; ?>')" /> Toggle All
</td><?php
for ($y = 0; $y < 7; $y++){ ?>
<td>
<!-- You will likely need to array this name, because if the
names are all the same, it will only take one value -->
<input type="checkbox" name="<?php echo $id; ?>[]" class="sub-<?php echo $id; ?>" value="bar1"> Bar 1<br/>
</td><?php } ?>
</tr><?php
} ?>
</table>

Categories